Serializer.cs
1 using System;
2 using System.Collections.Generic;
4 using Newtonsoft.Json;
5 using Newtonsoft.Json.Converters;
6 using Newtonsoft.Json.Linq;
7 using Newtonsoft.Json.Serialization;
8 
9 namespace Microsoft.Bot.Builder.Calling.ObjectModel.Misc
10 {
14  public static class Serializer
15  {
16  private static readonly JsonSerializerSettings defaultSerializerSettings = Serializer.GetSerializerSettings();
17  private static readonly JsonSerializerSettings loggingSerializerSettings = Serializer.GetSerializerSettings(Formatting.Indented);
18 
22  public static string SerializeToJson(object obj, bool forLogging = false)
23  {
24  return JsonConvert.SerializeObject(obj, forLogging ? loggingSerializerSettings : defaultSerializerSettings);
25  }
26 
32  public static JToken SerializeToJToken(Object obj)
33  {
34  return JToken.FromObject(obj, JsonSerializer.Create(defaultSerializerSettings));
35  }
36 
40  public static T DeserializeFromJson<T>(string json)
41  {
42  return JsonConvert.DeserializeObject<T>(json, defaultSerializerSettings);
43  }
44 
51  public static T DeserializeFromJToken<T>(JToken jToken)
52  {
53  return jToken.ToObject<T>(JsonSerializer.Create(defaultSerializerSettings));
54  }
55 
59  public static JsonSerializerSettings GetSerializerSettings(Formatting formatting = Formatting.None)
60  {
61  return new JsonSerializerSettings()
62  {
63  Formatting = formatting,
64  ContractResolver = new CamelCasePropertyNamesContractResolver(),
65  NullValueHandling = NullValueHandling.Ignore,
66  DateFormatHandling = DateFormatHandling.IsoDateFormat,
67  DateTimeZoneHandling = DateTimeZoneHandling.Utc,
68  ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
69  Converters = new List<JsonConverter> { new StringEnumConverter { CamelCaseText = true }, new ActionConverter(), new OperationOutcomeConverter(), new NotificationConverter() },
70  };
71  }
72  }
73 }
By default Json.net doesn&#39;t know how to deserialize JSON data into Interfaces or abstract classes...
Root namespace for the Microsoft Bot Builder Calling SDK object model.
Helper class for serializing/deserializing
Definition: Serializer.cs:14
static string SerializeToJson(object obj, bool forLogging=false)
Serialize input object to string
Definition: Serializer.cs:22
By default Json.net doesn&#39;t know how to deserialize JSON data into Interfaces or abstract classes...
Root namespace for the Microsoft Bot Builder Calling SDK.
By default Json.net doesn&#39;t know how to deserialize JSON data into Interfaces or abstract classes...
static JToken SerializeToJToken(Object obj)
Serialize to JToken
Definition: Serializer.cs:32
Namespace for contracts used by the Microsoft Bot Builder Calling SDK.
Definition: ActionBase.cs:7
static JsonSerializerSettings GetSerializerSettings(Formatting formatting=Formatting.None)
Returns default serializer settings.
Definition: Serializer.cs:59
Root namespace for the Microsoft Bot Builder SDK.