v icon indicating copy to clipboard operation
v copied to clipboard

json: make enum working with json encode+decode (string+int serialization)

Open felipensp opened this issue 1 year ago • 9 comments

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
}

felipensp avatar Mar 17 '23 19:03 felipensp

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.

JalonSolov avatar Mar 17 '23 19:03 JalonSolov

Please check #17510

It's a good idea to serialize as strings (the enum names), instead of the int values.

Ok. Encode done.

felipensp avatar Mar 17 '23 19:03 felipensp

 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}

medvednikov avatar Mar 17 '23 20:03 medvednikov

 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?

felipensp avatar Mar 18 '23 01:03 felipensp

Looks like you just need to update the test.

JalonSolov avatar Mar 18 '23 01:03 JalonSolov

Looks like you just need to update the test.

Done.

felipensp avatar Mar 18 '23 02:03 felipensp

Hrm. vls needs to be updated as well.

JalonSolov avatar Mar 18 '23 12:03 JalonSolov

According to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessage: image

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 avatar Mar 18 '23 13:03 spytheman

@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

medvednikov avatar Mar 18 '23 13:03 medvednikov

According to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#window_showMessage: image

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

felipensp avatar Mar 22 '23 00:03 felipensp