[jayrock] 18 new revisions pushed by azizatif on 2011-04-29 19:29 GMT

5 views
Skip to first unread message

jay...@googlecode.com

unread,
Apr 29, 2011, 3:30:17 PM4/29/11
to jayrock...@googlegroups.com
18 new revisions:

Revision: b861c53071e1
Author: azizatif
Date: Thu Apr 21 15:02:10 2011
Log: Added object construction, that enables using constructors to
create o...
http://code.google.com/p/jayrock/source/detail?r=b861c53071e1

Revision: 6bdef0e5dc01
Author: azizatif
Date: Thu Apr 21 15:20:57 2011
Log: Linked object construction into ImportContext and
CompontentImporter a...
http://code.google.com/p/jayrock/source/detail?r=6bdef0e5dc01

Revision: bcbb99a7593b
Author: azizatif
Date: Thu Apr 21 16:04:27 2011
Log: Broke out some types into their own files
http://code.google.com/p/jayrock/source/detail?r=bcbb99a7593b

Revision: 0daa5084cd43
Author: azizatif
Date: Thu Apr 21 16:06:48 2011
Log: Added NamedJsonBuffer.ToObject helper to convert an array of
NamedJson...
http://code.google.com/p/jayrock/source/detail?r=0daa5084cd43

Revision: f3f9caeb21be
Author: azizatif
Date: Thu Apr 21 16:10:33 2011
Log: Updated ObjectConstructor to support the default constructor that
is s...
http://code.google.com/p/jayrock/source/detail?r=f3f9caeb21be

Revision: 97185b7750fc
Author: azizatif
Date: Thu Apr 21 16:11:23 2011
Log: Fixed typo
http://code.google.com/p/jayrock/source/detail?r=97185b7750fc

Revision: b3dcb393bd4e
Author: azizatif
Date: Thu Apr 21 16:12:53 2011
Log: Renamed test for consistency
http://code.google.com/p/jayrock/source/detail?r=b3dcb393bd4e

Revision: 45d93e6db249
Author: azizatif
Date: Thu Apr 21 16:15:22 2011
Log: Updated ImportContext.FindCompatibleImporter to no longer exclude
the ...
http://code.google.com/p/jayrock/source/detail?r=45d93e6db249

Revision: c756eb01921c
Author: azizatif
Date: Thu Apr 21 16:25:52 2011
Log: Moved StockJsonBuffers to the correct namespace
http://code.google.com/p/jayrock/source/detail?r=c756eb01921c

Revision: 4b50fa0775d3
Author: azizatif
Date: Thu Apr 21 16:29:25 2011
Log: Added ObjectSurrogateConstructor that can be used to construct an
obje...
http://code.google.com/p/jayrock/source/detail?r=4b50fa0775d3

Revision: 17dbcba8c1bd
Author: azizatif
Date: Thu Apr 21 16:35:33 2011
Log: Added a base implementation for surrogate object constructors
that pri...
http://code.google.com/p/jayrock/source/detail?r=17dbcba8c1bd

Revision: a78cc21ac2a3
Author: azizatif
Date: Thu Apr 21 16:38:50 2011
Log: Aligned ExportContext.FindExporter with ImportContext.FindImporter
http://code.google.com/p/jayrock/source/detail?r=a78cc21ac2a3

Revision: 8df6e4f875e8
Author: azizatif
Date: Thu Apr 21 16:41:04 2011
Log: Optimized away array creation in ComponentExporter to fault-in
later w...
http://code.google.com/p/jayrock/source/detail?r=8df6e4f875e8

Revision: 27aff804a7a4
Author: azizatif
Date: Fri Apr 22 02:54:28 2011
Log: Fixed C# 1 compilation issue ObjectConstructor; need to spell out
dele...
http://code.google.com/p/jayrock/source/detail?r=27aff804a7a4

Revision: 78e0011b2ec7
Author: azizatif
Date: Sun Apr 24 08:09:46 2011
Log: Added anonymous type support to ImportContext for sake of
symmetry wit...
http://code.google.com/p/jayrock/source/detail?r=78e0011b2ec7

Revision: 7ab23338fb85
Author: azizatif
Date: Tue Apr 26 18:08:56 2011
Log: Updated file license headers
http://code.google.com/p/jayrock/source/detail?r=7ab23338fb85

Revision: 195961addddb
Author: azizatif
Date: Tue Apr 26 18:35:26 2011
Log: Chained JsonMemberNamingConvention constructors to avoid
duplication
http://code.google.com/p/jayrock/source/detail?r=195961addddb

Revision: 2fba3ebf9389
Author: azizatif
Date: Fri Apr 29 12:29:08 2011
Log: Rendered JsonTokenClass.All truly read-only
http://code.google.com/p/jayrock/source/detail?r=2fba3ebf9389

==============================================================================
Revision: b861c53071e1
Author: azizatif
Date: Thu Apr 21 15:02:10 2011
Log: Added object construction, that enables using constructors to
create objects from JSON
http://code.google.com/p/jayrock/source/detail?r=b861c53071e1

Added:
/src/Jayrock.Json/Comparers.cs
/src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs
/src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs
/src/Jayrock.Json/Json/Conversion/StockJsonBuffers.cs
/tests/Jayrock/Json/Conversion/TestObjectConstructor.cs
Modified:
/src/Jayrock.Json/Jayrock.Json.csproj
/tests/Jayrock/Jayrock.Tests.csproj

=======================================
--- /dev/null
+++ /src/Jayrock.Json/Comparers.cs Thu Apr 21 15:02:10 2011
@@ -0,0 +1,65 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock
+{
+ #region Imports
+
+ using System;
+ using System.Collections;
+
+ #endregion
+
+ internal sealed class ReverseComparer : IComparer
+ {
+ private readonly IComparer _comparer;
+
+ public ReverseComparer(IComparer comparer)
+ {
+ if (comparer == null) throw new
ArgumentNullException("comparer");
+ _comparer = comparer;
+ }
+
+ public int Compare(object x, object y)
+ {
+ return -1 * _comparer.Compare(x, y);
+ }
+ }
+
+ internal delegate IComparable ComparableSelector(object a);
+
+ internal sealed class DelegatingComparer : IComparer
+ {
+ private readonly ComparableSelector _selector;
+
+ public DelegatingComparer(ComparableSelector selector)
+ {
+ if (selector == null) throw new
ArgumentNullException("selector");
+ _selector = selector;
+ }
+
+ public int Compare(object x, object y)
+ {
+ return _selector(x).CompareTo(_selector(y));
+ }
+ }
+}
=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs Thu Apr 21
15:02:10 2011
@@ -0,0 +1,58 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ using System;
+
+ /// <summary>
+ /// Represents an object constructor.
+ /// </summary>
+
+ public interface IObjectConstructor
+ {
+ ObjectConstructionResult CreateObject(ImportContext context,
JsonReader reader);
+ }
+
+ public sealed class ObjectConstructionResult
+ {
+ private readonly object _obj;
+ private readonly JsonReader _tailReader;
+
+ public ObjectConstructionResult(object obj, JsonReader tailReader)
+ {
+ if (obj == null) throw new ArgumentNullException("obj");
+ if (tailReader == null) throw new
ArgumentNullException("reader");
+
+ _obj = obj;
+ _tailReader = tailReader;
+ }
+
+ public object Object { get { return _obj; } }
+ public JsonReader TailReader { get { return _tailReader; } }
+ }
+
+ public interface IObjectSurrogateConstructor
+ {
+ ObjectConstructionResult CreateObject(ImportContext context);
+ }
+}
=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
15:02:10 2011
@@ -0,0 +1,210 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ #region Imports
+
+ using System;
+ using System.Collections;
+ using System.Diagnostics;
+ using System.Globalization;
+ using System.Reflection;
+
+ #endregion
+
+ public sealed class ObjectConstructor : IObjectConstructor
+ {
+ private readonly ConstructorInfo[] _ctors;
+
+ private static readonly IComparer _arrayLengthComparer = new
ReverseComparer(new DelegatingComparer(GetParametersCount));
+
+ public ObjectConstructor(Type type) : this(type, null) {}
+
+ public ObjectConstructor(Type type, ConstructorInfo[] ctors)
+ {
+ if (type == null) throw new ArgumentNullException("type");
+
+ if (ctors == null)
+ {
+ ctors = type.GetConstructors();
+ }
+ else
+ {
+ foreach (ConstructorInfo ctor in ctors)
+ {
+ if (ctor.DeclaringType != type)
+ throw new ArgumentException(null, "ctors");
+ }
+
+ ctors = (ConstructorInfo[]) ctors.Clone();
+ }
+
+ if (ctors.Length == 0)
+ throw new ArgumentException(null, "ctors");
+
+ _ctors = ctors;
+ Array.Sort(_ctors, _arrayLengthComparer);
+ }
+
+ public ObjectConstructionResult CreateObject(ImportContext
context, JsonReader reader)
+ {
+ if (context == null) throw new
ArgumentNullException("context");
+ if (reader == null) throw new ArgumentNullException("reader");
+
+ return CreateObject(context,
JsonBuffer.From(reader).GetMembersArray());
+ }
+
+ public ObjectConstructionResult CreateObject(ImportContext
context, NamedJsonBuffer[] members)
+ {
+ if (context == null) throw new
ArgumentNullException("context");
+ if (members == null) throw new
ArgumentNullException("members");
+
+ foreach (ConstructorInfo ctor in _ctors)
+ {
+ ObjectConstructionResult result = TryCreateObject(context,
ctor, members);
+ if (result != null)
+ return result;
+ }
+
+ throw new JsonException(string.Format("None constructor could
be used to create {0} object from JSON.", _ctors[0].DeclaringType));
+ }
+
+ private static ObjectConstructionResult
TryCreateObject(ImportContext context, ConstructorInfo ctor,
NamedJsonBuffer[] members)
+ {
+ Debug.Assert(context != null);
+ Debug.Assert(ctor != null);
+ Debug.Assert(members != null);
+
+ ParameterInfo[] parameters = ctor.GetParameters();
+
+ if (parameters.Length > members.Length)
+ return null;
+
+ int[] bindings = Bind(context, parameters, members);
+
+ int argc = 0;
+ object[] args = null;
+ JsonBufferWriter tailw = null;
+
+ for (int i = 0; i < bindings.Length; i++)
+ {
+ int binding = bindings[i] - 1;
+
+ if (binding >= 0)
+ {
+ if (args == null)
+ args = new object[parameters.Length];
+
+ Type type = parameters[binding].ParameterType;
+ JsonBuffer arg = members[i].Buffer;
+ args[binding] = context.Import(type,
arg.CreateReader());
+ argc++;
+ }
+ else
+ {
+ if (tailw == null)
+ {
+ tailw = new JsonBufferWriter();
+ tailw.WriteStartObject();
+ }
+
+ NamedJsonBuffer member = members[i];
+ tailw.WriteMember(member.Name);
+ tailw.WriteFromReader(member.Buffer.CreateReader());
+ }
+ }
+
+ if (tailw != null)
+ tailw.WriteEndObject();
+
+ if (argc != parameters.Length)
+ return null;
+
+ object obj = ctor.Invoke(args);
+
+ JsonBuffer tail = tailw != null
+ ? tailw.GetBuffer()
+ : StockJsonBuffers.EmptyObject;
+
+ return new ObjectConstructionResult(obj, tail.CreateReader());
+ }
+
+ /// <remarks>
+ /// Bound indicies returned in the resulting array are one-based
+ /// therefore zero mean unbound.
+ /// </remarks>
+
+ private static int[] Bind(ImportContext context, ParameterInfo[]
parameters, NamedJsonBuffer[] members)
+ {
+ if (context == null) throw new
ArgumentNullException("context");
+ if (parameters == null) throw new
ArgumentNullException("parameters");
+ if (members == null) throw new
ArgumentNullException("members");
+
+ int[] bindings = new int[members.Length];
+
+ for (int i = 0; i < parameters.Length; i++)
+ {
+ ParameterInfo parameter = parameters[i];
+
+ if (parameter == null)
+ throw new ArgumentException(null, "parameters");
+
+ int mi = FindMember(members, parameter.Name);
+
+ if (mi >= 0)
+ bindings[mi] = i + 1;
+ }
+
+ return bindings;
+ }
+
+ private static int FindMember(NamedJsonBuffer[] members, string
name)
+ {
+ for (int i = 0; i < members.Length; i++)
+ {
+ NamedJsonBuffer member = members[i];
+
+ if (member.IsEmpty)
+ throw new ArgumentException(null, "members");
+
+ if (0 ==
CultureInfo.InvariantCulture.CompareInfo.Compare(name, member.Name,
CompareOptions.IgnoreCase))
+ return i;
+ }
+
+ return -1;
+ }
+
+ private static IComparable GetParametersCount(object obj)
+ {
+ if (obj == null)
+ throw new ArgumentNullException("obj");
+
+ MethodBase method = obj as MethodBase;
+
+ if (method == null)
+ throw new ArgumentException("obj");
+
+ return method.GetParameters().Length;
+ }
+ }
+}
=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/StockJsonBuffers.cs Thu Apr 21
15:02:10 2011
@@ -0,0 +1,37 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ public sealed class StockJsonBuffers
+ {
+ public static readonly JsonBuffer EmptyObject;
+
+ static StockJsonBuffers()
+ {
+ JsonBufferWriter writer = new JsonBufferWriter(2);
+ writer.WriteStartObject();
+ writer.WriteEndObject();
+ EmptyObject = writer.GetBuffer();
+ }
+ }
+}
=======================================
--- /dev/null
+++ /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
15:02:10 2011
@@ -0,0 +1,207 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Tests.Json.Conversion
+{
+ #region Imports
+
+ using System;
+ using System.Collections;
+ using System.Reflection;
+ using Jayrock.Json;
+ using Jayrock.Json.Conversion;
+ using NUnit.Framework;
+
+ #endregion
+
+ [TestFixture]
+ public class TestObjectConstructor
+ {
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotInitializeWithNullType()
+ {
+ new ObjectConstructor(null);
+ }
+
+ [Test, ExpectedException(typeof(ArgumentException))]
+ public void CannotInitializeWithConstructorsFromDifferentTypes()
+ {
+ ConstructorInfo[] fc = typeof(FooBase).GetConstructors();
+ Assert.AreEqual(1, fc.Length);
+ ConstructorInfo[] fdc = typeof(FooDerived).GetConstructors();
+ Assert.AreEqual(1, fdc.Length);
+ ConstructorInfo[] ctors = new ConstructorInfo[fc.Length +
fdc.Length];
+ Array.Copy(fc, 0, ctors, 0, fc.Length);
+ Array.Copy(fdc, 0, ctors, fc.Length, fdc.Length);
+ new ObjectConstructor(typeof(FooDerived), ctors);
+ }
+
+ class FooBase {}
+ class FooDerived : FooBase {}
+
+ [Test, ExpectedException(typeof(ArgumentException))]
+ public void CannotInitializeWithNonConstructibleType()
+ {
+ new ObjectConstructor(typeof(PrivateThing));
+ }
+
+ class PrivateThing { private PrivateThing() {} }
+
+ [Test, ExpectedException(typeof(ArgumentException))]
+ public void CannotInitializeWithZeroConstructors()
+ {
+ new ObjectConstructor(typeof(object), new ConstructorInfo[0]);
+ }
+
+ [Test]
+ public void Construction()
+ {
+ ObjectConstructor ctor = new ObjectConstructor(typeof(Point));
+ ImportContext context = JsonConvert.CreateImportContext();
+ const string json = "{ y: 456, x: 123 }";
+ ObjectConstructionResult result = ctor.CreateObject(context,
JsonText.CreateReader(json));
+ Point point = (Point)result.Object;
+ Assert.AreEqual(123, point.X);
+ Assert.AreEqual(456, point.Y);
+ Assert.AreEqual(0,
JsonBuffer.From(result.TailReader).GetMemberCount());
+ }
+
+ [Test]
+ public void ConstructionWithTail()
+ {
+ ObjectConstructor ctor = new ObjectConstructor(typeof(Point));
+ ImportContext context = JsonConvert.CreateImportContext();
+ const string json = "{ y: 456, z: 789, x: 123 }";
+ ObjectConstructionResult result = ctor.CreateObject(context,
JsonText.CreateReader(json));
+ Point point = (Point) result.Object;
+ Assert.AreEqual(123, point.X);
+ Assert.AreEqual(456, point.Y);
+ NamedJsonBuffer[] tail =
JsonBuffer.From(result.TailReader).GetMembersArray();
+ Assert.AreEqual(1, tail.Length);
+ NamedJsonBuffer z = tail[0];
+ Assert.AreEqual("z", z.Name);
+ Assert.AreEqual(789, z.Buffer.GetNumber().ToInt32());
+ }
+
+ [Test, ExpectedException(typeof(JsonException))]
+ public void CannotCreateIfNoneConstructorsMatch()
+ {
+ ObjectConstructor ctor = new ObjectConstructor(typeof(Point));
+ ImportContext context = JsonConvert.CreateImportContext();
+ ctor.CreateObject(context, JsonText.CreateReader("{ z: x: 123
}"));
+ }
+
+ class Point
+ {
+ private readonly int _x;
+ private readonly int _y;
+
+ public Point(int x, int y)
+ {
+ _x = x;
+ _y = y;
+ }
+
+ public int X { get { return _x; } }
+ public int Y { get { return _y; } }
+ }
+
+ [Test]
+ public void ConstructorSpecificity()
+ {
+ Thing thing = CreateThing("{ num: 42 }");
+ Assert.AreEqual(42, thing.Number);
+ Assert.AreEqual(GetThingConstructor(typeof(int)),
thing.Constructor);
+
+ thing = CreateThing("{ num: 42, flag: true }");
+ Assert.AreEqual(42, thing.Number);
+ Assert.IsTrue(thing.Flag);
+ Assert.AreEqual(GetThingConstructor(typeof(int),
typeof(bool)), thing.Constructor);
+
+ thing = CreateThing("{ num: 42, flag: true, str: hello }");
+ Assert.AreEqual(42, thing.Number);
+ Assert.IsTrue(thing.Flag);
+ Assert.AreEqual("hello", thing.String);
+ Assert.AreEqual(GetThingConstructor(typeof(int), typeof(bool),
typeof(string)), thing.Constructor);
+
+ thing = CreateThing("{ num: 42, flag: true, str: hello, obj: {
a: 123, b: 456 } }");
+ Assert.AreEqual(42, thing.Number);
+ Assert.IsTrue(thing.Flag);
+ Assert.AreEqual("hello", thing.String);
+ Assert.AreEqual(2, thing.Object.Count);
+ Assert.AreEqual(123, Convert.ToInt32(thing.Object["a"]));
+ Assert.AreEqual(456, Convert.ToInt32(thing.Object["b"]));
+ Assert.AreEqual(GetThingConstructor(typeof(int), typeof(bool),
typeof(string), typeof(IDictionary)), thing.Constructor);
+ }
+
+ private static ConstructorInfo GetThingConstructor(params Type[]
types)
+ {
+ return typeof(Thing).GetConstructor(BindingFlags.Instance |
BindingFlags.Public, null, types, null);
+ }
+
+ private Thing CreateThing(string json)
+ {
+ ObjectConstructor ctor = new ObjectConstructor(typeof(Thing));
+ ImportContext context = JsonConvert.CreateImportContext();
+ ObjectConstructionResult result = ctor.CreateObject(context,
JsonText.CreateReader(json));
+ return (Thing) result.Object;
+ }
+
+ class Thing
+ {
+ private readonly int _num;
+ private readonly bool _flag;
+ private readonly string _str;
+ private readonly IDictionary _obj;
+ private readonly ConstructorInfo _ctor;
+
+ // ReSharper disable UnusedMember.Local
+ public Thing(int num)
+ : this(num, false, null, null,
(ConstructorInfo)MethodBase.GetCurrentMethod()) {}
+
+ public Thing(int num, bool flag)
+ : this(num, flag, null, null,
(ConstructorInfo)MethodBase.GetCurrentMethod()) {}
+
+ public Thing(int num, bool flag, string str) :
+ this(num, flag, str, null,
(ConstructorInfo)MethodBase.GetCurrentMethod()) { }
+
+ public Thing(int num, bool flag, string str, IDictionary obj) :
+ this(num, flag, str, obj, (ConstructorInfo)
MethodBase.GetCurrentMethod()) {}
+
+ private Thing(int num, bool flag, string str, IDictionary obj,
ConstructorInfo ctor)
+ {
+ _num = num;
+ _flag = flag;
+ _str = str;
+ _obj = obj;
+ _ctor = ctor;
+ }
+ // ReSharper restore UnusedMember.Local
+
+ public int Number { get { return _num; } }
+ public bool Flag { get { return _flag; } }
+ public string String { get { return _str; } }
+ public IDictionary Object { get { return _obj; } }
+ public ConstructorInfo Constructor { get { return _ctor; } }
+ }
+ }
+}
=======================================
--- /src/Jayrock.Json/Jayrock.Json.csproj Sat Apr 16 03:31:20 2011
+++ /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 15:02:10 2011
@@ -160,6 +160,7 @@
<Compile Include="Json\Conversion\AnyType.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Comparers.cs" />
<Compile Include="Json\Conversion\Converters\AnyImporter.cs">
<SubType>Code</SubType>
</Compile>
@@ -306,6 +307,7 @@
<Compile Include="Json\Conversion\ImporterListSectionHandler.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\IObjectConstructor.cs" />
<Compile Include="Json\Conversion\IObjectMemberExporter.cs">
<SubType>Code</SubType>
</Compile>
@@ -331,9 +333,11 @@
<Compile
Include="Json\Conversion\JsonMemberNamingConventionAttribute.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\ObjectConstructor.cs" />
<Compile Include="Json\Conversion\PropertyCustomization.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\StockJsonBuffers.cs" />
<Compile Include="Json\EmptyJsonWriter.cs">
<SubType>Code</SubType>
</Compile>
=======================================
--- /tests/Jayrock/Jayrock.Tests.csproj Sat Apr 16 03:31:20 2011
+++ /tests/Jayrock/Jayrock.Tests.csproj Thu Apr 21 15:02:10 2011
@@ -114,6 +114,7 @@
<Compile Include="Json\Conversion\TestJsonConvert.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\TestObjectConstructor.cs" />
<Compile Include="Json\TestFreeJsonMemberReadingHelper.cs" />
<Compile Include="Json\TestJsonText.cs" />
<Compile Include="Reflection\TestReflector.cs" />

==============================================================================
Revision: 6bdef0e5dc01
Author: azizatif
Date: Thu Apr 21 15:20:57 2011
Log: Linked object construction into ImportContext and
CompontentImporter and also fixed a bug in JsonBufferReader as a result of
testing the change.
http://code.google.com/p/jayrock/source/detail?r=6bdef0e5dc01

Modified:
/src/Jayrock.Json/Json/Conversion/Converters/ComponentImporter.cs
/src/Jayrock.Json/Json/Conversion/ImportContext.cs
/src/Jayrock.Json/Json/JsonBufferReader.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/Converters/ComponentImporter.cs Fri
Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/Json/Conversion/Converters/ComponentImporter.cs Thu
Apr 21 15:20:57 2011
@@ -35,11 +35,18 @@
{
private readonly PropertyDescriptorCollection _properties; //
TODO: Review thread-safety of PropertyDescriptorCollection
private readonly IObjectMemberImporter[] _importers;
+ private readonly IObjectConstructor _constructor;

public ComponentImporter(Type type) :
- this(type, null) {}
-
- public ComponentImporter(Type type, ICustomTypeDescriptor
typeDescriptor) :
+ this(type, null, null) {}
+
+ public ComponentImporter(Type type, ICustomTypeDescriptor
typeDescriptor) :
+ this(type, typeDescriptor, null) {}
+
+ public ComponentImporter(Type type, IObjectConstructor
constructor) :
+ this(type, null, constructor) {}
+
+ public ComponentImporter(Type type, ICustomTypeDescriptor
typeDescriptor, IObjectConstructor constructor) :
base(type)
{
if (typeDescriptor == null)
@@ -69,6 +76,8 @@

if (count > 0)
_importers = importers;
+
+ _constructor = constructor;
}

protected override object ImportFromObject(ImportContext context,
JsonReader reader)
@@ -76,9 +85,22 @@
Debug.Assert(context != null);
Debug.Assert(reader != null);

- reader.Read();
-
- object obj = Activator.CreateInstance(OutputType);
+ object obj;
+
+ if (_constructor != null)
+ {
+ ObjectConstructionResult result =
_constructor.CreateObject(context, reader);
+ obj = result.Object;
+ reader = result.TailReader;
+ reader.MoveToContent();
+ reader.Read();
+ }
+ else
+ {
+ reader.Read();
+ obj = Activator.CreateInstance(OutputType);
+ }
+
INonObjectMemberImporter otherImporter = obj as
INonObjectMemberImporter;

while (reader.TokenClass != JsonTokenClass.EndObject)
=======================================
--- /src/Jayrock.Json/Json/Conversion/ImportContext.cs Tue Apr 19 18:51:59
2011
+++ /src/Jayrock.Json/Json/Conversion/ImportContext.cs Thu Apr 21 15:20:57
2011
@@ -181,7 +181,7 @@

if ((type.IsPublic || type.IsNestedPublic) &&
!type.IsPrimitive &&
- (type.IsValueType ||
type.GetConstructor(Type.EmptyTypes) != null))
+ (type.IsValueType || type.GetConstructors().Length > 0))
{
if (type.IsValueType)
{
@@ -192,7 +192,7 @@
}
else
{
- return new ComponentImporter(type);
+ return new ComponentImporter(type, new
ObjectConstructor(type));
}
}

=======================================
--- /src/Jayrock.Json/Json/JsonBufferReader.cs Fri Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/Json/JsonBufferReader.cs Thu Apr 21 15:20:57 2011
@@ -85,6 +85,9 @@
{
if (EOF)
return JsonBuffer.Empty;
+
+ if (!_buffer.IsStructured)
+ return _buffer;

JsonTokenClass tokenClass = TokenClass;
if (tokenClass.IsTerminator || tokenClass ==
JsonTokenClass.Member)

==============================================================================
Revision: bcbb99a7593b
Author: azizatif
Date: Thu Apr 21 16:04:27 2011
Log: Broke out some types into their own files
http://code.google.com/p/jayrock/source/detail?r=bcbb99a7593b

Added:
/src/Jayrock.Json/Json/Conversion/IObjectSurrogateConstructor.cs
/src/Jayrock.Json/Json/Conversion/ObjectConstructionResult.cs
Modified:
/src/Jayrock.Json/Jayrock.Json.csproj
/src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs

=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/IObjectSurrogateConstructor.cs Thu
Apr 21 16:04:27 2011
@@ -0,0 +1,33 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ /// <summary>
+ /// Represents an object's surrogate constructor.
+ /// </summary>
+
+ public interface IObjectSurrogateConstructor
+ {
+ ObjectConstructionResult CreateObject(ImportContext context);
+ }
+}
=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructionResult.cs Thu Apr
21 16:04:27 2011
@@ -0,0 +1,53 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ #region Imports
+
+ using System;
+
+ #endregion
+
+ /// <summary>
+ /// Represents the object resulting from construction as well as
+ /// a tail holding any remainders.
+ /// </summary>
+
+ public sealed class ObjectConstructionResult
+ {
+ private readonly object _obj;
+ private readonly JsonReader _tail;
+
+ public ObjectConstructionResult(object obj, JsonReader tail)
+ {
+ if (obj == null) throw new ArgumentNullException("obj");
+ if (tail == null) throw new ArgumentNullException("tail");
+
+ _obj = obj;
+ _tail = tail;
+ }
+
+ public object Object { get { return _obj; } }
+ public JsonReader TailReader { get { return _tail; } }
+ }
+}
=======================================
--- /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 15:02:10 2011
+++ /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:04:27 2011
@@ -315,6 +315,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Json\Conversion\INonObjectMemberImporter.cs" />
+ <Compile Include="Json\Conversion\IObjectSurrogateConstructor.cs" />
<Compile Include="Json\Conversion\JsonConvert.cs">
<SubType>Code</SubType>
</Compile>
@@ -333,6 +334,7 @@
<Compile
Include="Json\Conversion\JsonMemberNamingConventionAttribute.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\ObjectConstructionResult.cs" />
<Compile Include="Json\Conversion\ObjectConstructor.cs" />
<Compile Include="Json\Conversion\PropertyCustomization.cs">
<SubType>Code</SubType>
=======================================
--- /src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs Thu Apr 21
15:02:10 2011
+++ /src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs Thu Apr 21
16:04:27 2011
@@ -22,8 +22,6 @@

namespace Jayrock.Json.Conversion
{
- using System;
-
/// <summary>
/// Represents an object constructor.
/// </summary>
@@ -32,27 +30,4 @@
{
ObjectConstructionResult CreateObject(ImportContext context,
JsonReader reader);
}
-
- public sealed class ObjectConstructionResult
- {
- private readonly object _obj;
- private readonly JsonReader _tailReader;
-
- public ObjectConstructionResult(object obj, JsonReader tailReader)
- {
- if (obj == null) throw new ArgumentNullException("obj");
- if (tailReader == null) throw new
ArgumentNullException("reader");
-
- _obj = obj;
- _tailReader = tailReader;
- }
-
- public object Object { get { return _obj; } }
- public JsonReader TailReader { get { return _tailReader; } }
- }
-
- public interface IObjectSurrogateConstructor
- {
- ObjectConstructionResult CreateObject(ImportContext context);
- }
-}
+}

==============================================================================
Revision: 0daa5084cd43
Author: azizatif
Date: Thu Apr 21 16:06:48 2011
Log: Added NamedJsonBuffer.ToObject helper to convert an array of
NamedJsonBuffer into a JsonBuffer that creates a JSON object out of
http://code.google.com/p/jayrock/source/detail?r=0daa5084cd43

Modified:
/src/Jayrock.Json/Json/NamedJsonBuffer.cs
/tests/Jayrock/Json/TestNamedJsonBuffer.cs

=======================================
--- /src/Jayrock.Json/Json/NamedJsonBuffer.cs Fri Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/Json/NamedJsonBuffer.cs Thu Apr 21 16:06:48 2011
@@ -69,7 +69,26 @@

public override string ToString()
{
- return IsEmpty ? string.Empty :
Mask.EmptyString(Name, "(anonymous)") + ": " + Buffer;
+ return IsEmpty ? String.Empty :
Mask.EmptyString(Name, "(anonymous)") + ": " + Buffer;
+ }
+
+ public static JsonBuffer ToObject(params NamedJsonBuffer[] members)
+ {
+ if (members == null)
+ throw new ArgumentNullException("members");
+
+ if (members.Length == 0)
+ return Conversion.StockJsonBuffers.EmptyObject;
+
+ JsonBufferWriter writer = new JsonBufferWriter();
+ writer.WriteStartObject();
+ foreach (NamedJsonBuffer member in members)
+ {
+ writer.WriteMember(member.Name);
+ writer.WriteFromReader(member.Buffer.CreateReader());
+ }
+ writer.WriteEndObject();
+ return writer.GetBuffer();
}
}
}
=======================================
--- /tests/Jayrock/Json/TestNamedJsonBuffer.cs Fri Apr 15 16:09:54 2011
+++ /tests/Jayrock/Json/TestNamedJsonBuffer.cs Thu Apr 21 16:06:48 2011
@@ -114,5 +114,20 @@
Assert.AreEqual("(anonymous): \"foo\"",
new NamedJsonBuffer(string.Empty,
JsonBuffer.From("foo")).ToString());
}
+
+ [Test]
+ public void ToObject()
+ {
+ JsonBuffer obj = NamedJsonBuffer.ToObject(
+ new NamedJsonBuffer("foo", JsonBuffer.From("bar")),
+ new NamedJsonBuffer("qux", JsonBuffer.From("quux")));
+ Assert.AreEqual(6, obj.Length);
+ Assert.AreEqual(JsonToken.Object(), obj[0]);
+ Assert.AreEqual(JsonToken.Member("foo"), obj[1]);
+ Assert.AreEqual(JsonToken.String("bar"), obj[2]);
+ Assert.AreEqual(JsonToken.Member("qux"), obj[3]);
+ Assert.AreEqual(JsonToken.String("quux"), obj[4]);
+ Assert.AreEqual(JsonToken.EndObject(), obj[5]);
+ }
}
}

==============================================================================
Revision: f3f9caeb21be
Author: azizatif
Date: Thu Apr 21 16:10:33 2011
Log: Updated ObjectConstructor to support the default constructor that
is silently available for all value types
http://code.google.com/p/jayrock/source/detail?r=f3f9caeb21be

Modified:
/src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs
/tests/Jayrock/Json/Conversion/TestObjectConstructor.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
15:02:10 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
16:10:33 2011
@@ -34,6 +34,7 @@

public sealed class ObjectConstructor : IObjectConstructor
{
+ private readonly Type _type;
private readonly ConstructorInfo[] _ctors;

private static readonly IComparer _arrayLengthComparer = new
ReverseComparer(new DelegatingComparer(GetParametersCount));
@@ -59,9 +60,18 @@
ctors = (ConstructorInfo[]) ctors.Clone();
}

- if (ctors.Length == 0)
+ if (type.IsClass && ctors.Length == 0)
+ {
+ //
+ // Value types are excluded here because they always have
+ // a default constructor available but one which does not
+ // show up in reflection.
+ //
+
throw new ArgumentException(null, "ctors");
-
+ }
+
+ _type = type;
_ctors = ctors;
Array.Sort(_ctors, _arrayLengthComparer);
}
@@ -79,11 +89,27 @@
if (context == null) throw new
ArgumentNullException("context");
if (members == null) throw new
ArgumentNullException("members");

- foreach (ConstructorInfo ctor in _ctors)
- {
- ObjectConstructionResult result = TryCreateObject(context,
ctor, members);
- if (result != null)
- return result;
+ if (_ctors.Length > 0)
+ {
+ foreach (ConstructorInfo ctor in _ctors)
+ {
+ ObjectConstructionResult result =
TryCreateObject(context, ctor, members);
+ if (result != null)
+ return result;
+ }
+ }
+
+ if (_type.IsValueType)
+ {
+ //
+ // Value types always have a default constructor available
+ // but one which does not show up in reflection. If no
other
+ // constructors matched then just use the default one.
+ //
+
+ object obj = Activator.CreateInstance(_type);
+ JsonReader tail =
NamedJsonBuffer.ToObject(members).CreateReader();
+ return new ObjectConstructionResult(obj, tail);
}

throw new JsonException(string.Format("None constructor could
be used to create {0} object from JSON.", _ctors[0].DeclaringType));
=======================================
--- /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
15:02:10 2011
+++ /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
16:10:33 2011
@@ -203,5 +203,20 @@
public IDictionary Object { get { return _obj; } }
public ConstructorInfo Constructor { get { return _ctor; } }
}
+
+ [Test]
+ public void StructTypeWithDefaultConstructorConstruction()
+ {
+ ObjectConstructor constructor = new
ObjectConstructor(typeof(StructThing));
+ ObjectConstructionResult result = constructor.CreateObject(new
ImportContext(), JsonText.CreateReader("{foo:bar}"));
+ Assert.IsInstanceOfType(typeof(StructThing), result.Object);
+ JsonReader tail = result.TailReader;
+ tail.ReadToken(JsonTokenClass.Object);
+ Assert.AreEqual("foo", tail.ReadMember());
+ Assert.AreEqual("bar", tail.ReadString());
+ tail.ReadToken(JsonTokenClass.EndObject);
+ }
+
+ struct StructThing {}
}
}

==============================================================================
Revision: 97185b7750fc
Author: azizatif
Date: Thu Apr 21 16:11:23 2011
Log: Fixed typo
http://code.google.com/p/jayrock/source/detail?r=97185b7750fc

Modified:
/src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
16:10:33 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
16:11:23 2011
@@ -177,7 +177,7 @@

/// <remarks>
/// Bound indicies returned in the resulting array are one-based
- /// therefore zero mean unbound.
+ /// therefore zero means unbound.
/// </remarks>

private static int[] Bind(ImportContext context, ParameterInfo[]
parameters, NamedJsonBuffer[] members)

==============================================================================
Revision: b3dcb393bd4e
Author: azizatif
Date: Thu Apr 21 16:12:53 2011
Log: Renamed test for consistency
http://code.google.com/p/jayrock/source/detail?r=b3dcb393bd4e

Modified:
/tests/Jayrock/Json/Conversion/TestObjectConstructor.cs

=======================================
--- /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
16:10:33 2011
+++ /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
16:12:53 2011
@@ -205,11 +205,11 @@
}

[Test]
- public void StructTypeWithDefaultConstructorConstruction()
- {
- ObjectConstructor constructor = new
ObjectConstructor(typeof(StructThing));
+ public void ValueTypeWithDefaultConstructorConstruction()
+ {
+ ObjectConstructor constructor = new
ObjectConstructor(typeof(ValueThing));
ObjectConstructionResult result = constructor.CreateObject(new
ImportContext(), JsonText.CreateReader("{foo:bar}"));
- Assert.IsInstanceOfType(typeof(StructThing), result.Object);
+ Assert.IsInstanceOfType(typeof(ValueThing), result.Object);
JsonReader tail = result.TailReader;
tail.ReadToken(JsonTokenClass.Object);
Assert.AreEqual("foo", tail.ReadMember());
@@ -217,6 +217,6 @@
tail.ReadToken(JsonTokenClass.EndObject);
}

- struct StructThing {}
+ struct ValueThing {}
}
}

==============================================================================
Revision: 45d93e6db249
Author: azizatif
Date: Thu Apr 21 16:15:22 2011
Log: Updated ImportContext.FindCompatibleImporter to no longer exclude
the case of value types without properties since the rest of the
infrastructure, especially with respect to object construction, can support
it now
http://code.google.com/p/jayrock/source/detail?r=45d93e6db249

Modified:
/src/Jayrock.Json/Json/Conversion/ImportContext.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ImportContext.cs Thu Apr 21 15:20:57
2011
+++ /src/Jayrock.Json/Json/Conversion/ImportContext.cs Thu Apr 21 16:15:22
2011
@@ -183,17 +183,7 @@
!type.IsPrimitive &&
(type.IsValueType || type.GetConstructors().Length > 0))
{
- if (type.IsValueType)
- {
- CustomTypeDescriptor logicalType = new
CustomTypeDescriptor(type);
-
- if (logicalType.GetProperties().Count > 0)
- return new ComponentImporter(type, logicalType);
- }
- else
- {
- return new ComponentImporter(type, new
ObjectConstructor(type));
- }
+ return new ComponentImporter(type, new
ObjectConstructor(type));
}

return null;

==============================================================================
Revision: c756eb01921c
Author: azizatif
Date: Thu Apr 21 16:25:52 2011
Log: Moved StockJsonBuffers to the correct namespace
http://code.google.com/p/jayrock/source/detail?r=c756eb01921c

Added:
/src/Jayrock.Json/Json/StockJsonBuffers.cs
Deleted:
/src/Jayrock.Json/Json/Conversion/StockJsonBuffers.cs
Modified:
/src/Jayrock.Json/Jayrock.Json.csproj
/src/Jayrock.Json/Json/NamedJsonBuffer.cs

=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/StockJsonBuffers.cs Thu Apr 21 16:25:52 2011
@@ -0,0 +1,37 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json
+{
+ public sealed class StockJsonBuffers
+ {
+ public static readonly JsonBuffer EmptyObject;
+
+ static StockJsonBuffers()
+ {
+ JsonBufferWriter writer = new JsonBufferWriter(2);
+ writer.WriteStartObject();
+ writer.WriteEndObject();
+ EmptyObject = writer.GetBuffer();
+ }
+ }
+}
=======================================
--- /src/Jayrock.Json/Json/Conversion/StockJsonBuffers.cs Thu Apr 21
15:02:10 2011
+++ /dev/null
@@ -1,37 +0,0 @@
-#region License, Terms and Conditions
-//
-// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
-// Written by Atif Aziz (www.raboof.com)
-// Copyright (c) 2005 Atif Aziz. All rights reserved.
-//
-// This library is free software; you can redistribute it and/or modify it
under
-// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
-// any later version.
-//
-// This library is distributed in the hope that it will be useful, but
WITHOUT
-// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
-// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
-// details.
-//
-// You should have received a copy of the GNU Lesser General Public License
-// along with this library; if not, write to the Free Software Foundation,
Inc.,
-// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
-#endregion
-
-namespace Jayrock.Json.Conversion
-{
- public sealed class StockJsonBuffers
- {
- public static readonly JsonBuffer EmptyObject;
-
- static StockJsonBuffers()
- {
- JsonBufferWriter writer = new JsonBufferWriter(2);
- writer.WriteStartObject();
- writer.WriteEndObject();
- EmptyObject = writer.GetBuffer();
- }
- }
-}
=======================================
--- /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:04:27 2011
+++ /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:25:52 2011
@@ -339,7 +339,6 @@
<Compile Include="Json\Conversion\PropertyCustomization.cs">
<SubType>Code</SubType>
</Compile>
- <Compile Include="Json\Conversion\StockJsonBuffers.cs" />
<Compile Include="Json\EmptyJsonWriter.cs">
<SubType>Code</SubType>
</Compile>
@@ -409,6 +408,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Json\NamedJsonBuffer.cs" />
+ <Compile Include="Json\StockJsonBuffers.cs" />
<Compile Include="Reflection\Reflector.cs" />
<Compile Include="TypeResolution.cs" />
<Compile Include="UnixTime.cs">
=======================================
--- /src/Jayrock.Json/Json/NamedJsonBuffer.cs Thu Apr 21 16:06:48 2011
+++ /src/Jayrock.Json/Json/NamedJsonBuffer.cs Thu Apr 21 16:25:52 2011
@@ -78,7 +78,7 @@
throw new ArgumentNullException("members");

if (members.Length == 0)
- return Conversion.StockJsonBuffers.EmptyObject;
+ return StockJsonBuffers.EmptyObject;

JsonBufferWriter writer = new JsonBufferWriter();
writer.WriteStartObject();

==============================================================================
Revision: 4b50fa0775d3
Author: azizatif
Date: Thu Apr 21 16:29:25 2011
Log: Added ObjectSurrogateConstructor that can be used to construct an
object via a surrogate type
http://code.google.com/p/jayrock/source/detail?r=4b50fa0775d3

Added:
/src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructor.cs
/tests/Jayrock/Json/TestObjectSurrogateConstructor.cs
Modified:
/src/Jayrock.Json/Jayrock.Json.csproj
/tests/Jayrock/Jayrock.Tests.csproj

=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructor.cs Thu Apr
21 16:29:25 2011
@@ -0,0 +1,64 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ #region Imports
+
+ using System;
+
+ #endregion
+
+ public class ObjectSurrogateConstructor : IObjectConstructor
+ {
+ private readonly Type _surrogateType;
+
+ public ObjectSurrogateConstructor(Type surrogateType)
+ {
+ if (surrogateType == null)
+ throw new ArgumentNullException("surrogateType");
+
+ if
(!typeof(IObjectSurrogateConstructor).IsAssignableFrom(surrogateType))
+ {
+ throw new ArgumentException(string.Format(
+ "Surrogate type must implement {0} whereas {1} does
not.",
+ typeof (IObjectSurrogateConstructor),
surrogateType),
+ "surrogateType");
+ }
+
+ _surrogateType = surrogateType;
+ }
+
+ public Type SurrogateType { get { return _surrogateType; } }
+
+ public virtual ObjectConstructionResult CreateObject(ImportContext
context, JsonReader reader)
+ {
+ if (context == null)
+ throw new ArgumentNullException("context");
+ if (reader == null)
+ throw new ArgumentNullException("reader");
+
+ IObjectSurrogateConstructor ctor =
(IObjectSurrogateConstructor) context.Import(_surrogateType, reader);
+ return ctor.CreateObject(context);
+ }
+ }
+}
=======================================
--- /dev/null
+++ /tests/Jayrock/Json/TestObjectSurrogateConstructor.cs Thu Apr 21
16:29:25 2011
@@ -0,0 +1,91 @@
+#region License, Terms and Conditions
+//
+// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json
+{
+ #region Imports
+
+ using System;
+ using Conversion;
+ using NUnit.Framework;
+
+ #endregion
+
+ [ TestFixture ]
+ public class TestObjectSurrogateConstructor
+ {
+ [ Test, ExpectedException(typeof(ArgumentNullException)) ]
+ public void CannotInitializeWithNullSurrogateType()
+ {
+ new ObjectSurrogateConstructor(null);
+ }
+
+ [Test, ExpectedException(typeof(ArgumentException))]
+ public void
CannotInitializeWithNonSurrogateTypeNotImpementingIObjectSurrogateConstructor()
+ {
+ new ObjectSurrogateConstructor(typeof(object));
+ }
+
+ [Test]
+ public void SurrogateTypeInitialization()
+ {
+ ObjectSurrogateConstructor ctor = new
ObjectSurrogateConstructor(typeof(Surrogate));
+ Assert.AreEqual(typeof(Surrogate), ctor.SurrogateType);
+ }
+
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotCreateObjectWithNullImportContext()
+ {
+ ObjectSurrogateConstructor ctor = new
ObjectSurrogateConstructor(typeof(Surrogate));
+ ctor.CreateObject(null,
StockJsonBuffers.EmptyObject.CreateReader());
+ }
+
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotCreateObjectWithNullJsonReader()
+ {
+ ObjectSurrogateConstructor ctor = new
ObjectSurrogateConstructor(typeof(Surrogate));
+ ctor.CreateObject(new ImportContext(), null);
+ }
+
+ [Test]
+ public void CreateObject()
+ {
+ ObjectSurrogateConstructor ctor = new
ObjectSurrogateConstructor(typeof(Surrogate));
+ ImportContext context = new ImportContext();
+ ObjectConstructionResult result = ctor.CreateObject(context,
JsonText.CreateReader("{y:2000,m:12,d:4}"));
+ Assert.IsNotNull(result);
+ Assert.AreEqual(new DateTime(2000, 12, 4), result.Object);
+ }
+
+ public class Surrogate : IObjectSurrogateConstructor
+ {
+ [JsonMemberName("y")] public int Year;
+ [JsonMemberName("m")] public int Month;
+ [JsonMemberName("d")] public int Day;
+
+ public ObjectConstructionResult CreateObject(ImportContext
context)
+ {
+ return new ObjectConstructionResult(new DateTime(Year,
Month, Day), StockJsonBuffers.EmptyObject.CreateReader());
+ }
+ }
+ }
+}
=======================================
--- /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:25:52 2011
+++ /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:29:25 2011
@@ -334,6 +334,9 @@
<Compile
Include="Json\Conversion\JsonMemberNamingConventionAttribute.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Json\Conversion\ObjectSurrogateConstructor.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Json\Conversion\ObjectConstructionResult.cs" />
<Compile Include="Json\Conversion\ObjectConstructor.cs" />
<Compile Include="Json\Conversion\PropertyCustomization.cs">
=======================================
--- /tests/Jayrock/Jayrock.Tests.csproj Thu Apr 21 15:02:10 2011
+++ /tests/Jayrock/Jayrock.Tests.csproj Thu Apr 21 16:29:25 2011
@@ -117,6 +117,7 @@
<Compile Include="Json\Conversion\TestObjectConstructor.cs" />
<Compile Include="Json\TestFreeJsonMemberReadingHelper.cs" />
<Compile Include="Json\TestJsonText.cs" />
+ <Compile Include="Json\TestObjectSurrogateConstructor.cs" />
<Compile Include="Reflection\TestReflector.cs" />
<Compile Include="TestTypeResolution.cs" />
<Compile Include="JsonML\TestJsonMLCodec.cs" />

==============================================================================
Revision: 17dbcba8c1bd
Author: azizatif
Date: Thu Apr 21 16:35:33 2011
Log: Added a base implementation for surrogate object constructors
that principally takes care of tail handling
http://code.google.com/p/jayrock/source/detail?r=17dbcba8c1bd

Added:
/src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructorBase.cs
/tests/Jayrock/Json/TestObjectSurrogateConstructorBase.cs
Modified:
/src/Jayrock.Json/Jayrock.Json.csproj
/tests/Jayrock/Jayrock.Tests.csproj

=======================================
--- /dev/null
+++ /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructorBase.cs Thu
Apr 21 16:35:33 2011
@@ -0,0 +1,64 @@
+#region License, Terms and Conditions
+//
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json.Conversion
+{
+ using System;
+
+ public abstract class ObjectSurrogateConstructorBase :
+ IObjectSurrogateConstructor,
+ INonObjectMemberImporter
+ {
+ private JsonBufferWriter _tailw;
+
+ public virtual bool Import(ImportContext context, string name,
JsonReader reader)
+ {
+ if (context == null) throw new
ArgumentNullException("context");
+ if (reader == null) throw new ArgumentNullException("reader");
+
+ JsonBufferWriter tailw = _tailw;
+ if (tailw == null)
+ {
+ tailw = _tailw = new JsonBufferWriter();
+ tailw.WriteStartObject();
+ }
+ tailw.WriteMember(name);
+ tailw.WriteFromReader(reader);
+ return true;
+ }
+
+ public virtual ObjectConstructionResult CreateObject(ImportContext
context)
+ {
+ if (context == null) throw new
ArgumentNullException("context");
+
+ JsonBufferWriter tailw = _tailw;
+ _tailw = null;
+ if (tailw != null)
+ tailw.WriteEndObject();
+ JsonBuffer tail = (tailw != null ? tailw.GetBuffer() :
StockJsonBuffers.EmptyObject);
+ object obj = OnCreateObject(context);
+ return new ObjectConstructionResult(obj, tail.CreateReader());
+ }
+
+ public abstract object OnCreateObject(ImportContext context);
+ }
+}
=======================================
--- /dev/null
+++ /tests/Jayrock/Json/TestObjectSurrogateConstructorBase.cs Thu Apr 21
16:35:33 2011
@@ -0,0 +1,109 @@
+#region License, Terms and Conditions
+//
+// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
+// Written by Atif Aziz (www.raboof.com)
+// Copyright (c) Atif Aziz. All rights reserved.
+//
+// This library is free software; you can redistribute it and/or modify it
under
+// the terms of the GNU Lesser General Public License as published by the
Free
+// Software Foundation; either version 2.1 of the License, or (at your
option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful, but
WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with this library; if not, write to the Free Software Foundation,
Inc.,
+// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+#endregion
+
+namespace Jayrock.Json
+{
+ #region Imports
+
+ using System;
+ using Conversion;
+ using NUnit.Framework;
+
+ #endregion
+
+ [ TestFixture ]
+ public class TestObjectSurrogateConstructorBase
+ {
+ [ Test ]
+ public void ImplementsIObjectSurrogateConstructor()
+ {
+ Assert.IsInstanceOfType(typeof(IObjectSurrogateConstructor),
new Surrogate(new object()));
+ }
+
+ [Test]
+ public void ImplementsINonObjectMemberImporter()
+ {
+ Assert.IsInstanceOfType(typeof(INonObjectMemberImporter), new
Surrogate(new object()));
+ }
+
+ [Test]
+ public void ImportNonObjectMember()
+ {
+ Surrogate surrogate = new Surrogate(new object());
+ ImportContext context = new ImportContext();
+ surrogate.Import(context, "foo", JsonText.CreateReader("bar"));
+ JsonReader tail = surrogate.CreateObject(context).TailReader;
+ tail.ReadToken(JsonTokenClass.Object);
+ Assert.AreEqual("foo", tail.ReadMember());
+ Assert.AreEqual("bar", tail.ReadString());
+ tail.ReadToken(JsonTokenClass.EndObject);
+ }
+
+ [Test]
+ public void CreateObject()
+ {
+ object obj = new object();
+ Surrogate surrogate = new Surrogate(obj);
+ ImportContext context = new ImportContext();
+ ObjectConstructionResult result =
surrogate.CreateObject(context);
+ Assert.AreSame(obj, result.Object);
+ JsonReader tail = result.TailReader;
+ tail.ReadToken(JsonTokenClass.Object);
+ tail.ReadToken(JsonTokenClass.EndObject);
+ }
+
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotImportNonObjectMemberWithNullContext()
+ {
+ Surrogate surrogate = new Surrogate(new object());
+ surrogate.Import(null, "foo",
StockJsonBuffers.EmptyObject.CreateReader());
+ }
+
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotImportNonObjectMemberWithNullReader()
+ {
+ Surrogate surrogate = new Surrogate(new object());
+ surrogate.Import(new ImportContext(), "foo", null);
+ }
+
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void CannotCreateObjectWithNullContext()
+ {
+ new Surrogate(new object()).CreateObject(null);
+ }
+
+ class Surrogate : ObjectSurrogateConstructorBase
+ {
+ public object _obj;
+
+ public Surrogate(object obj)
+ {
+ _obj = obj;
+ }
+
+ public override object OnCreateObject(ImportContext context)
+ {
+ return _obj;
+ }
+ }
+ }
+}
=======================================
--- /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:29:25 2011
+++ /src/Jayrock.Json/Jayrock.Json.csproj Thu Apr 21 16:35:33 2011
@@ -339,6 +339,9 @@
</Compile>
<Compile Include="Json\Conversion\ObjectConstructionResult.cs" />
<Compile Include="Json\Conversion\ObjectConstructor.cs" />
+ <Compile Include="Json\Conversion\ObjectSurrogateConstructorBase.cs">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="Json\Conversion\PropertyCustomization.cs">
<SubType>Code</SubType>
</Compile>
=======================================
--- /tests/Jayrock/Jayrock.Tests.csproj Thu Apr 21 16:29:25 2011
+++ /tests/Jayrock/Jayrock.Tests.csproj Thu Apr 21 16:35:33 2011
@@ -118,6 +118,7 @@
<Compile Include="Json\TestFreeJsonMemberReadingHelper.cs" />
<Compile Include="Json\TestJsonText.cs" />
<Compile Include="Json\TestObjectSurrogateConstructor.cs" />
+ <Compile Include="Json\TestObjectSurrogateConstructorBase.cs" />
<Compile Include="Reflection\TestReflector.cs" />
<Compile Include="TestTypeResolution.cs" />
<Compile Include="JsonML\TestJsonMLCodec.cs" />

==============================================================================
Revision: a78cc21ac2a3
Author: azizatif
Date: Thu Apr 21 16:38:50 2011
Log: Aligned ExportContext.FindExporter with ImportContext.FindImporter
http://code.google.com/p/jayrock/source/detail?r=a78cc21ac2a3

Modified:
/src/Jayrock.Json/Json/Conversion/ExportContext.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ExportContext.cs Fri Apr 15 16:09:54
2011
+++ /src/Jayrock.Json/Json/Conversion/ExportContext.cs Thu Apr 21 16:38:50
2011
@@ -140,19 +140,9 @@

if ((type.IsPublic || type.IsNestedPublic) &&
!type.IsPrimitive && !type.IsEnum &&
- (type.IsValueType ||
type.GetConstructor(Type.EmptyTypes) != null))
- {
- if (type.IsValueType)
- {
- CustomTypeDescriptor descriptor = new
CustomTypeDescriptor(type);
-
- if (descriptor.GetProperties().Count > 0)
- return new ComponentExporter(type, descriptor);
- }
- else
- {
- return new ComponentExporter(type);
- }
+ (type.IsValueType || type.GetConstructors().Length > 0))
+ {
+ return new ComponentExporter(type);
}

CustomTypeDescriptor anonymousClass =
CustomTypeDescriptor.TryCreateForAnonymousClass(type);
@@ -222,6 +212,8 @@
exporters.Add(new DataRowExporter());
exporters.Add(new DbDataRecordExporter());
exporters.Add(new ControlExporter());
+ exporters.Add(new StringExporter(typeof(Uri)));
+ exporters.Add(new StringExporter(typeof(Guid)));

#if !NET_1_0 && !NET_1_1 && !NET_2_0


==============================================================================
Revision: 8df6e4f875e8
Author: azizatif
Date: Thu Apr 21 16:41:04 2011
Log: Optimized away array creation in ComponentExporter to fault-in
later when needed
http://code.google.com/p/jayrock/source/detail?r=8df6e4f875e8

Modified:
/src/Jayrock.Json/Json/Conversion/Converters/ComponentExporter.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/Converters/ComponentExporter.cs Fri
Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/Json/Conversion/Converters/ComponentExporter.cs Thu
Apr 21 16:41:04 2011
@@ -41,8 +41,9 @@
this(inputType, (ICustomTypeDescriptor) null) {}

public ComponentExporter(Type inputType, ICustomTypeDescriptor
typeDescriptor) :
- this(inputType, typeDescriptor != null ?
- typeDescriptor.GetProperties() : (new
CustomTypeDescriptor(inputType)).GetProperties()) {}
+ this(inputType, typeDescriptor != null
+ ? typeDescriptor.GetProperties()
+ : (new
CustomTypeDescriptor(inputType)).GetProperties()) {}

private ComponentExporter(Type inputType,
PropertyDescriptorCollection properties) :
base(inputType)
@@ -52,7 +53,7 @@
_properties = properties;

int count = 0;
- IObjectMemberExporter[] exporters = new
IObjectMemberExporter[properties.Count];
+ IObjectMemberExporter[] exporters = null;

for (int i = 0; i < properties.Count; i++)
{
@@ -66,6 +67,9 @@
if (exporter == null)
continue;

+ if (exporters == null) // fault
+ exporters = new
IObjectMemberExporter[properties.Count];
+
exporters[i] = exporter;
count++;
}

==============================================================================
Revision: 27aff804a7a4
Author: azizatif
Date: Fri Apr 22 02:54:28 2011
Log: Fixed C# 1 compilation issue ObjectConstructor; need to spell out
delegate creation
http://code.google.com/p/jayrock/source/detail?r=27aff804a7a4

Modified:
/src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Thu Apr 21
16:11:23 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Fri Apr 22
02:54:28 2011
@@ -36,8 +36,8 @@
{
private readonly Type _type;
private readonly ConstructorInfo[] _ctors;
-
- private static readonly IComparer _arrayLengthComparer = new
ReverseComparer(new DelegatingComparer(GetParametersCount));
+
+ private static readonly IComparer _arrayLengthComparer = new
ReverseComparer(new DelegatingComparer(new
ComparableSelector(GetParametersCount)));

public ObjectConstructor(Type type) : this(type, null) {}


==============================================================================
Revision: 78e0011b2ec7
Author: azizatif
Date: Sun Apr 24 08:09:46 2011
Log: Added anonymous type support to ImportContext for sake of
symmetry with ExportContext
http://code.google.com/p/jayrock/source/detail?r=78e0011b2ec7

Modified:
/src/Jayrock.Json/Json/Conversion/ImportContext.cs

=======================================
--- /src/Jayrock.Json/Json/Conversion/ImportContext.cs Thu Apr 21 16:15:22
2011
+++ /src/Jayrock.Json/Json/Conversion/ImportContext.cs Sun Apr 24 08:09:46
2011
@@ -185,6 +185,10 @@
{
return new ComponentImporter(type, new
ObjectConstructor(type));
}
+
+ CustomTypeDescriptor anonymousClass =
CustomTypeDescriptor.TryCreateForAnonymousClass(type);
+ if (anonymousClass != null)
+ return new ComponentImporter(type, anonymousClass, new
ObjectConstructor(type));

return null;
}

==============================================================================
Revision: 7ab23338fb85
Author: azizatif
Date: Tue Apr 26 18:08:56 2011
Log: Updated file license headers
http://code.google.com/p/jayrock/source/detail?r=7ab23338fb85

Modified:
/src/Jayrock.Json/Comparers.cs
/src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs
/src/Jayrock.Json/Json/Conversion/IObjectSurrogateConstructor.cs
/src/Jayrock.Json/Json/Conversion/ObjectConstructionResult.cs
/src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs
/src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructor.cs
/src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructorBase.cs
/src/Jayrock.Json/Json/StockJsonBuffers.cs
/tests/Jayrock/Json/Conversion/TestObjectConstructor.cs
/tests/Jayrock/Json/TestObjectSurrogateConstructor.cs
/tests/Jayrock/Json/TestObjectSurrogateConstructorBase.cs

=======================================
--- /src/Jayrock.Json/Comparers.cs Thu Apr 21 15:02:10 2011
+++ /src/Jayrock.Json/Comparers.cs Tue Apr 26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs Thu Apr 21
16:04:27 2011
+++ /src/Jayrock.Json/Json/Conversion/IObjectConstructor.cs Tue Apr 26
18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/IObjectSurrogateConstructor.cs Thu
Apr 21 16:04:27 2011
+++ /src/Jayrock.Json/Json/Conversion/IObjectSurrogateConstructor.cs Tue
Apr 26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectConstructionResult.cs Thu Apr
21 16:04:27 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructionResult.cs Tue Apr
26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Fri Apr 22
02:54:28 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectConstructor.cs Tue Apr 26
18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructor.cs Thu Apr
21 16:29:25 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructor.cs Tue Apr
26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructorBase.cs Thu
Apr 21 16:35:33 2011
+++ /src/Jayrock.Json/Json/Conversion/ObjectSurrogateConstructorBase.cs Tue
Apr 26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /src/Jayrock.Json/Json/StockJsonBuffers.cs Thu Apr 21 16:25:52 2011
+++ /src/Jayrock.Json/Json/StockJsonBuffers.cs Tue Apr 26 18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Thu Apr 21
16:12:53 2011
+++ /tests/Jayrock/Json/Conversion/TestObjectConstructor.cs Tue Apr 26
18:08:56 2011
@@ -6,7 +6,7 @@
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /tests/Jayrock/Json/TestObjectSurrogateConstructor.cs Thu Apr 21
16:29:25 2011
+++ /tests/Jayrock/Json/TestObjectSurrogateConstructor.cs Tue Apr 26
18:08:56 2011
@@ -1,12 +1,12 @@
#region License, Terms and Conditions
//
-// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
// Written by Atif Aziz (www.raboof.com)
-// Copyright (c) Atif Aziz. All rights reserved.
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT
=======================================
--- /tests/Jayrock/Json/TestObjectSurrogateConstructorBase.cs Thu Apr 21
16:35:33 2011
+++ /tests/Jayrock/Json/TestObjectSurrogateConstructorBase.cs Tue Apr 26
18:08:56 2011
@@ -1,12 +1,12 @@
#region License, Terms and Conditions
//
-// Jayrock - A JSON-RPC implementation for the Microsoft .NET Framework
+// Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
// Written by Atif Aziz (www.raboof.com)
-// Copyright (c) Atif Aziz. All rights reserved.
+// Copyright (c) 2005 Atif Aziz. All rights reserved.
//
// This library is free software; you can redistribute it and/or modify it
under
// the terms of the GNU Lesser General Public License as published by the
Free
-// Software Foundation; either version 2.1 of the License, or (at your
option)
+// Software Foundation; either version 3 of the License, or (at your
option)
// any later version.
//
// This library is distributed in the hope that it will be useful, but
WITHOUT

==============================================================================
Revision: 195961addddb
Author: azizatif
Date: Tue Apr 26 18:35:26 2011
Log: Chained JsonMemberNamingConvention constructors to avoid
duplication
http://code.google.com/p/jayrock/source/detail?r=195961addddb

Modified:
/src/Jayrock.Json/Json/Conversion/JsonMemberNamingConventionAttribute.cs

=======================================
---
/src/Jayrock.Json/Json/Conversion/JsonMemberNamingConventionAttribute.cs
Sun Apr 17 16:27:53 2011
+++
/src/Jayrock.Json/Json/Conversion/JsonMemberNamingConventionAttribute.cs
Tue Apr 26 18:35:26 2011
@@ -55,12 +55,11 @@
private NamingConvention _convention;
private UnderscoreConvention _underscores;

- public JsonMemberNamingConventionAttribute() {}
-
- public JsonMemberNamingConventionAttribute(NamingConvention
convention)
- {
- _convention = convention;
- }
+ public JsonMemberNamingConventionAttribute() :
+ this(NamingConvention.None) {}
+
+ public JsonMemberNamingConventionAttribute(NamingConvention
convention) :
+ this(convention, UnderscoreConvention.None) {}

public JsonMemberNamingConventionAttribute(NamingConvention
naming, UnderscoreConvention underscores)
{
@@ -74,7 +73,8 @@
set { _convention = value; }
}

- public UnderscoreConvention Underscores {
+ public UnderscoreConvention Underscores
+ {
get { return _underscores; }
set { _underscores = value; }
}

==============================================================================
Revision: 2fba3ebf9389
Author: azizatif
Date: Fri Apr 29 12:29:08 2011
Log: Rendered JsonTokenClass.All truly read-only
http://code.google.com/p/jayrock/source/detail?r=2fba3ebf9389

Modified:
/src/Jayrock.Json/Json/JsonTokenClass.cs
/tests/Jayrock/Json/TestJsonTokenClass.cs

=======================================
--- /src/Jayrock.Json/Json/JsonTokenClass.cs Fri Apr 15 16:09:54 2011
+++ /src/Jayrock.Json/Json/JsonTokenClass.cs Fri Apr 29 12:29:08 2011
@@ -46,7 +46,13 @@
public static readonly JsonTokenClass BOF = new
JsonTokenClass("BOF", Superclass.Terminator);
public static readonly JsonTokenClass EOF = new
JsonTokenClass("EOF", Superclass.Terminator);

- public static readonly ICollection All = new JsonTokenClass[] {
BOF, EOF, Null, Boolean, Number, String, Array, EndArray, Object,
EndObject, Member };
+ public static readonly ICollection All = ArrayList.ReadOnly(new
JsonTokenClass[]
+ {
+ BOF, EOF,
+ Null, Boolean, Number, String,
+ Array, EndArray,
+ Object, EndObject, Member
+ });

private readonly string _name;
[ NonSerialized ] private readonly Superclass _superclass;
=======================================
--- /tests/Jayrock/Json/TestJsonTokenClass.cs Fri Apr 15 16:09:54 2011
+++ /tests/Jayrock/Json/TestJsonTokenClass.cs Fri Apr 29 12:29:08 2011
@@ -62,7 +62,19 @@
foreach (JsonTokenClass tokenClass in JsonTokenClass.All)

Assert.IsTrue(tokenClass.GetHashCode().Equals(tokenClass.Name.GetHashCode()));
}
-
+
+ [ Test ]
+ public void AllIsReadOnly()
+ {
+ Assert.IsTrue(((IList) JsonTokenClass.All).IsReadOnly);
+ }
+
+ [ Test ]
+ public void AllIsFixedSize()
+ {
+ Assert.IsTrue(((IList) JsonTokenClass.All).IsFixedSize);
+ }
+
[ Test ]
public void All()
{
Reply all
Reply to author
Forward
0 new messages