microsoft/PowerPlatform-DataverseServiceClient

DataContractJsonSerializer throws exception - Value cannot be null. (Parameter 'key')

Closed this issue · 4 comments

It's basically what I put up here on Stackoverflow: https://stackoverflow.com/q/76973417/2410655

Using Microsoft.PowerPlatform.Dataverse.Client, version 1.0.1.

I have a JSON string I want to deserialize to Microsoft.Xrm.Sdk.Entity object:

{
    "Id": "28bdb958-71fd-e711-8129-5065f38be461",
    "Name": "name",
    "Attributes": [
        { "Key": "key1", "Value": "value1" },
        { "Key": "key2", "Value": "value2" }
    ]
}

and when I pass it to the following code (let's name the JSON string jsonString):

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using Microsoft.Xrm.Sdk;

var serializer = new DataContractJsonSerializer(typeof(Entity));
using var stream = new MemoryStream(Encoding.ASCII.GetBytes(jsonString));
var entity = serializer.ReadObject(stream) as Entity;

it throws an exception:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'key')
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<TKey,TValue>>.Add(KeyValuePair`2 keyValuePair)
   at ReadAttributeCollectionFromJson(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 ReadEntityFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] )

I found out that if the key and value label from the JSON string is lower case, it would work!

{
    "Id": "28bdb958-71fd-e711-8129-5065f38be461",
    "Name": "name",
    "Attributes": [
        { "key": "key1", "value": "value1" },
        { "key": "key2", "value": "value2" }
    ]
}

I have created 2 dotnetfiddles you can try:

I am not sure if it's a bug.

Hello @davidliang2008
you can take a look at my library https://github.com/albanian-xrm/PowerPlatform-Entity-Serializer for serialization/deserialization of the Dataverse SDK objects. Maybe it can help Identify the issues you are facing.

@BetimBeja: the library got passed the Key Value issue, but then it failed on somewhere else, like Unable to cast object of type 'System.Object' to type 'Microsoft.Xrm.Sdk.OptionSetValue'.

The Library should be able to serialize all to JSON and back... the serialization from JSON to Entity happens only if it was generated from the library as it has some type information encoded as well... I might try and add some logic to try casting unknown formats like this... maybe add a parameter to distinguish between the current format and other formats. The main problem is that Attributes are Objects so we don't know what that object maps to at deserialization time...

Is this sorted for you ? and if so can you close it @davidliang2008 ?

thanks