Author Topic: C# Automatic XML Deserialization  (Read 8838 times)

programmerlike

  • Newcomer
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • Email
C# Automatic XML Deserialization
« on: March 30, 2011, 09:09:48 PM »
Hello,

i did just finished to code a parser that can automatically fill class data from an Xml using reflection (it's c# but it could be easily translated to java).
Look at the following data (real example from the roguelike i'm working on):

Code: [Select]
    class Roll
    {
        [AutoSerializer(Optional = true)]
        public int Base
        { get; set; }

        public int Dice
        { get; set; }
       
        public int Faces
        { get; set; }

        public override string ToString()
        {
            return string.Format("{0}+{1}d{2}", Base, Dice, Faces);
        }
    }

    enum PotionKind
    {
        Healing = 0,

        Mana = 1       
    }

    class ItemTemplate
    {
        public string Name
        { get; private set; }

        public double Weight
        { get; private set; }       

        [AutoSerializer(Optional = true)]
        public bool IsStackable
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public Roll Damage
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public int Protection
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public PotionKind PotionKind
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public bool IsWeldable
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public bool IsWearable
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public bool IsQuaffable
        { get; private set; }

        [AutoSerializer(Optional = true)]
        public bool IsProjectile
        { get; private set; }

        ItemTemplate()
        {
        }
    }

The Optional property of the AutoSerializerAttribute tells the serializer that that property can be omitted in the xml file.
OK... now this is a test XML file:

Code: [Select]
<?xml version="1.0" encoding="utf-8" ?>
<GameData> 
  <ArrayItem>   
    <Name>Leather Armor</Name>
    <Weight>4.5</Weight>   
    <Protection>3</Protection>   
    <IsWearable>true</IsWearable>
  </ArrayItem>
  <ArrayItem>
    <Name>Long Sword</Name>
    <Weight>1</Weight>   
    <Damage>
      <!-- 2+2d6 -->
      <Base>2</Base>
      <Dice>2</Dice>
      <Faces>6</Faces>
    </Damage>   
    <IsWeldable>true</IsWeldable>
  </ArrayItem>
  <ArrayItem>
    <Name>Potion of Healing</Name>
    <Weight>0.1</Weight>
    <PotionKind>Healing</PotionKind>
    <IsQuaffable>true</IsQuaffable>
  </ArrayItem>
</GameData>

Now you just need to run the following method:

Code: [Select]
ItemTemplate[] items = AutoSerializer.Load<ItemTemplate>(xmlFile);

And the "items" array will be filled with the data from the XML file.
The system also supports nested arrays:

Code: [Select]
    class SomeObject
    {
        public int[] SomeValues
        { get; private set; }
    }

Code: [Select]
<?xml version="1.0" encoding="utf-8" ?>
<GameData> 
  <ArrayItem>
    <SomeValues>
      <ArrayItem>1</ArrayItem>
      <ArrayItem>2</ArrayItem>
      <ArrayItem>3</ArrayItem>
    </SomeValues>
  </ArrayItem>
  <ArrayItem>
    <SomeValues>
      <ArrayItem>4</ArrayItem>
      <ArrayItem>5</ArrayItem>
      <ArrayItem>6</ArrayItem>
    </SomeValues>
  </ArrayItem>
</GameData>

WELL... maybe no one is interested in it... just wanted to know your opinion...

dephbokks

  • Newcomer
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Re: C# Automatic XML Deserialization
« Reply #1 on: March 30, 2011, 09:42:20 PM »
Loading information from files when creating roguelikes seems very wise to me. I also use xml to serialize objects. I love that it is so easy to edit and read. I could not imagine hard-coding all the data!

Currently, I use xml for item data and monster data. When starting the game, I just read the item and monster xml files. From this, I create a pool of available monster and items available to the game. The game just uses these pools to populate the dungeon. That way, I can add an unlimited amount of new monsters/items so long as I enter their xml data.

In xml, I also store things like how the item is used. So when the player uses an item, that item will be used however it is intended. The only time code needs to be touched is when adding a new class of effect for items.

In the future, I am thinking of adding an xml file for game parameters. That way I can easily tweak these things for balancing the game. [In my current project, I am almost set for an alpha release, with game balancing to follow.]

So yeah, I am a big fan of xml. I use C++ since I made a roguelike library for it and there's some libraries for easily parsing the xml so I think using xml files can work for most languages. It is (maybe) a little more work to get set up initially, but in the end it is worth it. In my current project, I have 90+ monsters and 90+ items; after coding the requirements to 'run' these items and monsters, it only took me an afternoon to enter all this content (since typing things into xml is a piece of cake).

Also, it opens up the opportunity for players modding the game. For example, say some player wants, unicorns, dragons, and puppies as possible monsters, he only has to fill out the xml et voila he will be fighting them in the next game.

By the way, I use programmer's notepad and Editpad to work with the xml.

programmerlike

  • Newcomer
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • Email
Re: C# Automatic XML Deserialization
« Reply #2 on: March 30, 2011, 09:59:17 PM »
Maybe you misunderstood me...
My post was about a class i made that at runtime looks up into public fields of your class and finds xml tags with the same name, then fill those fields with data. Also arrays and nested complex objects are supported. Whatever class you want to load from xml you don't have to code anything!
Since you agree with me that easy data importing is a must have feature i hope you find my effort uefull. Anyway it's c# so you cant' use it in c++ code.

Hope this time was more clear!

dephbokks

  • Newcomer
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Re: C# Automatic XML Deserialization
« Reply #3 on: March 30, 2011, 10:30:52 PM »
I thought I understood you, but now I am not so sure. When I typed serializer, I meant to type deserialize. So maybe that's the confusion.

You have an xml file of items.
You then call:
Code: [Select]
ItemTemplate[] items = AutoSerializer.Load<ItemTemplate>(xmlFile);
so that:
Quote
And the "items" array will be filled with the data from the XML file.

I also have an xml file of items.
I have a class called ItemMgr.

I call
Code: [Select]
ItemMgr->LoadItemsXML(xmlfile);
And then I have a C++ vector of items, filled with the data from the xml file.

We are talking about the same thing, right? And that thing is good.

Hopefully, I am still not confused.

programmerlike

  • Newcomer
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • Email
Re: C# Automatic XML Deserialization
« Reply #4 on: March 31, 2011, 12:26:32 AM »
Ok... I just want to point it out... I made a general purpose xml library that can read any class from a file and not just item or monster. You create class "C" then call the loader and it understands how to load it without you coding the parser or providing the load method any additional parameter. It is achieved using type reflection.

dephbokks

  • Newcomer
  • Posts: 37
  • Karma: +0/-0
    • View Profile
Re: C# Automatic XML Deserialization
« Reply #5 on: March 31, 2011, 04:07:45 AM »
Oh, okay. I am with you now. In that case, I'd like to say good job. I'd be interested in seeing how this all works in practice. My C# is a bit rusty; I only made one game with it a year ago -- a simple shmup.

I can see how this will save time and perhaps add simplicity to the design. What other benefits could there be?

If you ever release yr code for yr project I would definitely check it out. I have been googling this stuff this evening and trying to make better sense of it.

purestrain

  • Rogueliker
  • ***
  • Posts: 172
  • Karma: +0/-0
    • View Profile
Re: C# Automatic XML Deserialization
« Reply #6 on: March 31, 2011, 06:57:06 AM »
What is the difference/advantage of your library compared to the already existing one in .net? Afair you could serialize/deserialize almost any objects using some attributes?

programmerlike

  • Newcomer
  • Posts: 11
  • Karma: +0/-0
    • View Profile
    • Email
Re: C# Automatic XML Deserialization
« Reply #7 on: March 31, 2011, 12:54:26 PM »
Yeah that's very similar to serialization mechanism already included in .NET!
It's just simpler to use and needs less attributes configuration to make it work. Obviously it just supports XML (.NET can serialize to binary files to) but it's pretty straightforward to use... also, i have a simple way to tell the serializer which fields must be included and which can be omitted (the Optional property of the AutoSerializerAttribute).
Of course .NET is better, faster and probably my lib still have some bugs... it's just simpler to use and more dedicated to XML. By the way it's indeed nothing amazing but just a tool to make things work as soon as possible for me without messing around with a lot of .NET attributes.
Anyway you did good making non .NET experts notice that this feature already existed natively in the language and that my library is just a home-made way to do a small portion of what .NET does in a simpler to understand way.
I'm still debugging it and also i have to add more detailed documentation.

If someone is still interested i will make it available as open source project as soon as it's good enough to show it.