Deserializing complex Json objects - c#

I want to deserialize a complex and let's say not well constructed json. That code that I wrote doesn't deserialize the object, the MovieInfo property is null. You can find the example json in the code. I want to avoid using JObject.Parse and dynamic objects. What am I missing here?
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ComplexJsonExample
{
class Program
{
static void Main(string[] args)
{
string jsonInText = #"
{
""movies"" : [
{
""Harry Potter"" : [
{ ""rating"": ""great""},
{ ""rating"": ""horrible""}
]
},
{
""Guardians of the galaxy"" : [
{ ""rating"": ""cool""},
{ ""rating"": ""awesome""}
]
}
]
}
";
var movieList = JsonConvert.DeserializeObject<MovieList>(jsonInText);
}
}
public class MovieList
{
[JsonProperty("movies")]
public IList<Movie> Movies { get; set; }
}
public class Movie
{
IDictionary<string, IList<MovieRating>> MovieInfo { get; set; }
}
public class MovieRating
{
[JsonProperty("rating")]
public string Rating { get; set; }
}
}

It's a bit ugly, but you can do it like this:
class MovieList
{
[JsonProperty("movies")]
public Movie[] Movies { get; set; }
}
class Movie : Dictionary<string, MovieRating[]>
{
}
class MovieRating
{
[JsonProperty("rating")]
public string Rating { get; set; }
}
It's weird that the movie is a dictionary with only one key (its title), but it matches the JSON structure. You can always map it to something more sensible after deserialization.

I consider this a bit of a hack, but it works, and deserializes your json string into the format you required:
class MovieConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Movie);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var movie = new Movie(){
MovieInfo = new Dictionary<string,IList<MovieRating>>()
};
while (reader.Read() && reader.Value != null)
{
var name = (string)reader.Value;
reader.Read();
var ratings = ((JArray)serializer.Deserialize(reader)).Values<string>("rating");
movie.MovieInfo.Add(name, ratings.Select(r => new MovieRating(){ Rating = r}).ToList());
}
return movie;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
I did have to make two changes to your original objects, first I made the accessor for MovieInfo public so I could access it, and second to add the JsonConverterAttribute:
[JsonConverter(typeof(MovieConverter))]
public class Movie
{
public IDictionary<string, IList<MovieRating>> MovieInfo { get; set; }
}

Related

How can I deserialize a JSON to a .NET object with several List<T> parameters?

I'm using this library (FireSharp) to access Firebase. I've retrieved a json as the body of a FirebaseResponse (status and body basically), which works good and looks like this (all test data):
"{\"Cobros\":{\"1001\":{\"Estado\":\"Cobrado\",\"Repartidor\":124},\"1112\":{\"Estado\":\"Pendiente\",\"Repartidor\":124}},\"Pedidos\":{\"1111\":{\"Estado\":\"Entregado\",\"Repartidor\":123}},\"Repartidores\":{\"123\":{\"ID Android\":\"asdadada\",\"Terminal\":123},\"124\":{\"ID Android\":\"dggrefawe\",\"Terminal\":124}}}"
The Firebase struct looks like this, leafs with some params behind that.
I need to deserialize the Json to embed it into a Object with several List params, one for each node behind 18500. I've checked this question and this is my code so far:
public class cargaFirebase
{
[JsonProperty("Cobros")]
public List<documentoFirebase> carfb_cobros { get; set; }
[JsonProperty("Pedidos")]
public List<documentoFirebase> carfb_pedidos { get; set; }
[JsonProperty("Repartidores")]
public List<repartidorFirebase> carfb_repartidores { get; set; }
}
And the deserialization try...
FirebaseResponse resp = fbClient.Get(request);
cargaFirebase carga_res = JsonConvert.DeserializeObject<cargaFirebase>(resp.Body);
This gives me a deesrialization error I can't understand, not really into jsons sorry, and thx in advanced!
{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Reparto_Android.Code.Helpers.FirebaseHelper+documentoFirebase]' 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) 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
this code works properly
cargaFirebase carga_res = JsonConvert.DeserializeObject<CargaFirebase>(resp.Body);
if you use these classes
public partial class CargaFirebase
{
[JsonProperty("Cobros")]
public Dictionary<string, Cobro> Cobros { get; set; }
[JsonProperty("Pedidos")]
public Pedidos Pedidos { get; set; }
[JsonProperty("Repartidores")]
public Dictionary<string, Repartidore> Repartidores { get; set; }
}
public partial class Cobro
{
[JsonProperty("Estado")]
public string Estado { get; set; }
[JsonProperty("Repartidor")]
public long Repartidor { get; set; }
}
public partial class Pedidos
{
[JsonProperty("1111")]
public Cobro The1111 { get; set; }
}
public partial class Repartidore
{
[JsonProperty("ID Android")]
public string IdAndroid { get; set; }
[JsonProperty("Terminal")]
public long Terminal { get; set; }
}
output
{
"Cobros": {
"1001": {
"Estado": "Cobrado",
"Repartidor": 124
},
"1112": {
"Estado": "Pendiente",
"Repartidor": 124
}
},
"Pedidos": {
"1111": {
"Estado": "Entregado",
"Repartidor": 123
}
},
"Repartidores": {
"123": {
"ID Android": "asdadada",
"Terminal": 123
},
"124": {
"ID Android": "dggrefawe",
"Terminal": 124
}
}
}
If all those leaf names, such as "1111", "1001", "1112", are dynamically generated and you want to ignore them, maybe you can try some customized json converters like below:
public class CargaFirebase
{
[JsonConverter(typeof(DocumentoFirebaseConverter))]
public List<DocumentoFirebase> Cobros { get; set;}
[JsonConverter(typeof(DocumentoFirebaseConverter))]
public List<DocumentoFirebase> Pedidos { get; set;}
[JsonConverter(typeof(RepartidorFirebaseConverter))]
public List<RepartidorFirebase> Repartidores { get; set; }
}
public class DocumentoFirebase
{
public string Estado { get; set; }
public int Repartidor { get; set; }
}
public class RepartidorFirebase
{
[JsonProperty(PropertyName = "Id Android")]
public string Id_Android { get; set; }
public int Terminal { get; set; }
}
class DocumentoFirebaseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<DocumentoFirebase>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObj = JObject.Load(reader);
List<DocumentoFirebase> docs = new List<DocumentoFirebase>();
foreach (JProperty prop in jObj.Properties())
{
var doc = prop.Value.ToObject<DocumentoFirebase>();
docs.Add(doc);
}
return docs;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
class RepartidorFirebaseConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<RepartidorFirebase>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jObj = JObject.Load(reader);
List<RepartidorFirebase> repartidors = new List<RepartidorFirebase>();
foreach (JProperty prop in jObj.Properties())
{
var repartidor = prop.Value.ToObject<RepartidorFirebase>();
repartidors.Add(repartidor);
}
return repartidors;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Then you can try to parse the raw json as what you did before:
cargaFirebase carga_res = JsonConvert.DeserializeObject<CargaFirebase>(resp.Body);

How to discard JSON elements with specific values while deserializing

This is an interesting situation. My application saves and loads JSON data from MongoDB, which works fine 95% of the time. The JSON data in such cases is like the following:
{
"isDemo": true,
"CustomerReference": "nabTest",
"Fee": null,
"OrderId": "48/XYZ3",
"Asynchronous": false,
}
The remaining 5% of the time,a legacy application loads some XML data, converts that into JSON format and inserts that into the same MongoDB collection. The JSON data in such cases is like this:
{
"#xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance",
"#xmlns:xsd":"http://www.w3.org/2001/XMLSchema",
"#xmlns":"http://MyDomain.com.au/SomeService.Contract/2007/06/18",
"isDemo":{
"#xmlns":"http://MyDomain.com.au/ABC.Services",
"#text":"true"
},
"Asynchronous":{
"#xmlns":"http://MyDomain.com.au/ABC.Services"
"#text":"false"
},
"Fee":{
"#xsi:nil":"true",
"#xmlns":"http://MyDomain.com.au/ABC.Services"
},
"CustomerReference":{
"#xmlns":"http://MyDomain.com.au/ABC.Services"
},
"OrderId":"48/XYZ3"
}
In such cases, Newtonsoft deserializer crashes with the following exception:
Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered
while parsing value: {. Path 'CustomerReference', line 1, position
919.'
The minimum code for this is the following:
// 'result' is of type object, loaded from MongoDB
var resultStr = JsonConvert.SerializeObject(result);
var obj = JsonConvert.DeserializeObject<T>(resultStr, jsonSerializerSettings);
What is an elegant way to handle this situation, with minimal code changes.
Your Object representation for the normal Json should be something like this :
using J = Newtonsoft.Json.JsonPropertyAttribute;
public partial class Data
{
[J("isDemo")] public bool? IsDemo { get; set; }
[J("Asynchronous")] public bool? IsAsynchronous { get; set; }
[J("OrderId")] public string OrderId { get; set; }
[J("CustomerReference")] public string CustomerReference { get; set; }
[J("Fee")] public double? Fee { get; set; }
}
While having a custom JsonConverter handeling the whole Xml representation will be nice. I find it difficult to write and maintain.
You should divide and conquer:
Generate a class that is the representation of the Xml-ish Json.
Remove unnecessary property, keep Xml "nil" where needed
Create simple custom converter for mapping string representation to the correct type.
eg: "true" => bool, "123" => int,
public partial class XmlRepresentation
{
[J("isDemo")] public BoolWrapper IsDemo { get; set; }
[J("Asynchronous")] public BoolWrapper Asynchronous { get; set; }
[J("Fee")] public DoubleWrapper Fee { get; set; }
[J("CustomerReference")] public StringWrapper CustomerReference { get; set; }
[J("OrderId")] public string OrderId { get; set; }
}
public partial class NullableXmlValue
{
[J("#xsi:nil")][JsonConverter(typeof(BoolParseStringConverter))]
public bool IsNull { get; set; }
}
public partial class BoolWrapper :NullableXmlValue
{
[J("#text")][JsonConverter(typeof(BoolParseStringConverter))]
public bool Value { get; set; }
}
public partial class StringWrapper :NullableXmlValue
{
[J("#text")] public string Value { get; set; }
}
public partial class DoubleWrapper :NullableXmlValue
{
[J("#text")][JsonConverter(typeof(DoubleParseStringConverter))]
public double Value { get; set; }
}
With some simple String to Type {Bool, Double} parser:
internal class BoolParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(bool) || t == typeof(bool?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
if (Boolean.TryParse(value, out bool b))
{
return b;
}
throw new Exception("Cannot unmarshal type bool");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (bool)untypedValue;
var boolString = value ? "true" : "false";
serializer.Serialize(writer, boolString);
return;
}
public static readonly BoolParseStringConverter Singleton = new BoolParseStringConverter();
}
internal class DoubleParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(double) || t == typeof(double?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
if (Double.TryParse(value, out double l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (double)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly DoubleParseStringConverter Singleton = new DoubleParseStringConverter();
}
This way you can directly deserialize to XmlRepresentation and use a simple projection logic to get the Data.
public partial class XmlRepresentation
{
public Data ToDataType(){
var result = new Data();
if( !this.IsDemo?.IsNull ?? false)
result.IsDemo = this.IsDemo.Value;
// etc..
return result;
}
}
And for knowing if it's the normal Json or the Xml-ish one I will simply use a try catch.
That way I won't have to write complexe JsonConverter or class that represent both Normal and Xml-ish.
Please use like this in Model:
public class User
{
// always require a string value
[JsonProperty("name", Required = Required.Always)]
public string Name { get; set; }
// don't require any value
[JsonProperty("role", NullValueHandling = NullValueHandling.Ignore)]
public string Role { get; set; }
// property is ignored
[JsonIgnore]
public string Password { get; set; }
}
Result generated with Json.NET serialization attributes:
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"role": {
"type": [
"string",
"null"
]
}
},
"required": [
"name"
]
}

Deserialize property to different class based on other property c#

I know there are many similar questions on SO, however all the ones I've found require a shared base class in order to work.
With a stream of JSON data like this:
[
{
"webhookType": "order",
"data": {
"id": "eeiefj393",
"orderProperty": "Value"
}
},
{
"webhookType": "customer",
"data": {
"id": 29238,
"customerProperty": "Value"
}
}
]
I wish to deserialize this into two containers, List<Customer> and List<Order>. Where the two classes are as follows:
class Order
{
public string Id { get; set; }
public string OrderProperty { get; set; }
[...]
}
class Customer
{
public long Id { get; set; }
public string CustomerProperty { get; set; }
[...]
}
There may be shared property names however there are no shared properties + type between these two classes and so the solutions involing a sub class aren't working for me.
You need to create a JsonConverter.
DataConverter
public class WebHookConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject)
{
JObject item = JObject.Load(reader);
if (item["webhookType"].Value<string>() == "order")
{
var webhook = new WebHook
{
Type = item["webhookType"].Value<string>(),
Data = item["data"].ToObject<Order>()
};
return webhook;
}
else if (item["webhookType"].Value<string>() == "customer")
{
var webhook = new WebHook
{
Type = item["webhookType"].Value<string>(),
Data = item["data"].ToObject<Customer>()
};
return webhook;
}
}
return null;
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
Objects
[JsonConverter(typeof(WebHookConverter))]
public class WebHook
{
[JsonProperty("webhookType")]
public string Type { get; set; }
public object Data { get; set; }
}
public class Order
{
public string Id { get; set; }
[JsonProperty("orderProperty")]
public string Property { get; set; }
}
public class Customer
{
public long Id { get; set; }
[JsonProperty("customerProperty")]
public string Property { get; set; }
}
Serialization
var json = File.ReadAllText("json1.json");
var obj = JsonConvert.DeserializeObject<List<WebHook>>(json);
var orderList = obj.Where(o => o.Type == "order").Select(o => o.Data).ToList();
var customerList = obj.Where(o => o.Type == "customer").Select(o => o.Data).ToList();
Output:

Newtonsoft deserialize multiple JSON files with cross references

I have two JSON files (that I can't change the format for) in the following format:
Main file -
[
{
"Name":"XYZ",
"UnitReferenceId":1
},
{
"Name":"ABC",
"UnitReferenceId":2
}
]
The lookup/reference JSON file -
[
{
"UnitReferenceId":1,
"Units":[
{
"Unit":"mg",
"Scale":1
},
{
"Unit":"gm",
"Scale":1000
},
{
"Unit":"kg",
"Scale":1000000
}
]
},
{
"UnitReferenceId":2,
"Units":[
{
"Unit":"mm",
"Scale":1
},
{
"Unit":"m",
"Scale":1000
},
{
"Unit":"km",
"Scale":1000000
}
]
}
]
How would I go about deserializing that into C# classes using Newtonsoft JSON into something like:
public class Widget
{
public string Name {get; set;}
public UnitReference UnitReference { get; set; }
}
public class UnitReference
{
public long UnitReferenceId { get; set; }
public List<Unit> Units { get; set; }
}
public class Unit
{
[JsonProperty("Unit")]
public string UnitValue { get; set; }
public long Scale { get; set; }
}
Any help would be greatly appreciated!
You can do this by reading your two JSON files as follows:
First read the lookup/reference JSON file for UnitReference as a List<UnitReference>, then convert to a Dictionary<long, UnitReference> lookup table.
Next, read the main file using a custom JsonConverter for Widget that is passed the Dictionary<long, UnitReference> lookup table and can translate between UnitReferenceId and UnitReference during reading and writing.
Thus your classes would look like the following:
public class UnitReference
{
readonly long unitReferenceId;
public UnitReference(long unitReferenceId)
{
this.unitReferenceId = unitReferenceId;
}
public long UnitReferenceId { get { return unitReferenceId; } }
public List<Unit> Units { get; set; }
}
public class Unit
{
[JsonProperty("Unit")]
public string UnitValue { get; set; }
public long Scale { get; set; }
}
public class Widget
{
public string Name { get; set; }
public UnitReference UnitReference { get; set; }
}
(My only modification was to make UnitReferenceId be read-only so that it safely could be used as a dictionary key.)
Then, define the following converter:
public class WidgetConverter : CustomPropertyConverterBase<Widget>
{
readonly IDictionary<long, UnitReference> units;
public WidgetConverter(IDictionary<long, UnitReference> units)
{
this.units = units;
}
protected override void ReadCustomProperties(JObject obj, Widget value, JsonSerializer serializer)
{
var id = (long?)obj.GetValue("UnitReferenceId", StringComparison.OrdinalIgnoreCase);
if (id != null)
value.UnitReference = units[id.Value];
}
protected override bool ShouldSerialize(JsonProperty property, object value)
{
if (property.UnderlyingName == nameof(Widget.UnitReference))
return false;
return base.ShouldSerialize(property, value);
}
protected override void WriteCustomProperties(JsonWriter writer, Widget value, JsonSerializer serializer)
{
if (value.UnitReference != null)
{
writer.WritePropertyName("UnitReferenceId");
writer.WriteValue(value.UnitReference.UnitReferenceId);
}
}
}
public abstract class CustomPropertyConverterBase<T> : JsonConverter where T : class
{
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var jObj = JObject.Load(reader);
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(objectType);
var value = existingValue as T ?? (T)contract.DefaultCreator();
ReadCustomProperties(jObj, value, serializer);
// Populate the remaining properties.
using (var subReader = jObj.CreateReader())
{
serializer.Populate(subReader, value);
}
return value;
}
protected abstract void ReadCustomProperties(JObject obj, T value, JsonSerializer serializer);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
writer.WriteStartObject();
foreach (var property in contract.Properties.Where(p => ShouldSerialize(p, value)))
{
var propertyValue = property.ValueProvider.GetValue(value);
if (propertyValue == null && serializer.NullValueHandling == NullValueHandling.Ignore)
continue;
writer.WritePropertyName(property.PropertyName);
serializer.Serialize(writer, propertyValue);
}
WriteCustomProperties(writer, (T)value, serializer);
writer.WriteEndObject();
}
protected virtual bool ShouldSerialize(JsonProperty property, object value)
{
return property.Readable && !property.Ignored && (property.ShouldSerialize == null || property.ShouldSerialize(value));
}
protected abstract void WriteCustomProperties(JsonWriter writer, T value, JsonSerializer serializer);
}
And deserialize as follows:
var units = JsonConvert.DeserializeObject<List<UnitReference>>(unitsJsonString)
.ToDictionary(u => u.UnitReferenceId);
var settings = new JsonSerializerSettings
{
Converters = { new WidgetConverter(units) },
};
var widgets = JsonConvert.DeserializeObject<List<Widget>>(widgetsJsonString, settings);
Notes:
Here I am deserializing from JSON strings for demo purposes, but you can deserialize directly from your file(s) as shown in Deserialize JSON from a file.
The base class CustomPropertyConverterBase<T> for WidgetConverter automatically reads and writes all properties for the object being (de)serialized. WidgetConverter then overrides this behavior for the UnitReference property only, avoiding the necessity to manually serialize all the remaining properties of Widget.
Sample fiddle.
I use json2csharp for creating my classes quickly. If you have to implement it in code, see JSON C# Class Generator Project.

Parse dynamic JSON

I am using c# and json.net 9.0.1
I have the following json
{
"lineups": [
{
"55": {
"id": "55",
"game_id": "1",
"player_id": "55",
"jersey_number": "78"
},
"56": {
"id": "56",
"game_id": "1",
"player_id": "56",
"jersey_number": "77"
},
"57": {
"id": "57",
"game_id": "1",
"player_id": "57",
"jersey_number": "76"
}
}
]
}
All of the array items are of type Player. How can I parse the json so that each item "55", "56", "57" are stored in a list of Players, List?
The source json can't be modified as it is coming from a 3rd party.
UPDATE
Modified the json to be valid,
If you created two classes like this:
public class Lineup
{
public List<Dictionary<string,Player>> lineups;
}
public class Player
{
public string id {get; set; }
public string game_id { get; set;}
public string player_id {get;set;}
public string jersey_number {get;set;}
}
Then you should (after you've fixed your invalid JSON), be able to deserialize like this:
var l = Newtonsoft.Json.JsonConvert.DeserializeObject<Lineup>(source);
Example: https://dotnetfiddle.net/e7eUUZ
You can also use the various attributes in JSON.Net to customize the deserialization. For example, you might want to use the JsonPropertyAttribute to map the names in your JSON source to C# property names that match the standard.
Given these classes:
public class Lineup
{
public List<Player> Players { get; set; }
}
public class Player
{
public string id { get; set; }
public string game_id { get; set; }
public string player_id { get; set; }
public string jersey_number { get; set; }
}
public class Result
{
public List<Lineup> Lineups { get; set; }
}
You could implement a custom JsonConverter like this:
public class LineupConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Lineup);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
if (reader.TokenType != JsonToken.StartObject)
throw new Exception("Expected an object");
var jObject = (JObject)JObject.ReadFrom(reader);
// I suspect the property name are the same as the id property
// of the contained objects. Discarding the information from serialization
var players = jObject.Properties()
.Select(p => p.Value.ToObject<Player>());
return new Lineup
{
Players = players.ToList()
};
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then use:
var lineups = JsonConvert.DeserializeObject<Result>(json, new LineupConverter());
You could also annotate the Player properties with JsonProperty attributes and have them more C#-like.

Categories