Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of strings containing only numbers #20

Closed
ronron-gh opened this issue Jun 16, 2024 · 8 comments
Closed

Conversion of strings containing only numbers #20

ronron-gh opened this issue Jun 16, 2024 · 8 comments

Comments

@ronron-gh
Copy link

When converting from YAML to JSON using deserializeYml(), even if I explicitly specified that the numbers are strings by enclosing them in double quotes, they were converted as numbers (int, float) as shown below. I'm sorry, but I haven't been able to find out the cause yet.

YAML

  yaml:
    str1: "1"
    str2: "1234567890"
    str3: "1234567890a"

YAML to JSON conversion result ( displayed with serializeJsonPretty() )

  "yaml": {
    "str1": 1,               ←intとして変換された
    "str2": 1.23456789e9,    ←floatとして変換された
    "str3": "1234567890a"    ←アルファベットを含んでいれば文字列として変換された
  },
@tobozo
Copy link
Owner

tobozo commented Jun 16, 2024

hi,

thanks for reporting this 👍

the wrong type guessing is probably caused by a poor decision tree.

the failing test:

bool is_string = (end == scalar || *end);

idea: it should set is_string to true when the value of yamlNode->data.scalar.style is one of YAML_SINGLE_QUOTED_SCALAR_STYLE / YAML_DOUBLE_QUOTED_SCALAR_STYLE

            // (...)
            if( is_string && yaml_node_is_bool( yamlNode, &bool_value ) ) {
              is_bool = true;
            }
            if( yamlNode->data.scalar.style == YAML_SINGLE_QUOTED_SCALAR_STYLE
             || yamlNode->data.scalar.style == YAML_DOUBLE_QUOTED_SCALAR_STYLE ) {
              is_string = true;
            }
            switch( nt ) 
            // (...)

@ronron-gh
Copy link
Author

Thank you for your response!
I was able to confirm that it works.
I would be happy if you could incorporate it into the next version.

YAML to JSON conversion result

  "yaml": {
    "str1": "1",
    "str2": "1234567890",
    "str3": "1234567890a"
  },

@tobozo
Copy link
Owner

tobozo commented Jun 16, 2024

thanks for testing this, I'll produce a new release soon

@tobozo
Copy link
Owner

tobozo commented Jun 16, 2024

whoops found a bug with ArduinoJSON 6.x so I'll restrict the next release to ArduinoJSON 7.x

more code needs to be migrated as JsonObject::createNestedArray and JsonArray::createNestedObject methods are being deprecated

@ronron-gh
Copy link
Author

I get it.

That's tough ^^;

@tobozo
Copy link
Owner

tobozo commented Jun 16, 2024

looks good

integer: 1234567890
quoted_integer: "1234567890"
float: 12.3323
quoted_float: "12.3323"
{
    "integer": 1234567890,
    "quoted_integer": "1234567890",
    "float": 12.3323,
    "quoted_float": "12.3323"
}

@ronron-gh
Copy link
Author

Thank you for the commit!

@tobozo
Copy link
Owner

tobozo commented Jun 18, 2024

is it worth trying to handle both ArduinoJson 6.x and 7.x ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants