8000 Make it easier to generate JSON documents · Issue #820 · inko-lang/inko · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make it easier to generate JSON documents #820

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

Open
yorickpeterse opened this issue Feb 12, 2025 · 0 comments
Open

Make it easier to generate JSON documents #820

yorickpeterse opened this issue Feb 12, 2025 · 0 comments
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module std Changes related to the standard library

Comments

@yorickpeterse
Copy link
Collaborator

Description

The std.json module provides a Json type that's used as both the output of the DOM parsing API, and as input for generating JSON strings. Using this API to generate objects is rather verbose, as you end up with something like this:

import std.json (Json)

let map = Map.new

map.set('name', Json.String('Alice'))
map.set('age', Json.Int(42))
Json.Object(map).to_string

https://github.com/fres621/inko-fastjson provides a DSL for generating JSON that makes this a little easier. We could adopt a similar approach such that one can write this:

import std.json (Json)

Json.object.string('name', 'Alice').int('age', 42).to_string

Here Json.object would return some sort of ObjectBuilder type that wraps a Map, and calling to_string basically does Json.Object(@map).to_string.

The caveat here is that ToString.to_string is an fn method meaning it can't take ownership of the internal Map value, but Json.Object needs an owned Map. This means we'd have to define to_string as fn move to_string and just not implement ToString, which is honestly not that big of a deal.

Similarly, we could add the following static methods for generating arrays:

  • Json.ints
  • Json.floats
  • Json.strings
  • Json.bools

These would all take an owned array and convert those into Array[Json], then return a Json.Array wrapping that resulting array.

Note that this is just one possible idea, and there may be better options that we should explore.

Related work

No response

@yorickpeterse yorickpeterse added accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module std Changes related to the standard library labels Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting contributions Issues that are suitable to be worked on by anybody, not just maintainers feature New things to add to Inko, such as a new standard library module std Changes related to the standard library
Projects
None yet
Development

No branches or pull requests

1 participant
0