v
v copied to clipboard
json: make enum working with json encode+decode (string+int serialization)
Fix #15093
This PR changes enum to be encode to string, and performs string->enum decoding.
It also add attribute [json_as_number]
to encode/decode enum as int.
[json_as_number]
pub enum MessageType {
error = 1
warning = 2
info = 3
log = 4
}
Please check https://github.com/vlang/v/issues/17510
It's a good idea to serialize as strings (the enum names), instead of the int values.
Please check #17510
It's a good idea to serialize as strings (the enum names), instead of the int values.
Ok. Encode done.
FAIL [ 134/1353] 351.882 ms vlib/json/json_test.v
/home/runner/work/v/v/vlib/json/json_test.v:21: fn test_simple
> assert s == '{'name':'Peter','age':28,'salary':95000.5,'title':2}'
Left value:
{"name":"Peter","age":28,"salary":95000.5,"title":"executive"}
Right value:
{"name":"Peter","age":28,"salary":95000.5,"title":2}
FAIL [ 134/1353] 351.882 ms vlib/json/json_test.v /home/runner/work/v/v/vlib/json/json_test.v:21: fn test_simple > assert s == '{'name':'Peter','age':28,'salary':95000.5,'title':2}' Left value: {"name":"Peter","age":28,"salary":95000.5,"title":"executive"} Right value: {"name":"Peter","age":28,"salary":95000.5,"title":2}
Yes. We need expect a break about old json generated. What do you think?
Looks like you just need to update the test.
Looks like you just need to update the test.
Done.
Hrm. vls needs to be updated as well.
According to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessage:
i.e. the Language Server Protocol requires that to be encoded as an integer.
We would have to change VLS. I do agree that in general this change (encoding enum values with their string names) is good, since it makes such encodings more robust to future changes.
However imho we should also support an easy way to opt out of it, and let software like VLS use that, so that it does not break its observable intentional behavior.
What do you think about something like this:
[json_as_number]
pub enum MessageType {
error = 1
warning = 2
info = 3
log = 4
}
@spytheman yes, I agree, it should be possible to opt out. It's just about json though, so it should be something like
[encode_as_number]
or [use_ints]
or something
According to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessage:
i.e. the Language Server Protocol requires that to be encoded as an integer.
We would have to change VLS. I do agree that in general this change (encoding enum values with their string names) is good, since it makes such encodings more robust to future changes.
However imho we should also support an easy way to opt out of it, and let software like VLS use that, so that it does not break its observable intentional behavior.
What do you think about something like this:
[json_as_number] pub enum MessageType { error = 1 warning = 2 info = 3 log = 4 }
I've submitted the PR https://github.com/vlang/vls/pull/433