Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace MacUistin.Serialization
- {
- // I wrote this to help when creating new JsonConverter
- public static class JsonSerializationHelper
- {
- #region Retrieve values
- /*
- Can simplify the code for retreiving and defaulting values:
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- if (reader.TokenType == JsonToken.StartObject)
- {
- JObject item = JObject.Load(reader);
- if (item["Items"] == null)
- return null;
- var items = item.GetValueOrDefault<MyCustomList>("Items", serializer);
- string listField1 = item.GetValueOrDefault("ListField1", "xyz");
- int listField2 = item.GetValueOrDefault("ListField2", 123);
- return new MyCustomList(items, listField1, listField2);
- }
- throw new JsonSerializationException($"Unexpected format deserializing MyCustomList. Token type: {reader.TokenType}");
- }
- */
- public static T GetValueOrDefault<T>(this JObject jObject, string key, JsonSerializer serializer, T defaultValue = null) where T : class
- {
- var value = jObject[key];
- if (value == null)
- return defaultValue ?? new T();
- try
- {
- return value.ToObject<T>();
- }
- catch
- {
- return defaultValue ?? new T();
- }
- }
- public static string GetValueOrDefault(this JObject jObject, string key, string defaultValue = "")
- {
- var value = jObject[key];
- if (value == null)
- return defaultValue;
- try
- {
- return value.Value<string>();
- }
- catch
- {
- return defaultValue;
- }
- }
- public static T GetValueOrDefault<T>(this JObject jObject, string key, T defaultValue = default(T)) where T : struct
- {
- var value = jObject[key];
- if (value == null)
- return defaultValue;
- try
- {
- return value.Value<T>();
- }
- catch
- {
- return defaultValue;
- }
- }
- #endregion Retrieve values
- #region Set values
- /*
- Can simplify the writing of values to JSON:
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
- var items = (MyCustomList)value;
- writer.WriteObject(() =>
- {
- writer.WriteField("ListField1", items.ListField1);
- writer.WriteField("ListField2", items.ListField2);
- writer.WriteArray("Items", items, serializer);
- });
- }
- */
- public static void WriteField<T>(this JsonWriter writer, string fieldName, T fieldValue)
- {
- writer.WritePropertyName(fieldName);
- writer.WriteValue(fieldValue);
- }
- public static void WriteArray(this JsonWriter writer, string fieldName, Action action)
- {
- writer.WritePropertyName(fieldName);
- writer.WriteStartArray();
- action();
- writer.WriteEndArray();
- }
- public static void WriteArray(this JsonWriter writer, string fieldName, IEnumerable enumerable, JsonSerializer serializer)
- {
- writer.WriteArray(fieldName, () =>
- {
- foreach (var item in enumerable)
- {
- serializer.Serialize(writer, item);
- }
- });
- }
- public static void WriteObject(this JsonWriter writer, Action action)
- {
- writer.WriteStartObject();
- action();
- writer.WriteEndObject();
- }
- #endregion Set values
- }
- }
Add Comment
Please, Sign In to add comment