Serialization for list of custom objects.

Mar 26, 2013 at 8:32 PM
Keith,

I need to pass a list of customer objects as one of the property of the event object. I read some of the discussion, but all of them are for the older version of the framework. I am using 2.1. I really don't want to use the default .NET serialization to convert my object to byte array for performance reason. Can you please provide a sample event class has list of custom object property with custom serialization and deserialization.
Coordinator
Mar 26, 2013 at 9:30 PM
Can you send your class? Or at least something similar? Then I'll give you back the class with the serialization code.
Mar 26, 2013 at 10:07 PM
Edited Mar 26, 2013 at 10:09 PM
Hello Keith,
Below is a sample class.
Thank you


public class Order : Event
{
   public List<Product> Products { get; set; }
   public DateTime OrderDate { get; set; }

   public override void GetObjectData(WspBuffer buffer)
   {
       throw new NotImplementedException();
   }
}


public class Product
{
    public int Id { get; set; }
    public Item[] items { get; set; }
}

public class Item
{
    public int ItemId { get; set; }
    public string SKU { get; set; }
    public string Color { get; set; }
       }
Coordinator
Mar 27, 2013 at 5:38 AM
This would be easier with the new 3.0 version. I think this will work but I didn't test it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WebSolutionsPlatform.Event;

namespace Order
{
public class Order : Event
{
    public List<Product> Products { get; set; }
    public DateTime OrderDate { get; set; }

    /// <summary>
    /// Base constructor to create a new event
    /// </summary>
    public Order()
        : base()
    {
    }

    /// <summary>
    /// Base constructor to create a new event from a serialized event
    /// </summary>
    /// <param name="serializationData">Serialized event buffer</param>
    public Order(byte[] serializationData)
        : base(serializationData)
    {
    }

    /// <summary>
    /// Used for event serialization.
    /// </summary>
    /// <param name="buffer">SerializationData object passed to store serialized object</param>
    public override void GetObjectData(WspBuffer buffer)
    {
        buffer.AddElement(@"OrderDate", OrderDate);

        // Write out Products
        buffer.Write(Products.Count);

        for (int i = 0; i < Products.Count; i++)
        {
            buffer.Write(Products[i].Id);
            buffer.Write(Products[i].items.Length);

            for (int j = 0; j < Products[i].items.Length; j++)
            {
                buffer.Write(Products[i].items[j].ItemId);
                buffer.Write(Products[i].items[j].SKU);
                buffer.Write(Products[i].items[j].Color);
            }
        }
    }

    /// <summary>
    /// Set values on object during deserialization
    /// </summary>
    /// <param name="elementName">Name of property</param>
    /// <param name="elementValue">Value of property</param>
    /// <returns></returns>
    public override bool SetElement(string elementName, object elementValue)
    {
        switch (elementName)
        {
            case "OrderDate":
                OrderDate = (DateTime)elementValue;

                int productsCount;
                int id;
                int itemsCount;
                int itemId;
                string sku;
                string color;

                SerializedEvent.Read(out productsCount);

                Products = new List<Product>(productsCount);

                for (int i = 0; i < productsCount; i++)
                {
                    Products.Add(new Product());

                    SerializedEvent.Read(out id);
                    Products[i].Id = id;

                    SerializedEvent.Read(out itemsCount);

                    Products[i].items = new Item[itemsCount];

                    for (int j = 0; j < itemsCount; j++)
                    {
                        Products[i].items[j] = new Item();

                        SerializedEvent.Read(out itemId);
                        SerializedEvent.Read(out sku);
                        SerializedEvent.Read(out color);

                        Products[i].items[j].ItemId = itemId;
                        Products[i].items[j].SKU = sku;
                        Products[i].items[j].Color = color;
                    }
                }

                break;

            default:
                base.SetElement(elementName, elementValue);
                break;
        }

        return true;
    }
}


public class Product
{
    public int Id { get; set; }
    public Item[] items { get; set; }
}

public class Item
{
    public int ItemId { get; set; }
    public string SKU { get; set; }
    public string Color { get; set; }
}
}
Mar 27, 2013 at 5:38 PM
Thank you Keith, works like a charm.