2

The problem: When converting from XML to JSON, quotation marks should only be used for strings and not be used for numbers, booleans, etc

The following XML:

<root>
    <someNumber>22</someNumber>
    <someBoolean>true</someBoolean>
    <someString>23</someString>
</root>

Should be mapped to this JSON:

{
    root: {
        someNumber: 22,
        someBoolean: true,
        someString: "23"
    }   
}

And not this:

{
    root: {
        someNumber: "22",
        someBoolean: "true",
        someString: "23"
    }   
}

Using type information from the XSD:

<xs:complexType name="someComplexType">
    <xs:all>
        <xs:element name="someNumber" type="xs:integer"/>
        <xs:element name="someBoolean" type="xs:boolean"/>
        <xs:element name="someString" type="xs:string"/>
    </xs:all>
</xs:complexType>

Currently I'm using JsonConvert.SerializeXNode (from Newtonsoft Json.NET)

I have all the type information in the XSD (Xml Schema), and do not want to add any additional type information using XML attributes, as suggested when using JsonConvert.SerializeXNode.

Is it possible to provide the XSD when converting from JSON to XML?

Could anyone please point me in the right direction?

andersh
  • 8,105
  • 6
  • 39
  • 30
  • Json.NET has no such built-in logic, see https://www.newtonsoft.com/json/help/html/ConvertingJSONandXML.htm – dbc Oct 31 '18 at 14:31
  • I know @dbc, so I have to use something else. It seems JSON.Net only supports this using XML attributes: https://github.com/lukegothic/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs#L995 – andersh Oct 31 '18 at 14:35
  • There seem to be some tools in other frameworks, e.g. [Converting XML to JSON using XML Schema](https://stackoverflow.com/q/24174963) or [Build xml based on xml schema in Node.js](https://stackoverflow.com/q/29367474) (javascript) and [Conversion XML => JSON => XML based on a XSD Schema](https://stackoverflow.com/q/47031693) (Java). – dbc Oct 31 '18 at 14:38
  • Thanks @dbc, were building and Angular7/TypeScript application on top (that's why we have to convert between JSON and XML in the first place), so I'll look into it – andersh Oct 31 '18 at 14:41
  • @andersh I have the same mapping requirements, did you find a soulution ? – proless8 Sep 18 '22 at 06:44

0 Answers0