private object ParseObject(string JsonString, Type objType)
{
object rv = objType.GetConstructor(new Type[] {}).Invoke(new object[] {});
int pos = 0;
string fn = GetNextQuotedString(JsonString, ref pos);
while (fn != string.Empty)
{
FieldInfo fi = objType.GetField(fn);
if (fi.FieldType == typeof(string))
{
string val = GetValueString(JsonString, ref pos); //GetNextQuotedString(JsonString, ref pos);
fi.SetValue(rv, val);
}
else
{
if (fi.FieldType == typeof(int))
{
string val = GetValueString(JsonString, ref pos);
int i = (val == null) ? 0 : int.Parse(val);
fi.SetValue(rv, i);
}
else
{
if (fi.FieldType == typeof(float))
{
string val = GetValueString(JsonString, ref pos);
float f = (val == null) ? 0f : val.ToFloat();
fi.SetValue(rv, f);
}
else
{
if (fi.FieldType == typeof(double))
{
string val = GetNextQuotedString(JsonString, ref pos);
double d = (val == null) ? 0.0 : double.Parse(val);
fi.SetValue(rv, d);
}
else
{
if (fi.FieldType == typeof(Guid))
{
string val = GetNextQuotedString(JsonString, ref pos);
Guid uid = val.ToGuid();
fi.SetValue(rv, uid);
}
else
{
if (fi.FieldType == typeof(DateTime))
{
string val = GetValueString(JsonString, ref pos);
if (val != null)
{
DateTime dt = val.ToDateTime();
fi.SetValue(rv, dt);
}
}
else
{
if (fi.FieldType == typeof(Hashtable))
{
Hashtable ht = new Hashtable();
string key = GetNextQuotedString(JsonString, ref pos);
while (key != string.Empty)
{
string val = GetNextQuotedString(JsonString, ref pos);
ht.Add(key, val);
key = GetNextQuotedString(JsonString, ref pos);
}
fi.SetValue(rv, ht);
pos++;
}
else
{
if (fi.FieldType.IsArray)
{
throw new NotImplementedException("Array deserialization is not implemented yet.");
// TODO: deserialize array
}
else // structures
{
string val = GetSubObject(JsonString, ref pos);
object ov = ParseObject(val, fi.FieldType);
}
}
}
}
}
}
}
}
fn = GetNextQuotedString(JsonString, ref pos);
}
return rv;
}
}