Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractJsonSerializer doesn't work with formatted JSON?

Tags:

json

c#

.net

wcf

When using the DataContractJsonSerializer to deserialize JSON, if the input has tabs in it (formatted JSON) then the serializer throws an exception (shown below). If I replace all of the spaces, tabs and new-lines with "", the serializer is able to deserialize it just fine.

What's the deal?

Exception

System.MemberAccessException: Cannot create an abstract class.
at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type) 
at System.Runtime.Serialization.FormatterServices.GetUninitializedObject(Type type) 
at System.Runtime.Serialization.XmlFormatReaderGenerator.UnsafeGetUninitializedObject(Int32 id) 
at ReadBaseSearchElementFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] ) 
at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) 
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) 
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns) 
at ReadSearchElementsFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract ) 
at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) 
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) 
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns) 
at ReadSearchGroupFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] ) 
at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader) 
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) 
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns) 
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName) 
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) 
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) 
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader) 
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)

Formatted JSON

This does not deserialize, throwing the 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 a sample solution which exhibits this problem.

like image 520
Josh M. Avatar asked Apr 28 '11 20:04

Josh M.


2 Answers

Most of your formatting is actually fine. You just can't have any whitespace between { and "__type".

See this previous answer: JSON deseralization to abstract list using DataContractJsonSerializer

So for instance, your sample code is trying to deserialize this 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 it to this, and the 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"
                }
            ]
        }
    ]
}
like image 75
Tim Goodman Avatar answered Oct 23 '22 08:10

Tim Goodman


Weird. You may want to try JSON.NET, a nice library for JSON serialization and deserialization. I've used it in a project recently and it worked in cases where the DataContractJsonSerializer had failed.

You can find it at http://json.codeplex.com/

like image 1
Roy Dictus Avatar answered Oct 23 '22 08:10

Roy Dictus