question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Instances of abstract classes cannot be created" exception for classes with JObject properties

See original GitHub issue

Hi, I’m getting a similar exception “Instances of abstract classes cannot be created.” as in #52, except that I don’t have nested collection of abstract types. Instead, I have DTOs with JObject prop (and JObject has a property of JToken type and JToken is abstract). Protected ctors and abstract classes are fine, but JObject breaks the things.

Interesting, that even if JObject is the type on both source and input DTOs (so technically it should not be mapped, just deep copied? ), mapster tries to go deeper and map all subtypes. Am I thinking about Mapster expected behaviour wrong ?

Is there any configuration setting to tell mapster not to create deep mapping substructure ? I tried to play with ConstructUsing for JObject to avoid defining it for every DTO, but that doesn’t help

Here is the repro script for LINQPad (Latest Mapster 2.4.3 and latest Json.NET 9.0.1 are referenced)

void Main()
{
    string jsonText = @"
    {
      ""AA"": {
        ""Zorder"": ""9999"",
        ""PageNumber"": 1,
        ""Id"": ""f494dba7-372c-458f-942e-ecab7c938fa7"",
        ""Created"": ""2016-05-27T16:36:18.4172629+10:00"",
        ""Type"": 18,
        ""Content"": """",
        ""Geometry"": """",
        ""BlendMode"": 1
      },
      ""Id"": ""c0000000-0000-0000-0000-000000000000"",
      ""TraceId"": ""bbbbbbbb-bbbb-bb11-1111-111111111111""
    }";

    var jObjData = JObject.Parse(jsonText);

    var input = new MainSrc
    {
        BaseId = 11,
        SrcId = 1123,
        LastItem = jObjData,
    };
    //var res = jObjData.DeepClone();
    //res.GetType().Dump();
    //input.Dump();

    WebDTOMapSetup.Verify();
    var output = input.Adapt<MainSrc, MainDest>();
    output.Dump();
}


public class WebDTOMapSetup
{
    static WebDTOMapSetup()
    {
        TypeAdapterConfig<MainSrc, MainDest>.ForType();
        //TypeAdapterConfig<JObject, JObject>.ForType().ConstructUsing(f=>(JObject)f.DeepClone() as JObject)
        ;
    }
    public static void Verify()     { TypeAdapterConfig.GlobalSettings.Compile();}
}

abstract class SrcBase
{
    protected SrcBase() { }
    protected SrcBase(int baseid) { BaseId = baseid; }
    public int BaseId { get; set; }
}

abstract class DestBase
{
    protected DestBase() { }
    protected DestBase(int baseid) { BaseId = baseid; }
    public int BaseId { get; set; }
}

class MainSrc : SrcBase
{
    public int SrcId { get; set; }
    public JObject LastItem { get; set; }
}

class MainDest : DestBase
{
    public int DestId { get; set; }
    public JObject LastItem { get; set; }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
chaowlertcommented, Aug 2, 2016

I also have this issue. Problem is JObject contains internal setter like Parent, Next, Previous (which Mapster will translate type as class rather than primitive). Then, Mapster try to perform deep copy, but Parent, Next, Previous are JToken which is abstract. Currently, I solve this problem by using MapWith to perform direct copy.

TypeAdapterConfig<JObject, JObject>.NewConfig().MapWith(src => src);
TypeAdapterConfig<JArray, JArray>.NewConfig().MapWith(src => src);
TypeAdapterConfig<JValue, JValue>.NewConfig().MapWith(src => src);
TypeAdapterConfig<JToken, JToken>.NewConfig().MapWith(src => src);
0reactions
chaowlertcommented, Aug 8, 2016

I just added a couple of lines for MapWith in doc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Protobuf-net: Instances of abstract classes cannot be created
I get the error "InvalidOperationException: Instances of abstract classes cannot be created." My StackTrace is as follows: at ctorWrapper() at ...
Read more >
Can we create an object of an abstract class in Java
No, we can't create an object of an abstract class. But we can create a reference variable of an abstract class. The reference...
Read more >
Abstract Class in Java
An abstract class in Java is one that is declared with the abstract keyword. It may have both abstract and non-abstract methods(methods with ......
Read more >
Abstract Classes and Abstract Methods in C# - ...
No. We cannot create an instance of an abstract class. Whether the abstract class contains any abstract methods or not, it is not...
Read more >
Interfaces and Abstract Classes - C# in Simple Terms
An abstract class is defined using the abstract keyword. This keyword tells us that the object being modified has a missing or incomplete ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found