Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 65 additions & 13 deletions UnitTests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public class RetNestedclass

#endregion

[TestFixtureSetUp]
[OneTimeSetUp]
public static void setup()
{
//fastJSON.JSON.Parameters = new JSONParameters();
Expand Down Expand Up @@ -1864,24 +1864,76 @@ public static void comments()
Assert.AreEqual(2, (o as IDictionary).Count);
}

public class ctype
public struct Point
{
public System.Net.IPAddress ip;
public int x;
public float y;
}
public class Complex
{
public int i;
public List<Point[]> l;
}
[Test]
public static void CustomTypes()
{
var ip = new ctype();
ip.ip = System.Net.IPAddress.Loopback;

JSON.RegisterCustomType(typeof(System.Net.IPAddress),
(x) => { return x.ToString(); },
(x) => { return System.Net.IPAddress.Parse(x); });

var s = JSON.ToJSON(ip);
JSON.RegisterCustomType(typeof(Point),
(o) => {
var pt = (Point)o;
return string.Format("[{0},{1}]", pt.x, pt.y);
},
(o, cb) => {
var l = o as List<object>;
return new Point() { x = Convert.ToInt32(l[0]), y = Convert.ToSingle(l[1]) };
});
JSON.RegisterCustomType(typeof(Complex),
(o) => {
var c = (Complex)o;
return string.Format("[{0},{1}]", c.i, JSON.ToJSON(c.l));
},
(o, cb) => {
var l = o as List<object>;
return new Complex() { i = Convert.ToInt32(l[0]), l = (List<Point[]>)cb(l[1], typeof(List<Point[]>)) };
});

var o = JSON.ToObject<ctype>(s);
Assert.AreEqual(ip.ip, o.ip);
var pt1 = new Point() { x = 1, y = 2.3f };
var s = JSON.ToJSON(pt1);
Console.WriteLine(s);
var pt2 = JSON.ToObject<Point>(s);
Assert.AreEqual(pt1, pt2);

var c1 = new Complex() {
i = 9,
l = new List<Point[]>() {
new Point[] {
new Point() { x = 1, y = 2.3f },
new Point() { x = 2, y = 3.4f },
},
new Point[] {
new Point() { x = 5, y = 6.7f },
},
}
};
s = JSON.ToJSON(c1);
Console.WriteLine(s);
var c2 = JSON.ToObject<Complex>(s);
Assert.AreEqual(s, JSON.ToJSON(c2));

var d1 = new Dictionary<string, List<List<Point>>>() {
{"a", new List<List<Point>> {
new List<Point> {
new Point() { x = 1, y = 2.3f },
new Point() { x = 2, y = 3.4f },
},
new List<Point> {
new Point() { x = 5, y = 6.7f },
},
}}
};
s = JSON.ToJSON(d1);
Console.WriteLine(s);
var d2 = JSON.ToObject(s, d1.GetType());
Assert.AreEqual(s, JSON.ToJSON(d2));
}

[Test]
Expand Down
38 changes: 27 additions & 11 deletions fastJSON/JSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

namespace fastJSON
{
public delegate object DeserializeCallback(object data, Type type);
public delegate string Serialize(object data);
public delegate object Deserialize(string data);
public delegate object Deserialize(object data, DeserializeCallback cb);

public sealed class JSONParameters
{
Expand Down Expand Up @@ -382,6 +383,12 @@ public object ToObject(string json)
}

public object ToObject(string json, Type type)
{
object o = new JsonParser(json).Decode();
return ToTyped(o, type);
}

public object ToTyped(object o, Type type)
{
//_params = Parameters;
_params.FixValues();
Expand All @@ -392,7 +399,9 @@ public object ToObject(string json, Type type)
_params.UsingGlobalTypes = false;
_usingglobals = _params.UsingGlobalTypes;

object o = new JsonParser(json).Decode();
if ((type != null) && Reflection.Instance.IsTypeRegistered(type))
return Reflection.Instance.CreateCustom(o, type, ToTyped);

if (o == null)
return null;
#if !SILVERLIGHT
Expand Down Expand Up @@ -490,9 +499,6 @@ private object ChangeType(object value, Type conversionType)
else if (conversionType == typeof(DateTimeOffset))
return CreateDateTimeOffset((string)value);

else if (Reflection.Instance.IsTypeRegistered(conversionType))
return Reflection.Instance.CreateCustom((string)value, conversionType);

// 8-30-2014 - James Brooks - Added code for nullable types.
if (IsNullable(conversionType))
{
Expand Down Expand Up @@ -612,8 +618,17 @@ private void DoParseList(object parse, Type it, IList o)
{
_usingglobals = false;
object v = k;
if (k is Dictionary<string, object>)
if ((it != null) && Reflection.Instance.IsTypeRegistered(it))
v = Reflection.Instance.CreateCustom(k, it, ToTyped);
else if (k is Dictionary<string, object>)
v = ParseDictionary(k as Dictionary<string, object>, globals, it, null);
else if (k is List<object>)
{
if (it != null && it.IsArray)
v = RootArray(k, it);
else
v = RootList(k, it);
}
else
v = ChangeType(k, it);

Expand Down Expand Up @@ -682,6 +697,8 @@ internal object ParseDictionary(Dictionary<string, object> d, Dictionary<string,
return CreateNV(d);
if (type == typeof(StringDictionary))
return CreateSD(d);
if ((type != null) && type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(Dictionary<,>)))
return RootDictionary(d, type);

if (d.TryGetValue("$i", out tn))
{
Expand Down Expand Up @@ -789,7 +806,7 @@ internal object ParseDictionary(Dictionary<string, object> d, Dictionary<string,
case myPropInfoType.StringKeyDictionary: oset = CreateStringKeyDictionary((Dictionary<string, object>)v, pi.pt, pi.GenericTypes, globaltypes); break;
case myPropInfoType.NameValue: oset = CreateNV((Dictionary<string, object>)v); break;
case myPropInfoType.StringDictionary: oset = CreateSD((Dictionary<string, object>)v); break;
case myPropInfoType.Custom: oset = Reflection.Instance.CreateCustom((string)v, pi.pt); break;
case myPropInfoType.Custom: oset = Reflection.Instance.CreateCustom(v, pi.pt, ToTyped); break;
default:
{
if (pi.IsGenericType && pi.IsValueType == false && v is List<object>)
Expand Down Expand Up @@ -997,13 +1014,12 @@ private object CreateGenericList(List<object> data, Type pt, Type bt, Dictionary
{
if (ob is IDictionary)
col.Add(ParseDictionary((Dictionary<string, object>)ob, globalTypes, it, null));

else if (ob is List<object>)
{
if (bt.IsGenericType)
col.Add((List<object>)ob);//).ToArray());
if (it.IsArray)
col.Add(RootArray(ob, it));
else
col.Add(((List<object>)ob).ToArray());
col.Add(RootList(ob, it));
}
else
col.Add(ChangeType(ob, it));
Expand Down
7 changes: 6 additions & 1 deletion fastJSON/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void WriteCustom(object obj)
{
Serialize s;
Reflection.Instance._customSerializer.TryGetValue(obj.GetType(), out s);
WriteStringFast(s(obj));
WriteRaw(s(obj));
}

private void WriteEnum(Enum e)
Expand Down Expand Up @@ -686,5 +686,10 @@ private void WriteString(string s)

_output.Append('\"');
}

private void WriteRaw(string s)
{
_output.Append(s);
}
}
}
4 changes: 2 additions & 2 deletions fastJSON/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ private Reflection()
internal SafeDictionary<Type, Serialize> _customSerializer = new SafeDictionary<Type, Serialize>();
internal SafeDictionary<Type, Deserialize> _customDeserializer = new SafeDictionary<Type, Deserialize>();

internal object CreateCustom(string v, Type type)
internal object CreateCustom(object v, Type type, DeserializeCallback cb)
{
Deserialize d;
_customDeserializer.TryGetValue(type, out d);
return d(v);
return d(v, cb);
}

internal void RegisterCustomType(Type type, Serialize serializer, Deserialize deserializer)
Expand Down