V's none
can be mapped to JSON/TOML as an empty map.
#25265
Replies: 2 comments
-
Making sum type Details in issue #25267 . |
Beta Was this translation helpful? Give feedback.
-
GolangGolang has json version 1 and version 2 and detailed instructions on how to migrate . TLDR;
Similar to this discussion, versions changed the way to represent emptiness with whether JSON's
And also specific objects holds preferences about how to select between
V json2In reality JSON is more complex than our first thoughts and is convenient to have a way to define and redefine how to convert V from/to JSON objects. I think Web services importanceThe web services written in different languages communicate with JSON messages that should be simple and flexible enough to prevent security issues and prevent wrong results. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Mapping V's option
none
to JSON is not defined currently and is pending. Ifnone
is a value of a map, the key/value pair can be omitted from the JSON map. But ifnone
is an element of an array with options,none
cannot be omitted since the JSON array length would be affected.V's optional array
[?int(0), none, 2]
could be encoded as the valid JSON text[0,null,2]
.But alternatively we can define a sum type
Elem = int | Empty
where Empty is an empty structstruct Empty { }
and then have and array converted to[Empty(0), { }, 2 ]
and converted to (also) valid JSON[0,{},2]
.The sum type method seems more complicated but the text works also for TOML, a format that does not have a
null
as JSON does.In the next program valid TOML text
elems = [ 0, {}, 2 ]
is converted to json text (TOML uses json2). Then the produced json text is encoded correctly byjson2
into a V's type ofmap[string][]Elem
.The problem with this idea is that decoder fails and while testing I filled issues #25261, #25262 and #25263 .
If json2 encoding/decoding can include EncodingOptions, then
none
could be mapped asnull
or empty map{ }
as a preference.Beta Was this translation helpful? Give feedback.
All reactions