我注意到,在我到XML轉換從JSON,如果我的JSON對象之一爲null,對象是從XML缺席,例如空屬性都將被丟棄/與JSONIX
JSON
{ "name": { "namespaceURI": "", "localPart": "myApp", "prefix": "", "key": "myApp", "string": "myApp" }, "value": { "TYPE_NAME": "generated.MyApp", "inputData": [{ "TYPE_NAME": "generated.MyApp.InputData", "dataType": "text", "mandatory": false, "length": 25, "valid": true, "value":null }] } }
轉換爲XML輸出(marshalString)
var context2 = new j.Context([generated2]); var marshaller2 = context2.createMarshaller(); var objectAsXMLString2 = marshaller2.marshalString(template2);
<myApp> <inputData> <dataType>text</dataType> <mandatory>false</mandatory> <length>25</length> <valid>true</valid> </inputData> </myApp>
話又說回來轉換回JSON(unmarshalString)
var unmarshaller2 = context2.createUnmarshaller(); var resultJSON2 = unmarshaller2.unmarshalString(objectAsXMLString2);
{ "name": { "namespaceURI": "", "localPart": "myApp", "prefix": "", "key": "myApp", "string": "myApp" }, "value": { "TYPE_NAME": "generated.MyApp", "inputData": [{ "TYPE_NAME": "generated.MyApp.InputData", "dataType": "text", "mandatory": false, "length": 25, "valid": true }] } }
所以你看,有信息丟失。當發生完整的轉換週期時,JSON - > XML - >返回到JSON,一些屬性不會被保留。任何null值都會被刪除 - 在這種情況下,它是屬性的「值」。對於期望某個對象上存在某些屬性的下游應用程序,這是一個問題。這是用於生成jsonix映射對象
我的XML模式看起來像這樣
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="myApp"> <xs:complexType> <xs:sequence> <xs:element name="inputData" maxOccurs="unbounded" minOccurs="0"> <xs:complexType> <xs:sequence > <xs:element type="xs:string" name="dataType"/> <xs:element type="xs:boolean" name="mandatory"/> <xs:element type="xs:short" name="length"/> <xs:element type="xs:string" name="elementId"/> <xs:element type="xs:string" name="formId"/> <xs:element type="xs:anySimpleType" name="value" /> <xs:element type="xs:boolean" name="valid"/> <xs:element type="xs:short" name="precision"/> <xs:element type="xs:short" name="scale"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
而生成的映射使用jsonix-架構編譯器
var generated_Module_Factory = function() { var generated = { name: 'generated', typeInfos: [{ localName: 'MyApp', typeName: null, propertyInfos: [{ name: 'inputData', minOccurs: 0, collection: true, elementName: { localPart: 'inputData' }, typeInfo: '.MyApp.InputData' }] }, { localName: 'MyApp.InputData', typeName: null, propertyInfos: [{ name: 'dataType', required: true, elementName: { localPart: 'dataType' } }, { name: 'mandatory', required: true, elementName: { localPart: 'mandatory' }, typeInfo: 'Boolean' }, { name: 'length', required: true, elementName: { localPart: 'length' }, typeInfo: 'Short' }, { name: 'elementId', required: true, elementName: { localPart: 'elementId' } }, { name: 'formId', required: true, elementName: { localPart: 'formId' } }, { name: 'value', required: true, elementName: { localPart: 'value' }, typeInfo: 'AnySimpleType' }, { name: 'valid', required: true, elementName: { localPart: 'valid' }, typeInfo: 'Boolean' }, { name: 'precision', required: true, elementName: { localPart: 'precision' }, typeInfo: 'Short' }, { name: 'scale', required: true, elementName: { localPart: 'scale' }, typeInfo: 'Short' }] }], elementInfos: [{ typeInfo: '.MyApp', elementName: { localPart: 'myApp' } }] }; return { generated: generated }; }; if (typeof define === 'function' && define.amd) { define([], generated_Module_Factory); } else { var generated_Module = generated_Module_Factory(); if (typeof module !== 'undefined' && module.exports) { module.exports.generated = generated_Module.generated; } else { var generated = generated_Module.generated; } }
是否有在轉換期間保留所有屬性的方法,即使它們爲空?