stleary / JSON-java

A reference implementation of a JSON package in Java.
http://stleary.github.io/JSON-java/index.html
Other
4.54k stars 2.56k forks source link

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

Closed anki006 closed 8 months ago

anki006 commented 9 months ago

While parsing an XML which has hexadecimal value which starts with number (0-9), for example below: `

primary
<value>008E97</value>

`

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. `

primary
<value>fc4c02</value>

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 commented 9 months ago

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

javadev commented 9 months ago

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 commented 9 months ago

Hello @stleary Any update on this issue?

stleary commented 9 months ago

@anki006 I believe this may be related to #826

johnjaylward commented 9 months ago

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

stleary commented 8 months ago

@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.