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

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

Closed
anki006 opened this issue Jan 30, 2024 · 6 comments
Closed

Comments

@anki006
Copy link

anki006 commented Jan 30, 2024

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.

@stleary
Copy link
Owner

stleary commented Jan 30, 2024

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

@javadev
Copy link
Contributor

javadev commented Feb 1, 2024

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"
}

@anki006
Copy link
Author

anki006 commented Feb 12, 2024

Hello @stleary Any update on this issue?

@stleary
Copy link
Owner

stleary commented Feb 13, 2024

@anki006 I believe this may be related to #826

@johnjaylward
Copy link
Contributor

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

@stleary
Copy link
Owner

stleary commented Feb 28, 2024

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants