Announcements
Hi All
I am sure I have built solutions before that use json to serialize and deserialize the Entity object. Using the newer Nuget package Microsoft.PowerPlatform.Dataverse.Client in .net 7 I am finding that I cannot deserialize specifically I get the error:
"System.NotSupportedException: The collection type 'Microsoft.Xrm.Sdk.DataCollection`2[System.String,System.Object' is abstract, an interface, or is read only, and could not be instantiated and populated."
The AttributeCollection in this library is:
[Serializable] public abstract class DataCollection : IEnumerable<KeyValuePair>, IEnumerable { ..... }
I am sure that this collectio was not abstract in the full .net framework version hence the new issue. Is there a simple way around this to achieve the desired outcome?
Thanks in advance
Nick
You can use this excellent library called Xrm-Entity-Serializer github.com/.../Xrm-Entity-Serializer
Here is an implementation example:
you can use a custom json deserializer and register it with the JSON serialization library you're using. You can achieve this with different libraries like Newtonsoft.Json or System.Text.Json by following these steps:
Create a custom deserializer class that inherits from JsonConverter and overrides the ReadJson and CanConvert methods.
In the ReadJson method, you can handle the deserialization of the DataCollection type.
Register the custom deserializer by using the JsonConverterAttribute on the property or the class that uses the abstract type.
Here's an example of a custom deserializer for the DataCollection class using Newtonsoft.Json:
public class DataCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DataCollection<string, object>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var dictionary = new Dictionary<string, object>();
reader.Read();
while (reader.TokenType == JsonToken.PropertyName)
{
var key = (string)reader.Value;
reader.Read();
var value = serializer.Deserialize(reader);
dictionary.Add(key, value);
reader.Read();
}
return new DataCollection<string, object>(dictionary);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then you can use the JsonConverter attribute to register the custom deserializer in your class:
[JsonConverter(typeof(DataCollectionConverter))]
public DataCollection<string, object> Data { get; set; }
You can also do the same with System.Text.Json using a JsonConverter and registering it using JsonSerializerOptions.Converters collection
class DataCollectionConverter : JsonConverter<DataCollection<string, object>>
{
public override DataCollection<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var dictionary = new Dictionary<string, object>();
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
break;
}
reader.Read();
var key = reader.GetString();
reader.Read();
var value = JsonSerializer.Deserialize(ref reader, options);
dictionary.Add(key, value);
}
André Arnaud de Cal...
294,161
Super User 2025 Season 1
Martin Dráb
232,942
Most Valuable Professional
nmaenpaa
101,158
Moderator