5 ms·
It seems like you're asking a more general question unrelated to JSON. Rather you want to know how to manipulate data without all the ceremony of a strong type
by alephu5 5y ago
It seems like you're asking a more general question unrelated to JSON. Rather you want to know how to manipulate data without all the ceremony of a strong type system, as you would in JavaScript or python. This is completely valid and is the best choice for simple jobs.
If you have a set of key value pairs where all the values are the same, you can use a Map or HashMap. These are both easily serialisable.
I'm not sure there's a way of having key-value pairs without a schema and with different types of values. If it has a schema you can use a record of course.
If you just want to add a single value, such as pdfUrl for example, you can include this as a nullable (Maybe Text) value in the record. Receive the data without it and return it with it. If you wanted to, you could also use a type parameter to guarantee that a pdfUrl is always included in the response record.
- square_usual 5y ago> If you have a set of key value pairs where all the values are the same, you can use a Map or HashMap. These are both easily serialisable. This is ultimately the choice most people suggested back then, but I have two issues with it: 1. It leads to a ton of boilerplate, first in the step that converts the request record to a map, then adding stuff to the map and then serializing it. Instead, consider what just adding a field to what you know is a record is in F#: let newRecord = {| oldRecord with token = token |} This is vey close to the spread syntax of JS, and is super intuitive and elegant to write. 2. You lose type safety: say your JSON object has a bunch of things of different types within it, like a record with a few records or arrays inside. If you want to convert that to a map, you effectively have to make it a Dictionary<string, obj>, or whatever the Haskell equivalent for obj would be (obj is the "base class" in F#: everything is an obj.) That means you completely lose type information from that point on. The closest any functional language gets to what I would consider "ideal" in this scenario is ReScript, which allows you to type row polymorphic open objects and use JS-like syntax to work with them. This is in addition to good ol' records, of course. Reason is pretty good, in this aspect, too. The closest any functional language gets to this
- lalaithion 5y agoHaskell's type-level programming is pretty strong, and so if you're willing to use some language extensions you can get let newRecord = oldRecord & #token .~ token as seen in https://hackage.haskell.org/package/row-types https://hackage.haskell.org/package/row-types The syntax isn't great, admittedly, but if you're willing to write a template haskell macro you could probably get something like let newRecord = [json| oldRecord with token = token |] working.
- mixedCase 5y agoYour schemas only have to include types for things you use. Don't feel you have to type your entire schema.