C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
ComplexNestedObjectConverter.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using Newtonsoft.Json;
4
6{
9 public class ComplexNestedObjectConverter : JsonConverter
10 {
13 public override bool CanWrite => false;
14
19 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
20 {
21 throw new NotSupportedException("CustomCreationConverter should only be used while deserializing.");
22 }
23
27 public override bool CanConvert(Type objectType)
28 {
29 return objectType == typeof(Dictionary<string, object>) ||
30 objectType == typeof(List<object>) ||
31 objectType == typeof(object);
32 }
33
40 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
41 {
42 if (reader.TokenType == JsonToken.Null)
43 return null;
44 object retVal;
45 // If the next token is an object deserialize to a Dictionary
46 if (reader.TokenType == JsonToken.StartObject)
47 {
48 retVal = new Dictionary<string, object>();
49 serializer.Populate(reader, retVal);
50 }
51 // If the next token is an array deserialize to an Array
52 else if (reader.TokenType == JsonToken.StartArray)
53 {
54 retVal = new List<object>();
55 serializer.Populate(reader, retVal);
56 }
57 // If the next token is not an object or list then fall back on the standard deserializer
58 else
59 {
60 retVal = serializer.Deserialize(reader);
61 }
62 return retVal;
63 }
64 }
65}
Ensure that when converting arbitrary dictionaries, nested dictionaries are correctly converted as we...
override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
Reads the JSON representation of the object.
override bool CanWrite
Gets a value indicating whether this T:Newtonsoft.Json.JsonConverter can write JSON.
override bool CanConvert(Type objectType)
Determines whether this instance can convert the specified object type.
override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
Writes the JSON representation of the object.