Skip to main content

Notifications

Announcements

No record found.

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

  • Eris Adhami Profile Picture
    Eris Adhami 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

  • Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 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);
    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,468 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,287 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans