codec icon indicating copy to clipboard operation
codec copied to clipboard

ADTs example

Open BebeSparkelSparkel opened this issue 5 years ago • 1 comments

I have figured out how to parse an ADT but am not sure how to serialize it well. Instead of the constructor being the initial function, a function that picks the constructor would be the starting function. However, I am not sure how to continue from there.

Using JSON:

data StringEncoded
  = Date Year Month Day
  | Money Currency Int

stringEncodedObjCodec :: JSONCodec StringEncoded
stringEncodedObjCodec = ???

BebeSparkelSparkel avatar Oct 31 '20 21:10 BebeSparkelSparkel

Json data

{"type": "date"
,"year": 1234
,"month": 5
,"day": 6
}
{"type": "money"
,"currency": "USD"
,"amount": 1234
}

BebeSparkelSparkel avatar Oct 31 '20 22:10 BebeSparkelSparkel