Skip to content

Information is lost when converting hexadecimal value from xml to json. #852

Description

@anki006

While parsing an XML which has hexadecimal value which starts with number (0-9), for example below:
<color> <color_type>primary</color_type> <value>008E97</value> </color>

The equivalent json is formed is:
"color": [ { "color_type": "primary", "value": 8e+97 } ]

However, if the hexadecimal value starts with string, its equivalent json is also a string.
<color> <color_type>primary</color_type> <value>fc4c02</value> </color>
The equivalent json is formed is:
"color": [ { "color_type": "primary", "value": "fc4c02" } ]

The code fails to handle hexadecimal conversion and creates ambiguity by treating it as number and string both. I assume, if there is ambiguities like this, we need to stick to string rather than converting to number.

Activity

  1. stleary commented on Jan 30, 2024

    @stleary
    Owner

    @anki006 Thanks for bringing this issue up, will look into it.

  2. javadev commented on Feb 1, 2024

    @javadev
    Contributor

    xml

    <color> <color_type>primary</color_type> <value>008E97</value> </color>

    may be converted to json

    {
      "color": {
        "color_type": "primary",
        "value": "008E97"
      },
      "#omit-xml-declaration": "yes"
    }
  3. anki006 commented on Feb 12, 2024

    @anki006
    Author

    Hello @stleary Any update on this issue?

  4. stleary commented on Feb 13, 2024

    @stleary
    Owner

    @anki006 I believe this may be related to #826

  5. johnjaylward commented on Feb 13, 2024

    @johnjaylward
    Contributor

    Pretty sure this is just another case of someone not using the "keep strings" setting in the xml parser

  6. stleary commented on Feb 28, 2024

    @stleary
    Owner

    @anki006 After some review - JSON does not recognize hex as a valid type. In this case, the 'e' char is causing the string to be interpreted as a number in scientific notation, which is supported. Agreed, you should be keeping the values as strings.

    For example, XML.toJSONObject(str, new XMLParserConfiguration().withKeepStrings(true));

    Where str contains the XML to be converted to JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions