I am trying to upgrade a project from 1.3.1 to 1.74 that uses a customer converter. Everything works OK in 1.3.1 but fails with 1.74
with the error below. Am I adding the custom converter to the MyJsonApiSerializerSettings in the correct way?
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[PUBGChampionApi.PubgPlayer]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 1, position 8.
I've added a MyJsonApiSerializerSettings class and into the method to try and get it to call the custom converter
public class MyJsonApiSerializerSettings : JsonSerializerSettings
{
public MyJsonApiSerializerSettings(JsonConverter resourceObjectConverter)
{
base.Converters.Add(new RelationshipIdConverter());
}
public MyJsonApiSerializerSettings()
: this(new ResourceObjectConverter())
{
}
}
var settings = new MyJsonApiSerializerSettings()
var converted = JsonConvert.DeserializeObject<IEnumerable<PubgPlayer>>(collectionJson, settings);
public class RelationshipIdConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => false;
public override bool CanWrite => false;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//if the reader is not reading a relationship object just deserialize as normal.
//This allows us to serialize and deserialize multiple times after converting from the Json-API format
if (reader.TokenType != JsonToken.StartObject)
return serializer.Deserialize(reader, objectType);
JToken jt = JToken.Load(reader);
var dataToken = jt.SelectToken("data");
if (objectType == typeof(string))
return dataToken["id"].ToString();
return dataToken.Select(x => (string)x["id"]).ToList();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }
}
The class I'm trying to deserialize into
public class PubgPlayer : PubgShardedEntity
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty]
public DateTime CreatedAt { get; set; }
[JsonProperty]
public string PatchVersion { get; set; }
[JsonProperty]
public string TitleId { get; set; }
[JsonProperty("matches")]
[JsonConverter(typeof(RelationshipIdConverter))]
public IEnumerable<string> MatchIds { get; set; }
}
I am trying to upgrade a project from 1.3.1 to 1.74 that uses a customer converter. Everything works OK in 1.3.1 but fails with 1.74 with the error below. Am I adding the custom converter to the MyJsonApiSerializerSettings in the correct way?
I've added a MyJsonApiSerializerSettings class and into the method to try and get it to call the custom converter
I think it's similar to this question. [JsonConverter] seems to be ignored as of 1.6.0
This is the converter class
The class I'm trying to deserialize into
Json from API