Got the #MidnightMurderParty editor back in working condition! As I thought, tonight was dedicated to entirely rewriting the #JSON encoding/decoding using #PureScript Argonaut. Mostly, it was pretty simple—just a lot of rewriting—but I did run into one super sneaky “gotcha!” right at the end. For some reason, my encoding was leaving out a field, no matter what I was encoding.
"secretKey" := secretKey ~> "data" := postData
This was leaving out the "data" field, for example. I only figured out what was wrong because, after the server got mad at me for sending bad data (or no "data", rather), I dumped my other JSON to the console and dug through the keys until I spotted the correlation. Whichever field was encoded last just didn’t get encoded at all.
Turns out the fix was simple:
"secretKey" := secretKey ~> "data" := postData ~> jsonEmptyObject
Yup, just slap an empty JSON object onto the end of the extend (~>) chain, and it works.
11 May json midnightmurderparty PureScript
Huh, I wonder why it skips the last one. Seems like odd behaviour. 🤔
11 May
@larouxn - The
extendfunction (aliased to the~>operator) takes a key-value pair and some base thing to extend. Looking at the source code, it basically says “if the thing to extend is aJObject, then add the key-value pair to the object. If it’s anything else, make a singleton object out of the key-value pair.extend ("secretKey" := secretKey) ("data" := postData)ended up making that first key-value pair into a singleton object because("data" := postData)was also a key-value pair, not an object to extend. By adding an empty object at the end of the chain, it all wraps up nicely into that object instead of failing and creating a singleton.The reason only the last field disappeared was because, in the absence of a base object, the key-value pair before the last got turned into a singleton object for everything else to be added to, and the last got discarded.
11 May
Oh wow, okay, sounds crazy to me but I guess that’s somewhat reasonable functional behaviour. Well, glad you figured it out. Nice work. 👍
11 May
Good explanation, thanks! That does seem like a huge gotcha that’s not at all obvious unless you understand the underlying implementation.
11 May
Yup, I did find an example of using the
jsonEmptyObjectin the docs, but that was only after I guessed I needed to put something at the end and went looking for it.11 May