C# Client Library
A C# Client Library for the AnalyzeRe REST API
AnalyzeReJsonConverter.cs
Go to the documentation of this file.
1 using System;
2 using System.Globalization;
3 
4 using Newtonsoft.Json;
5 using Newtonsoft.Json.Converters;
6 using RestSharp;
7 using IDeserializer = RestSharp.Deserializers.IDeserializer;
8 using ISerializer = RestSharp.Serializers.ISerializer;
9 
10 namespace AnalyzeRe.JsonConverters
11 {
18  {
20  public static readonly AnalyzeReJsonConverter Default = new AnalyzeReJsonConverter();
21 
22  #region Serializer Settings
23  private static JsonSerializerSettings _defaultDeserializerSettings;
27  public static JsonSerializerSettings DefaultDeserializerSettings =>
28  _defaultDeserializerSettings ?? (_defaultDeserializerSettings =
29  new JsonSerializerSettings
30  {
31  Culture = CultureInfo.InvariantCulture,
32  DateFormatHandling = DateFormatHandling.IsoDateFormat,
33  DateParseHandling = DateParseHandling.None,
34  DateTimeZoneHandling = DateTimeZoneHandling.Utc,
35  FloatFormatHandling = FloatFormatHandling.Symbol,
36  NullValueHandling = NullValueHandling.Ignore,
37  Converters = new JsonConverter[]
38  {
39  new StringEnumConverter(),
40  new ReferenceConverter(),
42  // Allow creating any other APIType object.
44  // Handle nested object dictionaries and lists (e.g. for metadata)
46  new UriConverter(),
47  },
48  ContractResolver = new RespectIgnoreDataMemberResolver()
49  });
50 
51  private static JsonSerializerSettings _defaultSerializerSettings;
55  public static JsonSerializerSettings DefaultSerializerSettings =>
56  _defaultSerializerSettings ?? (_defaultSerializerSettings =
57  new JsonSerializerSettings
58  {
59  Culture = CultureInfo.InvariantCulture,
60  DateFormatHandling = DateFormatHandling.IsoDateFormat,
61  DateTimeZoneHandling = DateTimeZoneHandling.Utc,
62  ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
63  FloatFormatHandling = FloatFormatHandling.Symbol,
64  NullValueHandling = NullValueHandling.Ignore,
65  Converters = new JsonConverter[]
66  {
67  // Analyze Re server only supports up to 3 sub-second decimal places.
68  new IsoDateTimeConverter {
69  DateTimeFormat = API.DateTimeFormat,
70  DateTimeStyles = DateTimeStyles.AdjustToUniversal
71  },
72  new StringEnumConverter(),
73  new ReferenceConverter(),
75  new UriConverter()
76  },
77  ContractResolver = new AnalyzeReSerializationContractResolver()
78  });
79 
83  public readonly JsonSerializerSettings DeserializerSettings = DefaultDeserializerSettings.ShallowCopy();
84 
88  public readonly JsonSerializerSettings SerializerSettings = DefaultSerializerSettings.ShallowCopy();
89  #endregion Serializer Settings
90 
91  #region Interface Properties
92  public string ContentType { get; set; }
95  public CultureInfo Culture { get; set; }
97  public string DateFormat { get; set; }
99  public string Namespace { get; set; }
101  public string RootElement { get; set; }
102  #endregion Interface Properties
103 
106  {
107  Culture = CultureInfo.InvariantCulture;
108  ContentType = "application/json";
109  }
110 
119  T IDeserializer.Deserialize<T>(IRestResponse response)
120  {
121  return Deserialize<T>(response.Content);
122  }
123 
132  public T Deserialize<T>(IRestResponse response)
133  {
134  return Deserialize<T>(response.Content);
135  }
136 
145  public T Deserialize<T>(string json)
146  {
147  return JsonConvert.DeserializeObject<T>(json, DeserializerSettings);
148  }
149 
159  public object Deserialize(string json, Type deserializeType)
160  {
161  return JsonConvert.DeserializeObject(json, deserializeType, DeserializerSettings);
162  }
163 
167  public string Serialize(object obj)
168  {
169  return Serialize(obj, false);
170  }
171 
176  public string Serialize(object obj, bool indented)
177  {
178  return JsonConvert.SerializeObject(obj,
179  indented ? Formatting.Indented : Formatting.None, SerializerSettings);
180  }
181  }
182 }
Allows serializing and deserializing derived Analyze Re Resources to a target base type...
Ensure that when converting arbitrary dictionaries, nested dictionaries are correctly converted as we...
API methods / requests made available to the user.
Allows serializing and deserializing derived AnalyzeRe Resource Views to a target base type...
string Serialize(object obj)
Serialize the object as JSON
object Deserialize(string json, Type deserializeType)
Deserializes the rest response to a run-time type as per this converter&#39;s configuration.
AnalyzeReJsonConverter()
Create a new AnalyzeRe JsonConverter
Allows serializing and deserializing PortfolioViews, which require a special rule since they can only...
A pre-configured RestSharp serializer / deserializer which is made to support parsing of AnalyzeRe AP...
static readonly string DateTimeFormat
The format used when transmitting DateTime information to Analyze Re servers.
Converts JSON string representations of URLs to and from System.Uri instances.
Definition: UriConverter.cs:9
Properties tagged with the system IgnoreDataMemberAttribute should be ignored by the JSON serializer...
Custom property contract resolver which specifies that Server-Generated Analyze Re properties should ...
string Serialize(object obj, bool indented)
Serialize the object as JSON
Deserializes AnalyzeRe API Reference objects to a new instance of a the target generic Reference type...