decoder2: rework decoding of sumtypes#24949
Conversation
|
Connected to Huly®: V_0.6-23428 |
|
I don't think that |
|
To avoid the divergence break between the json, json2 and decoder2 libraries, I think it is worth investing in integrating decoder2 with json2. It won't be long before this becomes possible and it would avoid a huge headache. You are doing a very dedicated job and I am sure you will succeed |
|
I remove the json2.Null decoding and allowed options to decode null as none, so now you can do: type NewAny = int | string | bool | []NewAny | map[string]NewAny | ?int
decoder2.decode[NewAny]('{"name": null, "value": [0, 1, "hi"]}')!
// NewAny({'name': NewAny(Option(none)), 'value': NewAny([NewAny(0), NewAny(1), NewAny('hi')])})
I also made some minor fixes to the checker, so that it no longer throws errors for |
As for integrating into json2, I only found 2 things that don't work. The first one is that the number conversion will not correctly checks types (will try to convert -1.92 into a u8 with no error), which is easy to fix. |
|
I also removed all references to |
|
@felipensp @spytheman I think there's a feature or discussion about every sumtype being optional, right? How can I test whether a sumtype has been initialized or not? |
|
If not, I think we should think about it a bit. I think it's a bit dangerous for decoders to be forced to choose a type when the decoded value has no defined type |
|
For example, what happened here, nothing tells me that the null came from an integer value. But I also don't see how this could have been done better. assert json.decode[Maybes]('null')! == Maybes(?int(none)) |
There is not. Sumtype values are not options. The default value for a sumtype, is the default value for the first variant in the sumtype list. |
|
In any case, JSON on its own lacks the concept of sumtypes, so we are free to implement whatever encoding/decoding for them that we want to. |
|
I still think the changes in this commit are correct, options should decode null as none and decoder2 should not have any references to null or any types. I plan to implement custom decoders anyways, so you could do struct Null implements CustomNullDecoder {}
type Any = string | f64 | bool | []Any | map[string]Any | Nullwithout any references to Null or Any types. |
|
custom decoders could be used for time.Time or math.big too. |
| break | ||
| } | ||
| fn (mut decoder Decoder) check_element_type_valid[T](element T, current_node Node[ValueInfo]) bool { | ||
| $if T.unaliased_typ is json2.Any { |
There was a problem hiding this comment.
Personally speaking, I'm against using Any within decoder2 as if it were different from any other sumtype. I think that what works for Any, should work for any other sumtype. To keep the json library simple and avoid unexpected behavior
|
Good work!!! In case you're interested, I always ran benchmarks |
|
@Larsimusrex I also think this is excellent work. Thank you. |

Reworks the sumtype decoder to allow for more complex sumtypes and better error handling.
New Features
could not resolve sumtype Sum, got string_instead ofExpected number, but got string_type MultiArray = []int | []bool. This also enhances error messages.BREAKS
Because the decoder now gives errors, instead of resolving bogus sumtypes, sumtypes with structs behave differently from json and json2. e.g.