using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Collections;
public static void Main()
string stringTest = "hello";
TypeWrapper<string> wrapper = new TypeWrapper<string>(stringTest);
var stringTestJson = wrapper.GetJSONObject();
Console.WriteLine(stringTestJson);
TypeWrapper<int> wrapper2 = new TypeWrapper<int>(intTest);
var intTestJson = wrapper2.GetJSONObject();
Console.WriteLine(intTestJson);
List<int> listOfIntTest = new List<int>(){5,4,3,2,1};
TypeWrapper<List<int>> wrapper3 = new TypeWrapper<List<int>>(listOfIntTest);
var listOfIntTestJson = wrapper3.GetJSONObject();
Console.WriteLine(listOfIntTestJson);
var unwrapper = new TypeUnwrapper();
var jListTest = unwrapper.GetObjectFromJSON(listOfIntTestJson);
Console.WriteLine(jListTest);
Console.WriteLine(jListTest.GetType());
public class TypeWrapper<T>
public TypeWrapper(T val){
public string Type { get; set; }
public T Value { get; set; }
public string GetJSONObject()
Type = Value.GetType().ToString();
var result = JsonConvert.SerializeObject(this);
public class TypeUnwrapper
public object GetObjectFromJSON(string JsonValue)
JToken jSettingObject = JObject.Parse(JsonValue);
JToken jSettingType = jSettingObject["Type"];
JToken jSettingValue = jSettingObject["Value"];
Type dotNetSettingType = jSettingObject.GetDotNetTypeFromJsonSettingToken();
object settingsObject = null;
if(jSettingValue.Type == JTokenType.Array){
Console.WriteLine(dotNetSettingType.IsGenericType && dotNetSettingType.GetGenericTypeDefinition() == typeof(List<>));
Console.WriteLine(IsGenericList<string>(dotNetSettingType));
if (IsGenericList(dotNetSettingType))
var listHandler = new ListSettingHandler();
return listHandler.GetListObjectFromJson(jSettingType, jSettingValue);
private bool IsGenericList(Type type)
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>);
private bool IsGenericList<T>(Type type)
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>) &&
Type.GetType(type.ToString()).GetGenericArguments().SingleOrDefault() == typeof(T);
public static class JsonExtensions
public static string StoreSettingTypeAndValueAsJson<T>(this T value)
var wrapper = new TypeWrapper<T>(value);
return wrapper.GetJSONObject();
public static Type GetDotNetTypeFromJsonSettingToken(this JToken token)
var jType = token["Type"];
return Type.GetType(jType.Value<string>());
catch (JsonReaderException ex)
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
public static bool TryParseJsonArray<T>(this string strInput, out List<T> output)
strInput = strInput.Trim();
JToken obj = JToken.Parse(strInput);
if (obj.Type != JTokenType.Array)
output = obj.ToObject<List<T>>();
catch (JsonReaderException ex)
Console.WriteLine(ex.Message);
Console.WriteLine(ex.ToString());
public class ListSettingHandler
public object GetListObjectFromJson(JToken jTypeValue, JToken jValue)
object listObject = null;
var jTokenValueArray = jValue;
var listItemType = Type.GetType(jTypeValue.Value<string>()).GetGenericArguments().FirstOrDefault();
if (listItemType == typeof(Int32))
listObject = Deserialize<int>(jTokenValueArray);
else if (listItemType == typeof(Int64))
listObject = Deserialize<long>(jTokenValueArray);
else if (listItemType == typeof(string))
listObject = Deserialize<string>(jTokenValueArray);
public static List<T> Deserialize<T>(JToken jArrayValue)
return jArrayValue.ToObject<List<T>>();