How to represent and serialize a data structure which requires duplicate keys? - c#

I'm working with a third-party API where you send an order along with an array of options you want to add to the order. Each Option has an OptionId.
You'd assume the required JSON would look something like this:
{
//More properties cut for brevity
"options":[
{
"optionId":"ID 1"
},
{
"optionId":"ID 2"
}
]
}
Instead, the required JSON actually looks like this:
{
//More properties cut for brevity
"options":[
{
"optionId":"ID 1",
"optionId":"ID 2"
}
]
}
Is there any way I can represent that data structure in C#? And if yes, is there any way I can tell Json.NET to serialize it the way the API requires?

The desired JSON is technically valid, though not recommended because having duplicate keys in an object makes it much more difficult to work with. I understand this is a third party API, and you don't have any other choice, but I want to make clear to future readers that this is a bad design.
To represent the option IDs in C#, you can use a simple List<string>. You will need to make a custom JsonConverter to write out the desired structure to the JSON. We'll call it OptionListConverter. So declare the option list as shown below:
class Order
{
[JsonProperty("options")]
[JsonConverter(typeof(OptionListConverter))]
public List<string> OptionIds { get; set; }
}
Then define the OptionListConverter like this:
class OptionListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<string>);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var list = (List<string>)value;
writer.WriteStartArray();
writer.WriteStartObject();
foreach (var id in list)
{
writer.WritePropertyName("optionId");
writer.WriteValue(id);
}
writer.WriteEndObject();
writer.WriteEndArray();
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You can then create the desired JSON like this:
var order = new Order
{
OptionIds = new List<string> { "ID 1", "ID 2" }
};
var json = JsonConvert.SerializeObject(order, Formatting.Indented);
Working demo: https://dotnetfiddle.net/rEYl8p

Related

JSON.Net - How to deserialize a nested field of a custom type

I've been having trouble finding a question about my particular case:
I need to deserialize the following JSON:
{
"name": "My Farm",
"barns": [
{
"name"": "Barn A"",
"animalTypes": [
"Cow",
"Goat"
]
}
]
}
to the following code model:
public class Farm
{
public string name;
public Barn[] barns;
}
public class Barn
{
public string name;
public AnimalType[] animalTypes;
}
public class AnimalType
{
public int typeID;
}
The problem: I need to deserialize a JSON string (which described an animal species name) into an 'AnimalType' object which contains a "type ID" int. This requirement is not something I can change.
To get the animal's integer type ID, I have access to an externally supplied "AnimalTypeResolver" class, which looks like this:
public class AnimalTypeResolver
{
public int GetAnimalType(string animalTypeName)
{
// queries a map to return the right ID.
}
}
I can query that resolver to get the int value I need to store for each animal type.
So, I tried to write a custom JSONConverter for AnimalType:
public class AnimalTypeConverter : JsonConverter<AnimalType>
{
public AnimalTypeResolver animalTypeResolver;
public AnimalTypeConverter(AnimalTypeResolver animalTypeResolver)
{
this.animalTypeResolver = animalTypeResolver;
}
public override bool CanWrite => false;
public override AnimalType ReadJson(ref JsonReader reader, Type objectType, AnimalType existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.String)
{
string animalTypeName = (string)reader.Value;
return new AnimalType
{
typeID = animalTypeResolver.GetAnimalType(animalTypeName)
};
}
return null;
}
public override void WriteJson(JsonWriter writer, AnimalType value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And my deserialization code looks like this:
string farmJSON =
#"{
""name"": ""My Farm"",
""barns"": [
{
""name"": ""Barn A"",
""animalTypes"": [
""Cow"",
""Goat""
]
}
]
}";
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new AnimalTypeConverter(animalTypeResolver)); // this animalTypeResolver is supplied from elsewhere in code.
Farm farm = JsonConvert.DeserializeObject<Farm>(farmJSON);
But I get a runtime error:
ArgumentException: Could not cast or convert from System.String to AnimalType.
From reading similar questions, I believe my problem is that I'm trying to deserialize a nested field of a custom type (AnimalType[]). Other answers have explained that JToken.FromObject() creates a new JsonSerializer for each level of deserialization, which has no concept of the JsonConverters I added to a higher-level serializer.
However, For one reason or another, the other questions on this site have answers which aren't exactly applicable to my case.
How I can use my custom JsonConverter, to handle a case in which the data is deeply nested?
If anyone can offer advice about how to make this work, thank you!
JsonConvert.DeserializeObject<Farm>(farmJSON) does not use your JsonSerializer instance. I mean, how could it possibly access the serializer variable that you created and assigned in your code?
You have two choices: Either use the Deserialize method of the serializer instance you did just setup to deserialize your json data and not use JsonConvert.DeserializeObject .
Or instead of setting up a serializer, define some JsonSerializerSettings with your custom JsonConverter and pass those settings to the JsonConvert.DeserializeObject method. Alternatively you could instead also define default serialization settings for JsonConvert.DeserializeObject as demonstrated here in the Newtonsoft.Json documentation: https://www.newtonsoft.com/json/help/html/DefaultSettings.htm

Newtonsoft.Json.JsonConvert.DeserializeObject into Newtonsoft.Json.Linq.JObject issues

I am working on a C# project.
[
{"FirstName":"XYZ","LastName":"SSS"},
{"FirstName":"ABC","LastName":"NNN"}
]
Each row represents an object of class ChildDTO.
I read the above data from the file and am trying to deserialize into a ParentCollection object like below:
string file = System.IO.File.ReadAllText("Children_CA.txt");
ParentCollection pCollection = Newtonsoft.Json.JsonConvert.DeserializeObject<ParentCollection>(file);
My DTO classes are like below:
[Serializable]
public class ParentCollection : CollectionBase
{
public void Add(ChildDTO dto)
{
//List is from Systems.Collections.CollectionBase class
List.Add(dto);
}
}
[Serializable]
public class ChildDTO
{
// properties like FirstName and LastName goes here
}
I cannot change my DTO classes since they are old and already in production from the past 20 years and many applications are using them.
When I see Quick Watch on pCollection, I notice the collection is having objects of type Newtonsoft.Json.Linq.JObject. I am hoping to have objects of type ChildDTO
Please let me know what mistake I am doing.
The problem is that CollectionBase, and by extension your ParentCollection class, are not generic, so Json.Net doesn't know that the items in the collection are supposed to be ChildDTO objects. Since it doesn't know what type the items are, it just uses JObject instead. You can fix the problem using a custom JsonConverter class like this:
class ParentCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ParentCollection);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
ParentCollection pc = new ParentCollection();
JArray array = JArray.Load(reader);
foreach (var item in array)
{
pc.Add(item.ToObject<ChildDTO>());
}
return pc;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
To use the converter, pass it to DeserializeObject<T> like this:
ParentCollection pCollection =
JsonConvert.DeserializeObject<ParentCollection>(json, new ParentCollectionConverter());
Working demo here: https://dotnetfiddle.net/3GM22c
Good morning. If you need the ChildDto's list, please convert it to List. Please refer to the code and image below. Many thanks.
string file = System.IO.File.ReadAllText("Children_CA.txt");
var results1 = JsonConvert.DeserializeObject<List<ChildDTO>>(file);

Why Json.NET cannot deserialize JSON arrays into object property?

Temporary note: This is NOT a duplicate of the above mentioned post
Let's say I have a server-side class structure like this.
public class Test
{
// this can be any kind of "Tag"
public object Data { get; set; }
}
public class Other
{
public string Test { get; set; }
}
Now a string like this is coming from let's say the client.
{"Data": [{$type: "MyProject.Other, MyProject", "Test": "Test"}] }
When I try to deserialize this into a Test instance, I get a result where the Tag property is a JToken instead of some kind of collection, for example ArrayList or List<object>.
I understand that Json.NET cannot deserialize into a strongly typed list, but I'd expect that it respects that it's at least a list.
Here is my current deserialization code.
var settings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto,
};
var str = "{\"Data\": [{\"$type\": \"MyProject.Other, MyProject\", \"Test\": \"Test\"}] }";
var test = JsonConvert.Deserialize<Test>(str, settings);
// this first assertion fails
(test.Data is IList).ShouldBeTrue();
(((IList)test.Data)[0] is Other).ShouldBeTrue();
I'm aware of the fact that if I serialize such a structure, then by default I'll get a { $type: ..., $values: [...]} structure in the JSON string instead of a pure array literal, and that will indeed properly deserialize. However, the client is sending a pure array literal, so I should be able to handle that in some way.
I managed to put together a JsonConverter to handle these kind of untyped lists. The converter applies when the target type is object. Then if the current token type is array start ([) it will force a deserialization into List<object>. In any other case it will fall back to normal deserialization.
This is a first version which passes my most important unit tests, however as I'm not a Json.NET expert, it might break some things unexpectedly. Please if anyone sees anything what I didn't, leave a comment.
public class UntypedListJsonConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanRead => true;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotSupportedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartArray)
{
return serializer.Deserialize(reader);
}
return serializer.Deserialize<List<object>>(reader);
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(object);
}
}
Usage example:
var settings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Auto,
Converters = new[] { new UntypedListJsonConverter() }
};
var str = "{\"Data\": [{\"$type\": \"MyProject.Other, MyProject\", \"Test\": \"Test\"}] }";
var test = JsonConvert.Deserialize<Test>(str, settings);
// now these assertions pass
(test.Data is IList).ShouldBeTrue();
(((IList)test.Data)[0] is Other).ShouldBeTrue();
Try this:
public class Test
{
public Dictionary<string, List<Other>> Data { get; } = new Dictionary<string, List<Other>>();
}
You need to set up the class you are trying to fill from json data to match as closely to the json structure. From the looks of it, the json looks a dictionary where the keys are strings and the values are arrays of Other objects.

Deserialize JSON object as .NET HashSet

Here's an example of raw JSON data:
{ "Standards": { "1": "1" } }
I want to deserialize the data to:
public class Model
{
public HashSet<String> Standards { get; set; }
}
The Standards field actually has the Dictionary<String, String> type. Somehow the keys and values are always equal. As the types are incompatible, I'm looking for a way to perform a custom deserialization of this field.
A solution based on JSON.NET library is preferred.
P.S.: I have no control over the data serialization process.
You can handle this with a custom JsonConverter. Here is the code you would need:
public class CustomHashSetConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(HashSet<string>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
return new HashSet<string>(jo.Properties().Select(p => p.Name));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
HashSet<string> hashSet = (HashSet<string>)value;
JObject jo = new JObject(hashSet.Select(s => new JProperty(s, s)));
jo.WriteTo(writer);
}
}
To use the converter, add a [JsonConverter] attribute to your model like this:
public class Model
{
[JsonConverter(typeof(CustomHashSetConverter))]
public HashSet<string> Standards { get; set; }
}
Then, just deserialize as normal and it should work:
Model model = JsonConvert.DeserializeObject<Model>(json);
Here is a round-trip demo: https://dotnetfiddle.net/tvHt5Y

Save Dictionary with nested array in MongoDB

The following class shall be received by an API as Json and stored in MongoDB, using the C# Driver and Web API. The data property is unstructured, but I can restrict it to key-value pairs with possibly nested arrays in these values.
public class Something
{
[BsonId, JsonIgnore]
public ObjectId _id { get; set; }
public IDictionary<string, object> data { get; set; }
}
When the json is posted from the client, Json.NET deserializes properly.
Saving the class to MongoDB, I get something like this in the database with the c# specific type:
{
property1: 'one',
property2: {
_t: 'System.Collections.Generic.List`1[System.Object]'
_v:
[
{}, {}, {}, ...
]
}
}
Based on these sources, I have pulled together a CustomCreationConverter for Json.NET that nests a List into the value of the Dictionary:
Apply JsonDictionaryAttributes to properties
Json.NET: Deserializing nested dictionaries
CustomCreationConverter Source Code
public class Something
{
...
[JsonProperty(ItemConverterType = typeof(CustomConverter))]
public IDictionary<string, object> data { get; set; }
}
with this override:
public class CustomConverter : CustomCreationConverter<IList<object>>
{
public override IList<object> Create(Type objectType)
{
return new List<object>();
}
public override bool CanConvert(Type objectType)
{
return true; // Just to keep it simple
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
return base.ReadJson(reader, objectType, existingValue, serializer);
return serializer.Deserialize(reader);
}
}
This works actually fine, but I still get the construction with the c# specific types in MongoDB. How can I get this data into MongoDB without the type-value properties when nested?
So I have this working with Dictionary<> rather than IDictionary<> without an custom converters, but I'm also using my own types rather than just object. I'm not clear what you mean by "useful way", but you can annotate your members with BsonDictionaryOptionsAttribute and pass in DictionaryRepresentation.Document or DictionaryRepresentation.ArrayOfDocument to change the shape persisted to MongoDB if that's what you mean?
Otherwise, what did you mean by "useful way" with regards to the way it's structured now? You'll alwways get the "_t" discriminator as long as there's more than one type possible. I'm guessing that you're seeing that because you're using IDictionary<>.

Categories