Make it easier to generate JSON documents #820
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
Description
The
std.json
module provides aJson
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: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:
Here
Json.object
would return some sort ofObjectBuilder
type that wraps aMap
, and callingto_string
basically doesJson.Object(@map).to_string
.The caveat here is that
ToString.to_string
is anfn
method meaning it can't take ownership of the internalMap
value, butJson.Object
needs an ownedMap
. This means we'd have to defineto_string
asfn move to_string
and just not implementToString
, 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 aJson.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
The text was updated successfully, but these errors were encountered: