[Mono-dev] FW: Replicating System.Web.Script.Serialization bugs

Konstantin Triger kostat at mainsoft.com
Tue Oct 16 04:58:02 EDT 2007


Hey Tyler,

1. Your code must come AFTER 'if (type == null)' for obvious reasons.
The best place would be just before 'return Convert.ChangeType (obj,
type);' at the end of the function.

2. Please add the following test cases (passing on MS .Net):
 Converting to Nullable<> from empty and non-empty strings.

3. Please commit the code below for performance reasons.
a. 'is' and casting are much cheaper.
b. don't need to create an instance, returning null should be OK, isn't?

+                       /*
+                        * Take care of the special case whereas in JSON
an empty string ("") really means 
+                        * an empty value 
+                        * (see:
https://bugzilla.novell.com/show_bug.cgi?id=328836)
+                        */
+                       if ( (type.IsGenericType) &&
(type.GetGenericTypeDefinition() == typeof(Nullable<>)) )
+                       {
+                               if ( (obj is String) &&
(((String)obj).Length == 0) )
+                               {
+                                       return null;
+                               }
+                       }
+

Regards,
Konstantin Triger

> -----Original Message-----
> From: R. Tyler Ballance [mailto:tyler at monkeypox.org]
> Sent: Sunday, October 14, 2007 7:13 AM
> To: Adar Wesley
> Cc: Konstantin Triger; mono-devel-list at lists.ximian.com
> Subject: Re: [Mono-dev] Replicating System.Web.Script.Serialization
bugs
> 
> 
> On Oct 1, 2007, at 10:17 AM, Adar Wesley wrote:
> 
> > Hi Kosta and Tyler,
> >
> > Having worked on this code with Kosta and following this thread a
> > couple of questions come to my mind.
> >
> > 1. Are there any other types that need special care, besides
> > Nullable?  How about generic types?
> >     As far as I remember, we don't have any tests with generics.
> > Are we missing anything else?
> >
> > 2. I was trying to think of a good strategy for implementing this,
> > and havn't made up my mind yet.
> >     I agree with Kosta that the try, catch approach is not the best
> > way.  I think something a bit more fine grained
> >     should be used.  What would happen if the value is "xxx" and
> > the type is "int?"?  This should probably
> >     throw the error and not silently assign the null value.
> > (Requires a test with MS code...)
> 
> Coming back to this, since I'm hacking on it right now. A value of
> "xxx"  _will_ throw an InvalidCastException, whereas an empty string
> will not.
> 
> I think I might be overcomplicating this, the only possible valid
> JSON syntax for an empty value seems to be "", I've got the following
> patch that I'm waiting on "peer-review" to commit.
> 
> It handles the case in https://bugzilla.novell.com/show_bug.cgi?
> id=328836 with regards to mimicking the .NET behavior for empty JSON
> values with regards to Nullable types. It'll still throw on "xxx"
> values that are attempting to be converted to long/int/etc.
> 
> Konstantin, just say the word and it'll be committed, otherwise, I'm
> at a lose for solutions to handle this :-P
> 
> Cheers,
> -R. Tyler Ballance




More information about the Mono-devel-list mailing list