roc-lang / book-of-examples

Software Design by Example in Roc
Other
23 stars 15 forks source link

topic: JSON ADT and codecs #43

Open monmcguigan opened 1 month ago

monmcguigan commented 1 month ago

I'm imagining writing an ADT for JSON structure and then showing how you can write encoders/decoders for this representation.

It would cover modelling data with algebraic data types, pattern matching, encoding/decoding etc.

I like this topic because I feel like there's lots of different details and levels you can dive into.

gvwilson commented 1 month ago

Cool - there was some discussion of this in #14 as well. I think there may be a JSON encoding/decoding package for Roc, but building a scale model from the ground up to show people how it works (in the way that chapters in the JavaScript and Python books built simplified versions of regular expression matchers to show how they work) would be awesome. Cc @FabHof (who's doing a binary encoder/decoder) for thoughts and to synchronize.

monmcguigan commented 1 month ago

Ah I wasn't aware there already was a JSON codec lib in Roc, I will sync with @FabHof on this.

FabHof commented 1 month ago

For reference: https://github.com/lukewilliamboswell/roc-json

To be honest, I'm not sure about the scope of my part. If you want to talk about Encoding/Decoding I can focus more on the binary representation and bit shifting stuff.

Also this discussion on zulip might be relevant: https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/Encoding.20that.20can.20fail

gvwilson commented 1 month ago

Hi again @monmcguigan - just to be clear, the fact that there's a JSON codec in Roc is an argument for including your chapter, not against it: building a scale model that includes the core features of the full module will help people understand how and why it does what it does, and they're more likely to believe this is a "real" problem if there's something for it in the standard library. I'm going to assign this one to you - if you and @FabHof can chat here, on Zulip, or by email about where yours will stop and theirs will start, that would be awesome.

rtfeldman commented 1 month ago

@monmcguigan A thought on this: there are also different approaches to decoding JSON, which beginners might benefit from. This article talks about starting from a simple JsonValue type and then shows where that runs into limitations which motivates building out a decoder system.

So the "scale model" approach Greg mentioned could work, and another way would be to show an alternative (simpler) design to the one found in the roc-json package, and then talk about the tradeoffs of that simpler design which motivate the decoding library.