Custom ToJSON instance for Persistent Key

I am building a web application in Haskell. I am using a persistent library to connect to the postgresql database.

I use the standard schema definition file system where the Haskell pattern is used to generate types from the schema.

share [mkPersist sqlSettings, mkMigrate "migrateAll"]
  $(persistFileWith lowerCaseSettings "schema")

I have many data types defined in my schema file that look like (fake example):

User json
  email Text

Post json
  owner UserId
  name  Text
  body  Text

The "json" next to the "Post" indicates that ToJSON / FromJSON should be automatically generated by the framework.

I use this automatic instance creation for many of my types so that I can serialize them over the network.

: ToJSON . , "UserId" " ". , , "PostId" "Key Post" ..

"Post" , "" , , "52".

. , , "52", "fiftytwo" ( ).

, -

instance ToJSON (Key record) where
  toJSON _ = Data.Aeson.String "placeholder"

- . , "ToJSON (Key Post)" .. ?

, .

newtypes, , .

!

+4
1

haoformayor, . , mkKeyTypeDec.

0
source

Source: https://habr.com/ru/post/1679443/


All Articles