Author: atsushi
Date: 2008-01-24 09:17:52 -0500 (Thu, 24 Jan 2008)
New Revision: 93795
Modified:
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
Log:
2008-01-24 Atsushi Enomoto <[EMAIL PROTECTED]>
* JsonSerializationReader.cs : when deserializing primitive strings,
make use of "type" attribute (they are supposed to exist).
Output source reader location if available.
* JsonReader.cs : implement IXmlLineInfo.
* JsonReaderTest.cs : test "type" attribute in several nodes.
Modified:
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
===================================================================
---
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
2008-01-24 14:11:18 UTC (rev 93794)
+++
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
2008-01-24 14:17:52 UTC (rev 93795)
@@ -1,5 +1,12 @@
2008-01-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * JsonSerializationReader.cs : when deserializing primitive strings,
+ make use of "type" attribute (they are supposed to exist).
+ Output source reader location if available.
+ * JsonReader.cs : implement IXmlLineInfo.
+
+2008-01-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+
* JsonSerializationReader.cs : new, for JSON deserialization support.
* DataContractJsonSerializer.cs, TypeMap.cs :
basic support for deserialization.
Modified:
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
===================================================================
---
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
2008-01-24 14:11:18 UTC (rev 93794)
+++
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonReader.cs
2008-01-24 14:17:52 UTC (rev 93795)
@@ -35,7 +35,7 @@
namespace System.Runtime.Serialization.Json
{
// FIXME: quotas check
- class JsonReader : XmlDictionaryReader, IXmlJsonReaderInitializer
+ class JsonReader : XmlDictionaryReader, IXmlJsonReaderInitializer,
IXmlLineInfo
{
class ElementInfo
{
@@ -79,6 +79,21 @@
SetInput (stream, encoding, quotas, onClose);
}
+ // IXmlLineInfo
+
+ public bool HasLineInfo ()
+ {
+ return true;
+ }
+
+ public int LineNumber {
+ get { return line; }
+ }
+
+ public int LinePosition {
+ get { return column; }
+ }
+
// IXmlJsonReaderInitializer
public void SetInput (byte [] buffer, int offset, int count,
Encoding encoding, XmlDictionaryReaderQuotas quotas, OnXmlDictionaryReaderClose
onClose)
Modified:
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
===================================================================
---
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
2008-01-24 14:11:18 UTC (rev 93794)
+++
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
2008-01-24 14:17:52 UTC (rev 93795)
@@ -69,7 +69,7 @@
public object ReadObject (Type type)
{
if (serialized_object_count ++ ==
serializer.MaxItemsInObjectGraph)
- throw new SerializationException (String.Format
("The object graph exceeded the maximum object count '{0}' specified in the
serializer", serializer.MaxItemsInObjectGraph));
+ throw SerializationError (String.Format ("The
object graph exceeded the maximum object count '{0}' specified in the
serializer", serializer.MaxItemsInObjectGraph));
switch (Type.GetTypeCode (type)) {
case TypeCode.String:
@@ -132,38 +132,44 @@
object ReadInstanceDrivenObject ()
{
+ string type = reader.GetAttribute ("type");
string v = reader.ReadElementContentAsString ();
- if (v.Length == 0)
- return String.Empty;
- if (v [0] == '"') {
- if (v [v.Length - 1] != '"')
- throw new SerializationException ("JSON
string should terminate with '\"'");
- return v.Substring (1, v.Length - 2);
- }
- switch (v) {
- case "true":
- return true;
- case "false":
- return false;
+ switch (type) {
+ case "boolean":
+ switch (v) {
+ case "true":
+ return true;
+ case "false":
+ return false;
+ default:
+ throw SerializationError (String.Format
("Invalid JSON boolean value: {0}", v));
+ }
+ case "string":
+ return v;
case "null":
+ if (v != "null")
+ throw SerializationError (String.Format
("Invalid JSON null value: {0}", v));
return null;
+ case "number":
+ int i;
+ if (int.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out i))
+ return i;
+ long l;
+ if (long.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out l))
+ return l;
+ ulong ul;
+ if (ulong.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out ul))
+ return ul;
+ double dbl;
+ if (double.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out dbl))
+ return dbl;
+ decimal dec;
+ if (decimal.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out dec))
+ return dec;
+ throw SerializationError (String.Format
("Invalid JSON input: {0}", v));
+ default:
+ throw SerializationError (String.Format
("Unexpected type: {0}", type));
}
- int i;
- if (int.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out i))
- return i;
- long l;
- if (long.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out l))
- return l;
- ulong ul;
- if (ulong.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out ul))
- return ul;
- double dbl;
- if (double.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out dbl))
- return dbl;
- decimal dec;
- if (decimal.TryParse (v, NumberStyles.None,
CultureInfo.InvariantCulture, out dec))
- return dec;
- throw new SerializationException (String.Format
("Invalid JSON input: {0}", v));
}
string FormatTypeName (Type type)
@@ -204,7 +210,7 @@
(IList) Activator.CreateInstance
(collectionType);
for (reader.MoveToContent (); reader.NodeType
!= XmlNodeType.EndElement; reader.MoveToContent ()) {
if (!reader.IsStartElement ("item"))
- throw new
SerializationException (String.Format ("Expected element 'item', but found
'{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
+ throw SerializationError
(String.Format ("Expected element 'item', but found '{0}' in namespace '{1}'",
reader.LocalName, reader.NamespaceURI));
Type et = elementType == typeof
(object) || elementType.IsAbstract ? null : elementType;
if (et == null) {
string typeName =
reader.GetAttribute ("__type");
@@ -220,7 +226,7 @@
MethodInfo add = collectionType.GetMethod
("Add", new Type [] {elementType});
for (reader.MoveToContent (); reader.NodeType
!= XmlNodeType.EndElement; reader.MoveToContent ()) {
if (!reader.IsStartElement ("item"))
- throw new
SerializationException (String.Format ("Expected element 'item', but found
'{0}' in namespace '{1}'", reader.LocalName, reader.NamespaceURI));
+ throw SerializationError
(String.Format ("Expected element 'item', but found '{0}' in namespace '{1}'",
reader.LocalName, reader.NamespaceURI));
object elem = ReadObject (elementType);
add.Invoke (c, new object [] {elem});
}
@@ -240,5 +246,14 @@
}
return map;
}
+
+ Exception SerializationError (string basemsg)
+ {
+ IXmlLineInfo li = reader as IXmlLineInfo;
+ if (li == null || !li.HasLineInfo ())
+ return new SerializationException (basemsg);
+ else
+ return new SerializationException
(String.Format ("{0}. Error at {1} ({2},{3})", basemsg, reader.BaseURI,
li.LineNumber, li.LinePosition));
+ }
}
}
Modified:
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
===================================================================
---
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
2008-01-24 14:11:18 UTC (rev 93794)
+++
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
2008-01-24 14:17:52 UTC (rev 93795)
@@ -1,5 +1,9 @@
2008-01-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+ * JsonReaderTest.cs : test "type" attribute in several nodes.
+
+2008-01-24 Atsushi Enomoto <[EMAIL PROTECTED]>
+
* DataContractJsonSerializerTest.cs : some more deserialization tests.
2008-01-22 Atsushi Enomoto <[EMAIL PROTECTED]>
Modified:
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
===================================================================
---
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
2008-01-24 14:11:18 UTC (rev 93794)
+++
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/JsonReaderTest.cs
2008-01-24 14:17:52 UTC (rev 93795)
@@ -201,6 +201,7 @@
Assert.AreEqual (ReadState.Interactive,
reader.ReadState, "#2");
Assert.AreEqual (XmlNodeType.Element, reader.NodeType,
"#2-1");
Assert.AreEqual ("root", reader.LocalName, "#2-2");
+ Assert.AreEqual ("boolean", reader.GetAttribute
("type"), "#2-3");
reader.Read (); // content (boolean)
Assert.AreEqual (ReadState.Interactive,
reader.ReadState, "#3");
Assert.AreEqual (XmlNodeType.Text, reader.NodeType,
"#3-1");
@@ -242,6 +243,7 @@
Assert.AreEqual (ReadState.Interactive,
reader.ReadState, "#2");
Assert.AreEqual (XmlNodeType.Element, reader.NodeType,
"#2-1");
Assert.AreEqual ("root", reader.LocalName, "#2-2");
+ Assert.AreEqual ("string", reader.GetAttribute
("type"), "#2-3");
reader.Read (); // content (number)
Assert.AreEqual (ReadState.Interactive,
reader.ReadState, "#3");
Assert.AreEqual (XmlNodeType.Text, reader.NodeType,
"#3-1");
@@ -368,6 +370,7 @@
AssertNode (0, "root", XmlNodeType.Element,
String.Empty, "array", reader, "#1");
reader.Read (); // 123 - element
+ Assert.AreEqual ("number", reader.GetAttribute
("type"), "#2-0");
AssertNode (1, "item", XmlNodeType.Element,
String.Empty, "number", reader, "#2");
reader.Read (); // 123 - text
AssertNode (2, String.Empty, XmlNodeType.Text, "123",
null, reader, "#3");
@@ -376,6 +379,7 @@
// string value #1
reader.Read (); // "123" - element
+ Assert.AreEqual ("string", reader.GetAttribute
("type"), "#5-0");
AssertNode (1, "item", XmlNodeType.Element,
String.Empty, "string", reader, "#5");
reader.Read (); // "123" - text
AssertNode (2, String.Empty, XmlNodeType.Text, "123",
null, reader, "#6");
@@ -383,6 +387,7 @@
AssertNode (1, "item", XmlNodeType.EndElement,
String.Empty, null, reader, "#7");
reader.Read (); // true - element
+ Assert.AreEqual ("boolean", reader.GetAttribute
("type"), "#8-0");
AssertNode (1, "item", XmlNodeType.Element,
String.Empty, "boolean", reader, "#8");
reader.Read (); // true - text
AssertNode (2, String.Empty, XmlNodeType.Text, "true",
null, reader, "#9");
@@ -391,6 +396,7 @@
// string value #2
reader.Read (); // "true" - element
+ Assert.AreEqual ("string", reader.GetAttribute
("type"), "#11-0");
AssertNode (1, "item", XmlNodeType.Element,
String.Empty, "string", reader, "#11");
reader.Read (); // "true" - text
AssertNode (2, String.Empty, XmlNodeType.Text, "true",
null, reader, "#12");
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches