web
You’re offline. This is a read only version of the page.
close
Skip to main content
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
    RE: Deserialize and Entity in Json throwing error for Attributes as its now Abstract

    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
    RE: Deserialize and Entity in Json throwing error for Attributes as its now Abstract

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 127

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 127

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 125 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans