elm icon indicating copy to clipboard operation
elm copied to clipboard

Concept: JSON Decoders

Open jiegillet opened this issue 3 years ago • 6 comments

Design

Goal

The Goal is to learn to use the Json.Decode and Json.Encode modules.

Learning objectives

Students should be able to

  • encode any JSON Value (Json.Encode.object, list and other primitives)
  • prettify a value (encode)
  • understand why decoders are useful even though the concept is unusual
  • decode JSON primitives (bool, int, string...)
  • decode data structures (list, dict, nullable)
  • decode objects (field, at)
  • deal with uncertainty (maybe, oneOf)
  • modify and chain decoders (map, andThen, succeed)
  • test a decoder on a string input (decodeString)

Out of scope

  • encoder aliases (array, set)
  • more niche decoders (array, keyValuePairs, oneOrMore, index, null, value)
  • dealing with Error and fail
  • decoding recursive structures (lazy, a quick mention is fine)

Concepts

The concept this exercise unlocks is:

  • json

Prerequisites

  • strings
  • lists
  • dict
  • records
  • maybe
  • custom-types (?)

Analyzer

TBD depending on the exercise


Comments

  • Would be nice to implement rest-api first to have one practice exercise
  • link: https://guide.elm-lang.org/effects/json.html

jiegillet avatar Nov 27 '22 05:11 jiegillet

@ceddlyburge related to our discussion on #621, I went through the exercises that we don't have implemented yet (bin/configlet info) to look for other exercises that we could use to practice JSON decoding, but hopefully a bit simpler than rest-api.

Basically no other is meant to practice JSON, so we would have to stretch some scopes.

I found two that may be interesting: pov and zipper. Those two exercises involve trees, pov is bout rerooting trees and zipper traversing trees. I say that they are interesting because parsing trees is fun, all the other exercises have very standard, predictable inputs. Unfortunately, these 2 are not really easy, so it doesn't help us that much.

If we are OK with very simple decoding, maybe we could use affine-cipher.

Best for last: bank-account could work as well, transforming the string instructions into meaningful types.

What do you think?

jiegillet avatar Oct 24 '23 13:10 jiegillet

I think the api concept is still good, its just that it is complicated, and the instructions are not as clear as they might be (especially for Elm I think).

Maybe we could jump on a call sometime and discuss how to make it clear what the student is supposed to do?

ceddlyburge avatar Oct 24 '23 14:10 ceddlyburge

Oh I still intend to keep rest-api, and I don't think having a difficult exercise is inherently a problem (I love those as a student), I'm just looking for another one that could help practicing decoders. I don't think that rest-api is THAT complicated to understand if you read the description and the tests carefully though (I'll still try to make it clearer).

jiegillet avatar Oct 25 '23 00:10 jiegillet

Hmmm, none of those look that good. Maybe we could create a new exercise. Json encoding / decoding is something that people do find confusing when starting Elm, so it is a good idea. Maybe encoding a request to the github api (which is well known), and decoding a response?

ceddlyburge avatar Oct 25 '23 07:10 ceddlyburge

Creating a proper new practice exercise is way more work than I'm willing to do! I guess we'll need a really good concept exercise :)

jiegillet avatar Oct 26 '23 13:10 jiegillet

Maybe encoding a request to the github api (which is well known), and decoding a response?

Good idea to work on a concrete real-life thing. We'd have to rebuild the JS Value ourselves directly in the test though as obviously there is no outside world access in tests file.

mpizenberg avatar Oct 26 '23 15:10 mpizenberg