Author: atsushi
Date: 2008-02-18 03:35:18 -0500 (Mon, 18 Feb 2008)
New Revision: 96032

Added:
   
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
Modified:
   
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
   
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
   
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
   
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/ChangeLog
   
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/JsonQueryStringConverter.cs
   
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Web_test.dll.sources
   
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
   
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
   
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/ChangeLog
Log:
2008-02-18  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * DataContractJsonSerializer.cs : IsStartObject() could raise
          an arbitrary exception, so wrap it inside try-catch too to enclose
          with SerializationException.
        * JsonSerializationReader.cs : support DBNull.

        * JsonQueryStringConverter.cs : implemented.

        * DataContractJsonSerializerTest.cs : added couple of DBNull tests.

        * JsonQueryStringConverterTest.cs : new test.



Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
       2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/ChangeLog
       2008-02-18 08:35:18 UTC (rev 96032)
@@ -1,3 +1,10 @@
+2008-02-18  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * DataContractJsonSerializer.cs : IsStartObject() could raise
+         an arbitrary exception, so wrap it inside try-catch too to enclose
+         with SerializationException.
+       * JsonSerializationReader.cs : support DBNull.
+
 2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * DataContractJsonSerializer.cs : fixed .ctor(type, knonwTypes) that

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
   2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/DataContractJsonSerializer.cs
   2008-02-18 08:35:18 UTC (rev 96032)
@@ -172,10 +172,10 @@
                {
                        if (reader == null)
                                throw new ArgumentNullException ("reader");
-                       if (verifyObjectName && !IsStartObject (reader))
-                               throw new SerializationException (String.Format 
("Expected element was '{0}', but the actual input element was '{1}' in 
namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
+                       try {
+                               if (verifyObjectName && !IsStartObject (reader))
+                                       throw new SerializationException 
(String.Format ("Expected element was '{0}', but the actual input element was 
'{1}' in namespace '{2}'", root, reader.LocalName, reader.NamespaceURI));
 
-                       try {
                                return new JsonSerializationReader (this, 
reader, type, verifyObjectName).ReadRoot ();
                        } catch (SerializationException) {
                                throw;

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-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.Runtime.Serialization.Json/JsonSerializationReader.cs
      2008-02-18 08:35:18 UTC (rev 96032)
@@ -72,6 +72,11 @@
                                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.DBNull:
+                               string dbn = reader.ReadElementContentAsString 
();
+                               if (dbn != String.Empty)
+                                       throw new SerializationException 
(String.Format ("The only expected DBNull value string is '{{}}'. Tha actual 
input was '{0}'.", dbn));
+                               return DBNull.Value;
                        case TypeCode.String:
                                return reader.ReadElementContentAsString ();
                        case TypeCode.Single:

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/ChangeLog
  2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/ChangeLog
  2008-02-18 08:35:18 UTC (rev 96032)
@@ -1,3 +1,7 @@
+2008-02-18  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * JsonQueryStringConverter.cs : implemented.
+
 2008-02-17  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * WebMessageFormatter.cs : implement WebOperationContext support.

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/JsonQueryStringConverter.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/JsonQueryStringConverter.cs
        2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Dispatcher/JsonQueryStringConverter.cs
        2008-02-18 08:35:18 UTC (rev 96032)
@@ -26,29 +26,188 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 using System;
+using System.Globalization;
+using System.IO;
+using System.Runtime.Serialization.Json;
 using System.ServiceModel;
 using System.ServiceModel.Description;
+using System.Text;
+using System.Xml;
 
 namespace System.ServiceModel.Dispatcher
 {
        public class JsonQueryStringConverter : QueryStringConverter
        {
-               [MonoTODO]
+               DataContractJsonSerializer serializer = new 
DataContractJsonSerializer (typeof (object));
+
                public override bool CanConvert (Type type)
                {
-                       throw new NotImplementedException ();
+                       // almost copy from QueryStringConverter, except that 
DBNull and XmlQualifiedName are supported
+                       switch (Type.GetTypeCode (type)) {
+                       //case TypeCode.DBNull:
+                       case TypeCode.Empty:
+                               return false;
+                       case TypeCode.Object:
+                               if (type == typeof (TimeSpan))
+                                       return true;
+                               if (type == typeof (DateTimeOffset))
+                                       return true;
+                               if (type == typeof (Guid))
+                                       return true;
+                               if (type == typeof (XmlQualifiedName))
+                                       return true;
+                               if (type == typeof (object))
+                                       return true;
+//                             if (type.GetCustomAttributes (typeof 
(TypeConverterAttribute), true).Length > 0)
+//                                     return true;
+                               return false;
+                       default:
+                               return true;
+                       }
                }
 
-               [MonoTODO]
                public override object ConvertStringToValue (string parameter, 
Type parameterType)
                {
-                       throw new NotImplementedException ();
+                       if (parameterType == null)
+                               throw new ArgumentNullException 
("parameterType");
+
+                       if (!CanConvert (parameterType))
+                               throw new NotSupportedException (String.Format 
("Conversion from the argument parameterType '{0}' is not supported", 
parameterType));
+
+                       // In general .NET JSON parser is sloppy. It accepts 
+                       // such a string that is actually invalid in terms of
+                       // the target type in JSON context.
+
+                       switch (Type.GetTypeCode (parameterType)) {
+                       //case TypeCode.String:
+                       //      return parameter;
+                       case TypeCode.Char:
+                               return parameter != null ? Char.Parse 
(parameter): default (char);
+                       case TypeCode.SByte:
+                               return parameter != null ? SByte.Parse 
(parameter, CultureInfo.InvariantCulture): default (sbyte);
+                       case TypeCode.Byte:
+                               return parameter != null ? Byte.Parse 
(parameter, CultureInfo.InvariantCulture): default (byte);
+                       case TypeCode.Int16:
+                               return parameter != null ? Int16.Parse 
(parameter, CultureInfo.InvariantCulture): default (short);
+                       case TypeCode.Int32:
+                               return parameter != null ? Int32.Parse 
(parameter, CultureInfo.InvariantCulture): default (int);
+                       case TypeCode.Int64:
+                               return parameter != null ? Int64.Parse 
(parameter, CultureInfo.InvariantCulture): default (long);
+                       case TypeCode.UInt16:
+                               return parameter != null ? UInt16.Parse 
(parameter, CultureInfo.InvariantCulture): default (ushort);
+                       case TypeCode.UInt32:
+                               return parameter != null ? UInt32.Parse 
(parameter, CultureInfo.InvariantCulture): default (uint);
+                       case TypeCode.UInt64:
+                               return parameter != null ? UInt64.Parse 
(parameter, CultureInfo.InvariantCulture): default (ulong);
+                       case TypeCode.DateTime:
+                               return parameter != null ? DateTime.Parse 
(parameter, CultureInfo.InvariantCulture): default (DateTime);
+                       case TypeCode.Boolean:
+                               return parameter != null ? Boolean.Parse 
(parameter): default (bool);
+                       case TypeCode.Single:
+                               return parameter != null ? Single.Parse 
(parameter, CultureInfo.InvariantCulture): default (float);
+                       case TypeCode.Double:
+                               return parameter != null ? Double.Parse 
(parameter, CultureInfo.InvariantCulture): default (double);
+                       case TypeCode.Decimal:
+                               return parameter != null ? Decimal.Parse 
(parameter, CultureInfo.InvariantCulture): default (decimal);
+                       }
+
+                       if (parameter == null)
+                               return null;
+
+
+                       DataContractJsonSerializer serializer =
+                               new DataContractJsonSerializer (parameterType);
+                       // hmm, it costs so silly though.
+                       return serializer.ReadObject (new MemoryStream 
(Encoding.UTF8.GetBytes (parameter)));
                }
 
-               [MonoTODO]
+               bool IsKnownType (Type t)
+               {
+                       switch (Type.GetTypeCode (t)) {
+                       case TypeCode.Object:
+                               if (t == typeof (Guid) ||
+                                   t == typeof (DBNull) ||
+                                   t == typeof (DateTimeOffset) ||
+                                   t == typeof (TimeSpan) ||
+                                   t == typeof (XmlQualifiedName))
+                                       return true;
+                               return false;
+                       default:
+                               return true;
+                       }
+               }
+
                public override string ConvertValueToString (object parameter, 
Type parameterType)
                {
+                       if (parameterType == null)
+                               throw new ArgumentNullException 
("parameterType");
+
+                       if (!CanConvert (parameterType))
+                               throw new NotSupportedException (String.Format 
("Conversion from the argument parameterType '{0}' is not supported", 
parameterType));
+
+                       if (parameter == null)
+                               return null;
+
+                       if (parameter is DBNull)
+                               return "{}";
+
+                       parameterType = ToActualType (parameterType);
+
+                       if (parameter is IConvertible)
+                               parameter = ((IConvertible) parameter).ToType 
(parameterType, CultureInfo.InvariantCulture);
+
+                       switch (Type.GetTypeCode (parameterType)) {
+                       case TypeCode.String:
+                               string s = parameter is IFormattable ?
+                                       ((IFormattable) parameter).ToString 
(null, CultureInfo.InvariantCulture) :
+                                       parameter.ToString ();
+                               StringBuilder sb = new StringBuilder (s);
+                               sb.Replace ("\"", "\\\"");
+                               sb.Insert (0, '\"');
+                               sb.Append ('\"');
+                               return sb.ToString ();
+                       default:
+                               if (parameterType == typeof (XmlQualifiedName)) 
{
+                                       var qname = (XmlQualifiedName) 
parameter;
+                                       return String.Concat ("\"", qname.Name, 
":", qname.Namespace, "\"");
+                               }
+                               return parameter.ToString ();
+                       }
+
                        throw new NotImplementedException ();
                }
+
+               Type ToActualType (Type t)
+               {
+                       switch (Type.GetTypeCode (t)) {
+                       case TypeCode.DBNull: // though DBNull.Value input is 
converted to "{}". This result is used for String input.
+                       case TypeCode.Char:
+                       case TypeCode.String:
+                               return typeof (string);
+                       case TypeCode.SByte:
+                       case TypeCode.Int16:
+                       case TypeCode.Int32:
+                       case TypeCode.Int64:
+//                             return typeof (long);
+                               return typeof (decimal);
+                       case TypeCode.Byte:
+                       case TypeCode.UInt16:
+                       case TypeCode.UInt32:
+                       case TypeCode.UInt64:
+//                             return typeof (ulong);
+                               return typeof (decimal);
+                       case TypeCode.DateTime:
+                       case TypeCode.Boolean:
+                               return t;
+                       case TypeCode.Single:
+                       case TypeCode.Double:
+//                             return typeof (double);
+                               return typeof (decimal);
+                       case TypeCode.Decimal:
+                               return typeof (decimal);
+                       default:
+                               return t;
+                       }
+               }
        }
 }

Modified: 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Web_test.dll.sources
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Web_test.dll.sources
  2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/System.ServiceModel.Web_test.dll.sources
  2008-02-18 08:35:18 UTC (rev 96032)
@@ -4,6 +4,7 @@
 System.ServiceModel.Channels/WebBodyFormatMessagePropertyTest.cs
 System.ServiceModel.Channels/WebMessageEncodingBindingElementTest.cs
 System.ServiceModel.Description/WebHttpBehaviorTest.cs
+System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
 System.ServiceModel.Dispatcher/QueryStringConverterTest.cs
 System.ServiceModel.Dispatcher/WebHttpDispatchOperationSelectorTest.cs
 System.ServiceModel.Syndication/Atom10FeedFormatterTest.cs

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-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/ChangeLog
  2008-02-18 08:35:18 UTC (rev 96032)
@@ -1,3 +1,7 @@
+2008-02-18  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * DataContractJsonSerializerTest.cs : added couple of DBNull tests.
+
 2008-01-30  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * DataContractJsonSerializerTest.cs : added deserialization tests

Modified: 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
  2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.Runtime.Serialization.Json/DataContractJsonSerializerTest.cs
  2008-02-18 08:35:18 UTC (rev 96032)
@@ -305,6 +305,37 @@
                        }
                }
 
+               // DBNull (primitive)
+
+               [Test]
+               public void SerializeDBNullXml ()
+               {
+                       StringWriter sw = new StringWriter ();
+                       SerializeDBNull (XmlWriter.Create (sw, settings));
+                       Assert.AreEqual (
+                               @"<root type=""object"" />",
+                               sw.ToString ());
+               }
+
+               [Test]
+               public void SerializeDBNullJson ()
+               {
+                       MemoryStream ms = new MemoryStream ();
+                       SerializeDBNull 
(JsonReaderWriterFactory.CreateJsonWriter (ms));
+                       Assert.AreEqual (
+                               "{}",
+                               Encoding.UTF8.GetString (ms.ToArray ()));
+               }
+
+               void SerializeDBNull (XmlWriter writer)
+               {
+                       DataContractJsonSerializer ser =
+                               new DataContractJsonSerializer (typeof 
(DBNull));
+                       using (XmlWriter w = writer) {
+                               ser.WriteObject (w, DBNull.Value);
+                       }
+               }
+
                // DCSimple1
 
                [Test]
@@ -978,6 +1009,14 @@
                }
 
                [Test]
+               [ExpectedException (typeof (SerializationException))]
+               public void DeserializeEmptyAsDBNull ()
+               {
+                       // it somehow expects "root" which should have been 
already consumed.
+                       Deserialize ("", typeof (DBNull));
+               }
+
+               [Test]
                public void DeserializeEmptyObjectAsString ()
                {
                        // looks like it is converted to ""
@@ -988,10 +1027,16 @@
                [ExpectedException (typeof (SerializationException))]
                public void DeserializeEmptyObjectAsInt ()
                {
-                       Assert.AreEqual (0, Deserialize ("{}", typeof (int)));
+                       Deserialize ("{}", typeof (int));
                }
 
                [Test]
+               public void DeserializeEmptyObjectAsDBNull ()
+               {
+                       Assert.AreEqual (DBNull.Value, Deserialize ("{}", 
typeof (DBNull)));
+               }
+
+               [Test]
                [ExpectedException (typeof (SerializationException))]
                public void DeserializeEnumByName ()
                {

Modified: 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/ChangeLog
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/ChangeLog
     2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/ChangeLog
     2008-02-18 08:35:18 UTC (rev 96032)
@@ -1,3 +1,7 @@
+2008-02-18  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * JsonQueryStringConverterTest.cs : new test.
+
 2008-02-15  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * WebHttpDispatchOperationSelectorTest.cs : added couple more tests

Added: 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
===================================================================
--- 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
       2008-02-18 07:21:57 UTC (rev 96031)
+++ 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
       2008-02-18 08:35:18 UTC (rev 96032)
@@ -0,0 +1,282 @@
+//
+// JsonQueryStringConverterTest.cs
+//
+// Author:
+//     Atsushi Enomoto  <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;
+using System.ComponentModel;
+using System.Globalization;
+using System.Runtime.Serialization;
+using System.ServiceModel.Dispatcher;
+using System.Text;
+using System.Xml;
+using NUnit.Framework;
+
+using CategoryAttribute = NUnit.Framework.CategoryAttribute;
+
+namespace MonoTests.System.ServiceModel.Description
+{
+       [TestFixture]
+       public class JsonQueryStringConverterTest
+       {
+               JsonQueryStringConverter c;
+
+               [SetUp]
+               public void Setup ()
+               {
+                       c = new JsonQueryStringConverter ();
+               }
+
+               [Test]
+               public void CanConvert ()
+               {
+                       Assert.IsTrue (c.CanConvert (typeof (bool)), "#1");
+                       Assert.IsTrue (c.CanConvert (typeof (char)), "#2");
+                       Assert.IsTrue (c.CanConvert (typeof (double)), "#3");
+                       Assert.IsTrue (c.CanConvert (typeof (decimal)), "#4");
+                       Assert.IsTrue (c.CanConvert (typeof (float)), "#5");
+                       Assert.IsTrue (c.CanConvert (typeof (string)), "#6");
+                       Assert.IsTrue (c.CanConvert (typeof (int)), "#7");
+                       Assert.IsTrue (c.CanConvert (typeof (byte)), "#8");
+                       Assert.IsTrue (c.CanConvert (typeof (sbyte)), "#9");
+                       Assert.IsTrue (c.CanConvert (typeof (long)), "#10");
+                       Assert.IsTrue (c.CanConvert (typeof (ulong)), "#11");
+                       Assert.IsTrue (c.CanConvert (typeof (DateTime)), "#12");
+                       Assert.IsTrue (c.CanConvert (typeof (DateTimeOffset)), 
"#13");
+                       Assert.IsTrue (c.CanConvert (typeof (TimeSpan)), "#14");
+                       Assert.IsTrue (c.CanConvert (typeof (Guid)), "#15");
+                       Assert.IsTrue (c.CanConvert (typeof 
(XmlQualifiedName)), "#16");
+                       Assert.IsTrue (c.CanConvert (typeof (object)), "#17");
+                       Assert.IsFalse (c.CanConvert (typeof 
(QueryStringConverter)), "#18");
+                       // TypeConverterAttribute does not help it.
+                       Assert.IsFalse (c.CanConvert (typeof (MyConvertible)), 
"#19");
+               }
+
+               // ConvertStringToValue
+
+               [Test]
+               public void ConvertValueToStringValidCast ()
+               {
+                       Assert.AreEqual ("\"ABC\"", c.ConvertValueToString 
("ABC", typeof (char)));
+               }
+
+               [Test]
+               [Ignore ("huh? .NET converts to 123, not \"123\"")]
+               public void ConvertValueToStringValidCast2 ()
+               {
+                       Assert.AreEqual ("\"123\"", c.ConvertValueToString 
(123, typeof (string)));
+               }
+
+               [Test]
+               [Ignore ("huh? .NET converts to \"123\", not 123")]
+               public void ConvertValueToStringValidCast3 ()
+               {
+                       Assert.AreEqual ("123", c.ConvertValueToString ("123", 
typeof (int)));
+               }
+
+               [Test]
+               public void ConvertValueToStringValidCast4 ()
+               {
+                       Assert.AreEqual ("123.45", c.ConvertValueToString 
(123.45, typeof (int)));
+               }
+
+               [Test]
+               public void ConvertValueToStringValidCast5 ()
+               {
+                       Assert.AreEqual ("123", c.ConvertValueToString (123, 
typeof (double)));
+               }
+
+               [Test]
+               public void ConvertValueToStringValidCast6 ()
+               {
+                       // umm... should be out of range
+                       Assert.AreEqual ("12345", c.ConvertValueToString 
(12345, typeof (byte)));
+               }
+
+               [Test]
+               public void ConvertValueToStringNullToValueType ()
+               {
+                       Assert.AreEqual (null, c.ConvertValueToString (null, 
typeof (char)));
+               }
+
+               [Test]
+               public void ConvertValueToStringDbNull ()
+               {
+                       Assert.AreEqual ("{}", c.ConvertValueToString 
(DBNull.Value, typeof (DBNull)));
+               }
+
+               [Test]
+               public void ConvertValueToStringDbNull2 ()
+               {
+                       Assert.AreEqual ("\"\"", c.ConvertValueToString ("", 
typeof (DBNull)));
+               }
+
+               [Test]
+               public void ConvertValueToStringDbNull3 ()
+               {
+                       Assert.AreEqual (null, c.ConvertValueToString (null, 
typeof (DBNull)));
+               }
+
+               [Test]
+               public void ConvertValueToStringDbNull4 ()
+               {
+                       // ... so, DBNull is just converted to String
+                       Assert.AreEqual ("\"ABC\"", c.ConvertValueToString 
("ABC", typeof (DBNull)));
+               }
+
+               [Test]
+               public void ConvertValueToStringQName ()
+               {
+                       Assert.AreEqual ("\"foo:\"", c.ConvertValueToString 
(new XmlQualifiedName ("foo"), typeof (XmlQualifiedName)), "#1");
+                       Assert.AreEqual ("\"foo:urn:bar\"", 
c.ConvertValueToString (new XmlQualifiedName ("foo", "urn:bar"), typeof 
(XmlQualifiedName)), "#2");
+               }
+
+               [Test]
+               public void ConvertValueToStringNullToString ()
+               {
+                       Assert.IsNull (c.ConvertValueToString (null, typeof 
(string)));
+               }
+
+               [Test]
+               public void ConvertValueToString ()
+               {
+                       Assert.AreEqual ("\"A\"", c.ConvertValueToString ('A', 
typeof (char)), "#1");
+                       Assert.AreEqual ("\"}}}\"", c.ConvertValueToString 
("}}}", typeof (string)), "#2");
+                       Assert.AreEqual ("123", c.ConvertValueToString (123.0, 
typeof (double)), "#3");
+               }
+
+               // ConvertStringToValue
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ConvertStringToValueInvalidCast ()
+               {
+                       c.ConvertStringToValue ("ABC", typeof (char));
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               [Category ("NotWorking")]
+               public void ConvertStringToValueInvalidCast2 ()
+               {
+                       c.ConvertStringToValue ("-123", typeof (uint));
+               }
+
+               [Test]
+               [ExpectedException (typeof (FormatException))]
+               public void ConvertStringToValueInvalidCast4 ()
+               {
+                       c.ConvertStringToValue ("123.45", typeof (int));
+               }
+
+               [Test]
+               public void ConvertStringToValueString1 ()
+               {
+                       // hmm ...
+                       Assert.AreEqual ("-123.45", c.ConvertStringToValue 
("-123.45", typeof (string)));
+               }
+
+               [Test]
+               public void ConvertStringToValueString2 ()
+               {
+                       Assert.AreEqual ("ABC", c.ConvertStringToValue 
("\"ABC\"", typeof (string)));
+               }
+
+               [Test]
+               [ExpectedException (typeof (SerializationException))]
+               public void ConvertStringToValueString3 ()
+               {
+                       // missing closing '"'
+                       Assert.AreEqual ("\"ABC", c.ConvertStringToValue 
("\"ABC", typeof (string)));
+                       // this test exposes that .NET uses 
DataContractJsonSerializer internally.
+               }
+
+               [Test]
+               public void ConvertStringToValueNullToValueType ()
+               {
+                       // hmm, it passes.
+                       Assert.AreEqual (default (char), c.ConvertStringToValue 
(null, typeof (char)));
+               }
+
+               [Test]
+               public void ConvertStringToValueDbNull ()
+               {
+                       Assert.AreEqual (null, c.ConvertStringToValue (null, 
typeof (DBNull)));
+               }
+
+               [Test]
+               [ExpectedException (typeof (SerializationException))]
+               public void ConvertStringToValueDbNull2 ()
+               {
+                       c.ConvertStringToValue ("", typeof (DBNull));
+               }
+
+               [Test]
+               [ExpectedException (typeof (SerializationException))]
+               public void ConvertStringToValueDbNull3 ()
+               {
+                       c.ConvertStringToValue ("ABC", typeof (DBNull));
+               }
+
+               [Test]
+               public void ConvertStringToValueDbNull4 ()
+               {
+                       Assert.AreEqual (DBNull.Value, c.ConvertStringToValue 
("{}", typeof (DBNull)));
+               }
+
+               [Test]
+               public void ConvertStringToValueNullToString ()
+               {
+                       Assert.IsNull (c.ConvertStringToValue (null, typeof 
(string)));
+               }
+
+               [Test]
+               public void ConvertStringToValue ()
+               {
+                       Assert.AreEqual ('A', c.ConvertStringToValue ("A", 
typeof (char)), "#1");
+                       // likely .NET bug: it should fail to deserialize as it 
is not a valid JSON string.
+                       //Assert.AreEqual ("}}}", c.ConvertStringToValue 
("}}}", typeof (string)), "#2");
+                       Assert.AreEqual (123.0, c.ConvertStringToValue 
("123.0", typeof (double)), "#3");
+                       Assert.AreEqual (123.0, c.ConvertStringToValue ("123", 
typeof (double)), "#4");
+               }
+
+               // Types
+
+               [TypeConverter (typeof (MyTypeConverter))]
+               class MyConvertible
+               {
+               }
+
+               class MyTypeConverter : TypeConverter
+               {
+                       public override object ConvertTo 
(ITypeDescriptorContext context, CultureInfo culture, object value, Type 
destinationType)
+                       {
+                               if (destinationType == typeof (string))
+                                       return "hogehoge";
+                               throw new Exception ();
+                       }
+               }
+       }
+}


Property changes on: 
trunk/olive/class/System.ServiceModel.Web/Test/System.ServiceModel.Dispatcher/JsonQueryStringConverterTest.cs
___________________________________________________________________
Name: svn:eol-style
   + native

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to