[英]Newtonsoft.Json.JsonSerializationException : Could not create an instance of type (abstract class)
I am trying to test Serialisation for the below example but i ended up getting this error Newtonsoft.Json.JsonSerializationException : Could not create an instance of type Core.Model.Assets.Asset. Type is an interface or abstract class and cannot be instantiated. Path '_source.articleAssets[0].asset.refId'
我试图测试下面的示例序列化,但我最终得到此错误Newtonsoft.Json.JsonSerializationException : Could not create an instance of type Core.Model.Assets.Asset. Type is an interface or abstract class and cannot be instantiated. Path '_source.articleAssets[0].asset.refId'
Newtonsoft.Json.JsonSerializationException : Could not create an instance of type Core.Model.Assets.Asset. Type is an interface or abstract class and cannot be instantiated. Path '_source.articleAssets[0].asset.refId'
Newtonsoft.Json.JsonSerializationException : Could not create an instance of type Core.Model.Assets.Asset. Type is an interface or abstract class and cannot be instantiated. Path '_source.articleAssets[0].asset.refId'
. Newtonsoft.Json.JsonSerializationException : Could not create an instance of type Core.Model.Assets.Asset. Type is an interface or abstract class and cannot be instantiated. Path '_source.articleAssets[0].asset.refId'
。 In below example, Asset
is a abstract class and there are more than 5 derived classes ( eg: BrightcoveVideo
is one of the derived class) 在下面的示例中, Asset
是一个抽象类,并且有超过5个派生类(例如: BrightcoveVideo
是派生类之一)
ArticleAssets = new List<ArticleAsset>()
{
new ArticleAsset()
{
ArticleId = 1,
Asset = new BrightcoveVideo()
{
AssetType = AssetTypeEnum.BrightcoveTitle, Id = 11, Name = "something for a name", DisplayName = "some display", RefId = "refrefref"
},
AssetId = 11,
AssetType = AssetTypeEnum.BrightcoveTitle
}
}
I have a custom JsonConverter class which looks like this : 我有一个自定义的JsonConverter类,如下所示:
public class AssetTypeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Asset) || objectType == typeof(SearchResultAsset);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value != null && value.GetType() == typeof(SearchResultAsset))
{
throw new NotImplementedException("WriteJson unexpectedly called for SearchResultAsset in AssetTypeConverter");
}
throw new NotImplementedException("WriteJson unexpectedly called for Asset in AssetTypeConverter");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}
Asset concreteAsset = new SearchResultAsset();
serializer.Populate(reader, concreteAsset);
return concreteAsset;
}
}
In ElasticSearchRegistry class,i am adding this converter in the following way : 在ElasticSearchRegistry类中,我将按以下方式添加此转换器:
var connectionSettings = new ConnectionSettings(connectionUri);
_elasticClient = new ElasticClient(connectionSettings);
connectionSettings.SetJsonSerializerSettingsModifier(p => p.Converters.Add(new AssetTypeConverter()));
I get that JsonSerialization exception at following Get
request : 我在以下Get
请求中得到了JsonSerialization异常:
public IGetResponse<Article> GetArticleResponse(int id)
{
var response = _elasticClient.Get<Article>(i => i.Index(_indexName)
.Type(DocumentType)
.Id(id)
);
return response;
}
I got it fixed, It was very small mistake. 我把它修好了,这是一个非常小的错误。 I have set JsonSerializerSettings
before instantiating ElasticClient
like this : 我在实例化ElasticClient
之前设置了JsonSerializerSettings
,如下所示:
connectionSettings = new ConnectionSettings(connectionUri);
connectionSettings.SetJsonSerializerSettingsModifier(
p => p.Converters.Add(new AssetTypeConverter())
);
_elasticClient = new ElasticClient(connectionSettings);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.