"Instances of abstract classes cannot be created" exception for classes with JObject properties
See original GitHub issueHi, 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:
- Created 7 years ago
- Comments:5 (2 by maintainers)

Top Related StackOverflow Question
I also have this issue. Problem is
JObjectcontains internal setter likeParent,Next,Previous(which Mapster will translate type as class rather than primitive). Then, Mapster try to perform deep copy, butParent,Next,PreviousareJTokenwhich is abstract. Currently, I solve this problem by usingMapWithto perform direct copy.I just added a couple of lines for MapWith in doc.