-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: XmlSerializer and NullableInt32
PostPosted: Tue Jul 26, 2005 7:21 am 
Hi,

we're using NH + NHContrib 0.5. We have a business class with a NullableInt32 property. We successfully manage to load it from the database.

Later though, we serialize the objects using XmlSerializer (to allow reloading them in "offline" mode). It seems that the serialization (the way we use it at least!) doesn't handle NullableInt32 properly.

Here's a small repro:

Code:
using System;
using System.Collections;
using NUnit.Framework;
using System.IO;
using System.Xml.Serialization;
using Nullables;

namespace NullableInt32Tests
{
   [TestFixture]
   public class NullableInt32Serialize
   {
      [Test,Explicit]
      public void SerializeUnserialize()
      {
         IList list=new ArrayList();
         list.Add(new Foo(new NullableInt32(10)));
         Assert.AreEqual(10,((Foo)list[0]).MyInt.Value);

         XmlSerializer serializer=new XmlSerializer(typeof(ArrayList),new Type[] { typeof(Foo) });
         FileStream stream=new FileStream("NullablesInt32Test.xml",FileMode.Create);
         serializer.Serialize(stream,list);
         stream.Close();

         serializer=new XmlSerializer(typeof(ArrayList),new Type[] { typeof(Foo) });
         stream=new FileStream("NullablesInt32Test.xml",FileMode.Open);
         list= (ArrayList)serializer.Deserialize(stream);
         stream.Close();

         Assert.AreEqual(1,list.Count);
         Foo foo=list[0] as Foo;
         // this assertion will fail
         Assert.IsTrue(foo.MyInt.HasValue);
         Assert.AreEqual(10,foo.MyInt.Value);
      }
   }

   [Serializable]
   public class Foo
   {
      public Foo()
      {
      }

      public Foo(NullableInt32 myInt)
      {
         _myInt=myInt;
      }

      private NullableInt32 _myInt;      
      public NullableInt32 MyInt
      {
         get { return _myInt; }
         set { _myInt=value; }
      }
   }
}


Does anyone see some obvious error ? Or is there something to be changed in NullableInt32 ?

best regards

Ludovic


Top
  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 10:37 am 
Beginner
Beginner

Joined: Wed Jun 29, 2005 10:40 am
Posts: 30
Location: denver, co
I believe the problem lies in the fact that the Nullables don't have default constructors, so they can't be serialized by the xml serializer.

I wish the nullables DID have a default constructor that would just set them to the default (no value). Perhaps an issue in JIRA...

The best thing to do without nullables serializing to xml is to create a data transport class that wraps nullable types in a serializer-friendly type. I've had to do this with dynamic proxy objects.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.