private static void ReadPropSimple <T>(T t, PropertyPair nextProp) where T : new() { PropertyInfo property = t.GetType().GetProperty(nextProp.PropName); object value = FindTypeAndValue(property, nextProp.Value); property?.SetValue(t, value); }
private static void ReadPropObject <T>(T t, PropertyPair nextProp) where T : new() { PropertyInfo property = t.GetType().GetProperty(nextProp.PropName); Type propType = property.PropertyType; object propObj = Activator.CreateInstance(propType); object obj = Deserialize(nextProp.Value, propObj); property.SetValue(t, obj); }
public static object Deserialize(String json, object obj) { // assume json string is correct - i.e. no error checking json = json.Substring(1); // skip '{' while (json != "") { PropertyPair nextProp = MakeNextProp(json); json = ReadValue(nextProp, obj); } return(obj); }
private static void ReadPropList <T>(T t, PropertyPair nextProp) where T : new() { //todo Not working /* * PropertyInfo property = t.GetType().GetProperty(nextProp.PropName); * IEnumerable valueList = new List<object>(); * * string json = nextProp.Value.Substring(1); * while (!(json.StartsWith(']'))) * { * //todo read values into valueList * json = json.Substring(1); * } */ //property?.SetValue(t, valueList); }
/* * Help Deserialize */ private static string ReadValue <T>(PropertyPair nextProp, T t) where T : new() { string json; if (nextProp.Value.StartsWith('[')) { ReadPropList(t, nextProp); } else if (nextProp.Value.StartsWith('{')) { ReadPropObject(t, nextProp); } else // read simple type { ReadPropSimple(t, nextProp); } return(nextProp.RestOfJson); }
/* * Help Deserialize */ private static PropertyPair MakeNextProp(string json) { PropertyPair retPair = new PropertyPair(); int ix = json.IndexOf(':'); int jix = GetLastIndexOfValue(json, ix + 1); // Property Name retPair.PropName = json.Substring(1, ix - 2).Trim(); // Property Value retPair.Value = json.Substring(ix + 1, jix - ix).Trim(); // The rest of the json string if (jix == json.Length - 1) // at the end of the json string { retPair.RestOfJson = ""; } else { retPair.RestOfJson = json.Substring(jix + 2).Trim(); } return(retPair); }