mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
Removing Utils directory to better match namespaces
This commit is contained in:
78
ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs
Normal file
78
ICD.Common.Utils/Json/AbstractGenericJsonConverter.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using ICD.Common.Properties;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace ICD.Common.Utils.Json
|
||||
{
|
||||
public abstract class AbstractGenericJsonConverter<T> : JsonConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes the JSON representation of the object.
|
||||
/// </summary>
|
||||
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
public override sealed void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
writer.WriteNull();
|
||||
return;
|
||||
}
|
||||
|
||||
WriteJson(writer, (T)value, serializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the JSON representation of the object.
|
||||
/// </summary>
|
||||
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
[PublicAPI]
|
||||
public abstract void WriteJson(JsonWriter writer, T value, JsonSerializer serializer);
|
||||
|
||||
/// <summary>
|
||||
/// Reads the JSON representation of the object.
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>
|
||||
/// The object value.
|
||||
/// </returns>
|
||||
public override sealed object ReadJson(JsonReader reader, Type objectType, object existingValue,
|
||||
JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return null;
|
||||
|
||||
return ReadJson(reader, (T)existingValue, serializer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the JSON representation of the object.
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>
|
||||
/// The object value.
|
||||
/// </returns>
|
||||
[PublicAPI]
|
||||
public abstract T ReadJson(JsonReader reader, T existingValue, JsonSerializer serializer);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified object type.
|
||||
/// </summary>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return objectType == typeof(T);
|
||||
}
|
||||
}
|
||||
}
|
||||
86
ICD.Common.Utils/Json/JsonItemWrapper.cs
Normal file
86
ICD.Common.Utils/Json/JsonItemWrapper.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ICD.Common.Utils.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple wrapper for serialization of an object and its type.
|
||||
/// </summary>
|
||||
public sealed class JsonItemWrapper
|
||||
{
|
||||
private const string TYPE_TOKEN = "t";
|
||||
private const string ITEM_TOKEN = "i";
|
||||
|
||||
private readonly string m_ItemTypeString;
|
||||
private readonly object m_Item;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string representation of the item type. Returns null if the item is null.
|
||||
/// </summary>
|
||||
public string ItemTypeString { get { return m_ItemTypeString; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Type of the item. Returns null if the item is null.
|
||||
/// </summary>
|
||||
public Type ItemType
|
||||
{
|
||||
get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : Type.GetType(m_ItemTypeString); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the wrapped item.
|
||||
/// </summary>
|
||||
public object Item { get { return string.IsNullOrEmpty(m_ItemTypeString) ? null : m_Item; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
public JsonItemWrapper(object item)
|
||||
{
|
||||
m_ItemTypeString = item == null ? null : item.GetType().FullName;
|
||||
m_Item = item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the JsonItemWrapper as a JObject.
|
||||
/// </summary>
|
||||
/// <param name="writer"></param>
|
||||
public void Write(JsonWriter writer)
|
||||
{
|
||||
if (writer == null)
|
||||
throw new ArgumentNullException("writer");
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
writer.WritePropertyName(TYPE_TOKEN);
|
||||
writer.WriteValue(m_ItemTypeString);
|
||||
|
||||
writer.WritePropertyName(ITEM_TOKEN);
|
||||
writer.WriteValue(JsonConvert.SerializeObject(m_Item));
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the JToken back to the wrapped object.
|
||||
/// </summary>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
public static object ReadToObject(JToken token)
|
||||
{
|
||||
if (token == null)
|
||||
throw new ArgumentNullException("token");
|
||||
|
||||
string typeString = (string)token.SelectToken(TYPE_TOKEN);
|
||||
if (string.IsNullOrEmpty(typeString))
|
||||
return null;
|
||||
|
||||
string itemString = (string)token.SelectToken(ITEM_TOKEN);
|
||||
Type type = Type.GetType(typeString);
|
||||
|
||||
return JsonConvert.DeserializeObject(itemString, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
79
ICD.Common.Utils/Json/JsonUtils.cs
Normal file
79
ICD.Common.Utils/Json/JsonUtils.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Utility methods for working with JSON.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public static class JsonUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Pretty-prints the JSON document.
|
||||
/// </summary>
|
||||
/// <param name="json"></param>
|
||||
[PublicAPI]
|
||||
public static void Print(string json)
|
||||
{
|
||||
int indent = 0;
|
||||
bool quoted = false;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < json.Length; i++)
|
||||
{
|
||||
char ch = json[i];
|
||||
switch (ch)
|
||||
{
|
||||
case '{':
|
||||
case '[':
|
||||
sb.Append(ch);
|
||||
if (!quoted)
|
||||
{
|
||||
sb.Append(IcdEnvironment.NewLine);
|
||||
Enumerable.Range(0, ++indent).ForEach(item => sb.Append('\t'));
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
case ']':
|
||||
if (!quoted)
|
||||
{
|
||||
sb.Append(IcdEnvironment.NewLine);
|
||||
Enumerable.Range(0, --indent).ForEach(item => sb.Append('\t'));
|
||||
}
|
||||
sb.Append(ch);
|
||||
break;
|
||||
case '"':
|
||||
sb.Append(ch);
|
||||
bool escaped = false;
|
||||
int index = i;
|
||||
while (index > 0 && json[--index] == '\\')
|
||||
escaped = !escaped;
|
||||
if (!escaped)
|
||||
quoted = !quoted;
|
||||
break;
|
||||
case ',':
|
||||
sb.Append(ch);
|
||||
if (!quoted)
|
||||
{
|
||||
sb.Append(IcdEnvironment.NewLine);
|
||||
Enumerable.Range(0, indent).ForEach(item => sb.Append('\t'));
|
||||
}
|
||||
break;
|
||||
case ':':
|
||||
sb.Append(ch);
|
||||
if (!quoted)
|
||||
sb.Append(" ");
|
||||
break;
|
||||
default:
|
||||
sb.Append(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
IcdConsole.PrintLine(sb.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user