web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Deserialize and Entity in Json throwing error for Attributes as its now Abstract

(0) ShareShare
ReportReport
Posted on by 15

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

I have the same question (0)
  • Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    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);
    }

  • Eris Adhami Profile Picture
    5 on at

    You can use this excellent library called Xrm-Entity-Serializer github.com/.../Xrm-Entity-Serializer

    Here is an implementation example:

    gist.github.com/.../832924babb4dc8355b730c43cb9ec61a

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 165

#2
ManoVerse Profile Picture

ManoVerse 161 Super User 2026 Season 1

#3
Zhilan Profile Picture

Zhilan 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans