Serialize complex objects in IronPython for ASP.NET?

Last post 03-20-2009 3:34 PM by JoshuaP_Ohio. 3 replies.

Sort Posts:

  • Serialize complex objects in IronPython for ASP.NET?

    03-10-2007, 2:00 PM
    • Member
      451 point Member
    • jdanforth
    • Member since 11-12-2003, 2:46 PM
    • Sweden
    • Posts 16

    I'm trying to create an XmlSerializer in an IronPython ASP.NET page to serialize an object with a few properties, but I get this error from the compiler:

     Parser Error Message: Unable to generate a temporary class (result=1).
    error CS0012: The type 'IronMath.ISlice' is defined in an assembly that is not referenced. You must add a reference to assembly 'IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

    I've tried a number of things to get this to work. Added this in my web.config:

    <

    add assembly="IronMath, Version=1.0.60816.1877, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

    But to no change. IronMath is in my bin directory. I've also tried to add reference to IronMath in code, but nothing works. This is my code:

    a = MyClass(

    "Johan",40)

    ser = XmlSerializer(a.GetType())

    writer = StreamWriter(

    "c:/dump.xml")

    ser.Serialize(writer,a)

    writer.Close()

    MyClass looks like this:

    class

    MyClass:

      def __init__(self, name, height):

      self.name = name

      self.height = height

    I guess it's a general problem to serialize objects in Python... Or is there a trick? I could define MyClass and code to serialize in a c# assembly, but I would like to do it in Python only.

     

    /Johan
  • Re: Serialize complex objects in IronPython for ASP.NET?

    03-11-2007, 9:57 PM
    • Contributor
      5,700 point Contributor
    • davidebb
    • Member since 06-11-2002, 8:31 AM
    • Redmond, WA
    • Posts 1,135
    • AspNetTeam

    Hi Johan,

    To isolate a bit, can you try running the same logic directly from ipy.exe instead of through ASP.NET?  If it happens there as well, then at least we know ASP.NET is not in the picture.

    thanks,
    David

  • Re: Serialize complex objects in IronPython for ASP.NET?

    07-12-2007, 5:35 AM
    • Member
      451 point Member
    • jdanforth
    • Member since 11-12-2003, 2:46 PM
    • Sweden
    • Posts 16

    Hi, back again... I've installed IronPython 2.0A1 which I believe is based on the Silverlight DLR stuff and done some more testing. Here's the code I'm runnig from the ipy.exe console:

    import clr

    import System

    clr.AddReference("System.Xml")

    from System import *

    from System.IO import *

    from System.Xml.Serialization import *

    class Myclass(System.Object):

    name = None

    age = 0

    def __init__(self):

    self.name = ""

    self.age = 0

    def __init__(self, n, a):

    self.name = n

    self.age = a

    def __str__(self):

    return "%s, %s years old"%(self.name,self.age)

    a = Myclass("Angie",66)

    print(a)

    ser = XmlSerializer(a.GetType())

    writer = StreamWriter(
    "C:/dump.xml")

    ser.Serialize(writer, a)

    writer.Close()

    And here's the output from running that piece: 

    C:\IronPython-2.0A1\IronPython-2.0A1>ipy Program.py
    Angie, 66 years old
    Traceback (most recent call last):
      File Program.py, line 26, in Initialize
      File , line 0, in _stub_##31
      File , line 0, in NonDefaultNew##36
      File , line 0, in .ctor##38
      File System.Xml, line unknown, in .ctor
      File System.Xml, line unknown, in .ctor
      File System.Xml, line unknown, in ImportTypeMapping
      File System.Xml, line unknown, in GetTypeModel
      File System.Xml, line unknown, in GetTypeDesc
      File System.Xml, line unknown, in CheckSupported
    SystemError: IronPython.NewTypes.System.Object_1 cannot be serialized because it does not have a parameterless constructor.

    Hope this helps. Again, I'm new dynamic languages and not sure how well serialization is supposed to work here. I'm also unfamilar to how constructor overloading works in python I'm afraid, as it might have something to do with the error in this case.

    Thanks,
    Johan

    /Johan
    Filed under:
  • Re: Serialize complex objects in IronPython for ASP.NET?

    03-20-2009, 3:34 PM
    • Member
      2 point Member
    • JoshuaP_Ohio
    • Member since 02-05-2009, 9:58 AM
    • Posts 10

    Hello Johan,

    What is the target of your serialized object?  Depending on the intended usage, there are other serialization types available:

    1. Simple XML generator on my blog here: http://www.globalherald.net/jb01/weblog/20.html
    2. Python Pickles might work, if your target is also Python: http://docs.python.org/library/pickle.html
    3. Google Protocol Buffers offer an extremely fast and compact format for objects being serialized between Python, Java, C++, and .NET languages: a) http://code.google.com/apis/protocolbuffers/ and b) http://code.google.com/p/protobuf-net/

    Does this help?

    Cheers,

    -JK

Page 1 of 1 (4 items)