c# - DataContractJsonSerializer doesn't work with formatted JSON? -


when using datacontractjsonserializer deserialize json, if input has tabs in (formatted json) serializer throws exception (shown below). if replace of spaces, tabs , new-lines "", serializer able deserialize fine.

what's deal?

exception

system.memberaccessexception: cannot create abstract class. @ system.runtime.serialization.formatterservices.nativegetuninitializedobject(runtimetype type)  @ system.runtime.serialization.formatterservices.getuninitializedobject(type type)  @ system.runtime.serialization.xmlformatreadergenerator.unsafegetuninitializedobject(int32 id)  @ readbasesearchelementfromjson(xmlreaderdelegator , xmlobjectserializerreadcontextcomplexjson , xmldictionarystring , xmldictionarystring[] )  @ system.runtime.serialization.json.jsonclassdatacontract.readjsonvaluecore(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.jsondatacontract.readjsonvalue(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.xmlobjectserializerreadcontextcomplexjson.readdatacontractvalue(datacontract datacontract, xmlreaderdelegator reader)  @ system.runtime.serialization.xmlobjectserializerreadcontext.internaldeserialize(xmlreaderdelegator reader, string name, string ns, type declaredtype, datacontract& datacontract)  @ system.runtime.serialization.xmlobjectserializerreadcontextcomplex.internaldeserialize(xmlreaderdelegator xmlreader, int32 declaredtypeid, runtimetypehandle declaredtypehandle, string name, string ns)  @ readsearchelementsfromjson(xmlreaderdelegator , xmlobjectserializerreadcontextcomplexjson , xmldictionarystring , xmldictionarystring , collectiondatacontract )  @ system.runtime.serialization.json.jsoncollectiondatacontract.readjsonvaluecore(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.jsondatacontract.readjsonvalue(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.xmlobjectserializerreadcontextcomplexjson.readdatacontractvalue(datacontract datacontract, xmlreaderdelegator reader)  @ system.runtime.serialization.xmlobjectserializerreadcontext.internaldeserialize(xmlreaderdelegator reader, string name, string ns, type declaredtype, datacontract& datacontract)  @ system.runtime.serialization.xmlobjectserializerreadcontextcomplex.internaldeserialize(xmlreaderdelegator xmlreader, int32 declaredtypeid, runtimetypehandle declaredtypehandle, string name, string ns)  @ readsearchgroupfromjson(xmlreaderdelegator , xmlobjectserializerreadcontextcomplexjson , xmldictionarystring , xmldictionarystring[] )  @ system.runtime.serialization.json.jsonclassdatacontract.readjsonvaluecore(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.jsondatacontract.readjsonvalue(xmlreaderdelegator jsonreader, xmlobjectserializerreadcontextcomplexjson context)  @ system.runtime.serialization.json.xmlobjectserializerreadcontextcomplexjson.readdatacontractvalue(datacontract datacontract, xmlreaderdelegator reader)  @ system.runtime.serialization.xmlobjectserializerreadcontext.internaldeserialize(xmlreaderdelegator reader, string name, string ns, type declaredtype, datacontract& datacontract)  @ system.runtime.serialization.xmlobjectserializerreadcontextcomplex.internaldeserialize(xmlreaderdelegator xmlreader, type declaredtype, datacontract datacontract, string name, string ns)  @ system.runtime.serialization.json.datacontractjsonserializer.internalreadobject(xmlreaderdelegator xmlreader, boolean verifyobjectname)  @ system.runtime.serialization.xmlobjectserializer.internalreadobject(xmlreaderdelegator reader, boolean verifyobjectname, datacontractresolver datacontractresolver)  @ system.runtime.serialization.xmlobjectserializer.readobjecthandleexceptions(xmlreaderdelegator reader, boolean verifyobjectname, datacontractresolver datacontractresolver)  @ system.runtime.serialization.json.datacontractjsonserializer.readobject(xmldictionaryreader reader)  @ system.runtime.serialization.json.datacontractjsonserializer.readobject(stream stream) 

formatted json

this does not deserialize, throwing exception above.

{     "mode":"and",     "elements":     [         {             "name":"id",             "operator":"equal",             "value":"3"         }     ] } 

non-formatted json

this does deserialize.

{"mode":"and","elements":[{"name":"id","operator":"equal","value":"3"}]} 

update

i've posed sample solution exhibits problem.

most of formatting fine. can't have whitespace between { , "__type".

see previous answer: json deseralization abstract list using datacontractjsonserializer

so instance, sample code trying deserialize json string:

"{\r\n\t\"mode\":\"and\",\r\n\t\"elements\":\r\n\t[\r\n\t\t{\r\n\t\t\t\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\"name\":\"lastname\",\r\n\t\t\t\"operator\":\"equal\",\r\n\t\t\t\"value\":\"smith\"\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"__type\":\"searchgroup:#jsontest.search\",\r\n\t\t\t\"mode\":\"or\",\r\n\t\t\t\"elements\":\r\n\t\t\t[\r\n\t\t\t\t{\r\n\t\t\t\t\t\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\t\t\"name\":\"firstname\",\r\n\t\t\t\t\t\"operator\":\"equal\",\r\n\t\t\t\t\t\"value\":\"tim\"\r\n\t\t\t\t},\r\n\t\t\t\t{\r\n\t\t\t\t\t\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\t\t\"name\":\"firstname\",\r\n\t\t\t\t\t\"operator\":\"equal\",\r\n\t\t\t\t\t\"value\":\"tom\"\r\n\t\t\t\t}\r\n\t\t\t]\r\n\t\t}\r\n\t]\r\n}" 

formatted:

{     "mode":"and",     "elements":     [         {             "__type":"searchparameter:#jsontest.search",             "name":"lastname",             "operator":"equal",             "value":"smith"         },         {             "__type":"searchgroup:#jsontest.search",             "mode":"or",             "elements":             [                 {                     "__type":"searchparameter:#jsontest.search",                     "name":"firstname",                     "operator":"equal",                     "value":"tim"                 },                 {                     "__type":"searchparameter:#jsontest.search",                     "name":"firstname",                     "operator":"equal",                     "value":"tom"                 }             ]         }     ] } 

change this, , error goes away:

"{\r\n\t\"mode\":\"and\",\r\n\t\"elements\":\r\n\t[\r\n\t\t{\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\"name\":\"lastname\",\r\n\t\t\t\"operator\":\"equal\",\r\n\t\t\t\"value\":\"smith\"\r\n\t\t},\r\n\t\t{\"__type\":\"searchgroup:#jsontest.search\",\r\n\t\t\t\"mode\":\"or\",\r\n\t\t\t\"elements\":\r\n\t\t\t[\r\n\t\t\t\t{\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\t\t\"name\":\"firstname\",\r\n\t\t\t\t\t\"operator\":\"equal\",\r\n\t\t\t\t\t\"value\":\"tim\"\r\n\t\t\t\t},\r\n\t\t\t\t{\"__type\":\"searchparameter:#jsontest.search\",\r\n\t\t\t\t\t\"name\":\"firstname\",\r\n\t\t\t\t\t\"operator\":\"equal\",\r\n\t\t\t\t\t\"value\":\"tom\"\r\n\t\t\t\t}\r\n\t\t\t]\r\n\t\t}\r\n\t]\r\n}" 

formatted:

{     "mode":"and",     "elements":     [         {"__type":"searchparameter:#jsontest.search",             "name":"lastname",             "operator":"equal",             "value":"smith"         },         {"__type":"searchgroup:#jsontest.search",             "mode":"or",             "elements":             [                 {"__type":"searchparameter:#jsontest.search",                     "name":"firstname",                     "operator":"equal",                     "value":"tim"                 },                 {"__type":"searchparameter:#jsontest.search",                     "name":"firstname",                     "operator":"equal",                     "value":"tom"                 }             ]         }     ] } 

Comments