Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON library #11

Open
CrowdHailer opened this issue Apr 28, 2020 · 2 comments
Open

JSON library #11

CrowdHailer opened this issue Apr 28, 2020 · 2 comments
Labels
Gleam Feature needed in language or stdlib Suggestion PR's will be accepted but not on the Roadmap wontfix This will not be worked on

Comments

@CrowdHailer
Copy link
Contributor

CrowdHailer commented Apr 28, 2020

Jiffy makes use of NIF's
jsone is simpler, jsx handles streaming etc

@CrowdHailer CrowdHailer added this to the 0.2.0 milestone Apr 29, 2020
@CrowdHailer
Copy link
Contributor Author

I will consider this done enough for milestone 0.2.0 the readme in gleam_jsone is enough information for now. However a general input parsing would be good, something that worked equally on querys form data and json.

@CrowdHailer CrowdHailer removed this from the 0.2.0 milestone Apr 29, 2020
@CrowdHailer CrowdHailer added wontfix This will not be worked on Suggestion PR's will be accepted but not on the Roadmap labels May 5, 2020
@CrowdHailer CrowdHailer added the Gleam Feature needed in language or stdlib label Jun 10, 2020
@CrowdHailer
Copy link
Contributor Author

For building arbitrary JSON data, a functional interface is probably best. e.g.

import jason.{object, string, boolean}
let body = jason.encode(
    object(
      [
        tuple("id_token", string(id_token)),
        tuple(
          "userinfo",
          object(
            [
              tuple("sub", string(email_address)),
              tuple("email", string(email_address)),
              tuple("email_verified", boolean(True)),
            ],
          ),
        ),
      ],
    ),
  )

This is extensible with your own types, for example dates etc.
Also for simplicity I think the return value from object should be the same as the return value from a decode function.

Certainly the above is similar in complexity to building with types. but it is cheaper, less intermediate data structures and with the benefit of extensibility.

  JsonObject(
    [
      tuple("id_token", JsonString(id_token)),
      tuple(
        "userinfo",
        JsonObject(
          [
            tuple("sub", JsonString(email_address)),
            tuple("email", JsonString(email_address)),
            tuple("email_verified", JsonBool(True)),
          ],
        ),
      ),
    ],
  ),

And is conceptually simpler to building your own dynamic data structures.

  let body = jason.encode(
    dynamic.from(
      map.from_list(
        [
          tuple("id_token", dynamic.from(id_token)),
          tuple(
            "userinfo",
            dynamic.from(
              map.from_list(
                [
                  tuple("sub", dynamic.from(email_address)),
                  tuple("email", dynamic.from(email_address)),
                  tuple("email_verified", dynamic.from(True)),
                ],
              ),
            ),
          ),
        ],
      ),
    ),
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gleam Feature needed in language or stdlib Suggestion PR's will be accepted but not on the Roadmap wontfix This will not be worked on
Development

No branches or pull requests

1 participant