1

I have a structure somewhat like an enum, except it has a bunch of methods:

public struct ComponentIdentifier : IComparable
{
    private long identifier;

    ...
}

I want this to serialize such that <ComponentIdentifier>1234</ComponentIdentifier> is that possible?

I want it to work for xml and json.

Thanks.

1 Answers1

0

There are a few options.

  1. Option 1 - Add properties in all parent objects of ComponentIdentifier that handle serialization
  2. Option 2 - User data contract surrogate
  3. Option 3 - Use JSON.NET's JsonConverter

Drawbacks

  1. Option 1 - Code required in all classes that use ComponentIdentifier & identifier made public
  2. Option 2 - You will not be able to deserialize using this method.
  3. Option 3 - Non-Microsoft library, may require performance tuning

I'm working through this problem myself, and I'm leaning towards option #3. I'd love it if Microsoft could do another rev of DataContractSerializer to make it a bit more flexible, alas.

Option 1:

static void Main(string[] args)
{
    var serializeMe = new Foo() { Id = new ComponentIdentifier() };
    var xml = Serialize(serializeMe);
    Console.WriteLine(xml);
    Console.ReadKey();
}

[DataContract]
public class Foo
{
    [DataMember(Name = "Id")]
    private string IdForSerialization
    {
        get { return Id.identifier.ToString(); }
        set { Id = new ComponentIdentifier() {identifier = int.Parse(value)}; }
    }

    public ComponentIdentifier Id { get; set; }
}

[DataContract]
public struct ComponentIdentifier
{
    [DataMember]
    public long identifier;
}

// thanks to http://stackoverflow.com/questions/5010191/using-datacontractserializer-to-serialize-but-cant-deserialize-back
public static string Serialize(object obj)
{        
    using (MemoryStream memoryStream = new MemoryStream())
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
        serializer.WriteObject(memoryStream, obj);
        memoryStream.Position = 0;
        return reader.ReadToEnd();
    }
}

Option 2:

static void Main(string[] args)
{
    var serializeMe = new Foo() { Id = new ComponentIdentifier() };
    var xml = Serialize(serializeMe, new ComponentIdentifierSurrogate());
    Console.WriteLine(xml);
    Console.ReadKey();
}

[DataContract]
public class Foo
{
    [DataMember]
    public ComponentIdentifier Id { get; set; }
}

[DataContract]
public struct ComponentIdentifier
{
    [DataMember]
    public long identifier;
}

class ComponentIdentifierSurrogate : IDataContractSurrogate
{
    public Type GetDataContractType(Type type)
    {
        if (type == typeof(ComponentIdentifier))
            return typeof(string);
        return type;
    }

    public object GetObjectToSerialize(object obj, Type targetType)
    {
        if (obj is ComponentIdentifier)
            return ((ComponentIdentifier)obj).identifier.ToString();
        return obj;
    }

    ...
}

public static string Serialize(object obj, IDataContractSurrogate surrogate)
{
    using (MemoryStream memoryStream = new MemoryStream())
    using (StreamReader reader = new StreamReader(memoryStream))
    {
        DataContractSerializer serializer = new DataContractSerializer(obj.GetType(), null, int.MaxValue, false, false, new ComponentIdentifierSurrogate());
        serializer.WriteObject(memoryStream, obj);
        memoryStream.Position = 0;
        return reader.ReadToEnd();
    }
}

Option 3:

static void Main(string[] args)
{
    var serializeMe = new Foo() { Id = new ComponentIdentifier() };
    var xml = Serialize(serializeMe);
    Console.WriteLine(xml);
    Console.ReadKey();
}

[DataContract]
public class Foo
{
    [DataMember]
    public ComponentIdentifier Id { get; set; }
}

[DataContract, JsonConverter(typeof(ComponentIdentifierJsonConverter))]
public struct ComponentIdentifier
{
    [DataMember]
    private long identifier;

    public class ComponentIdentifierJsonConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(((ComponentIdentifier)value).identifier.ToString());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return new ComponentIdentifier() { identifier = int.Parse((string)JToken.ReadFrom(reader)) };
        }

        public override bool CanConvert(Type objectType)
        {
            if (objectType == typeof(ComponentIdentifier))
                return true;
            return false;
        }
    }
}

public static string Serialize(object obj)
{
    var json = JsonConvert.SerializeObject(obj);
    var xml = JsonConvert.DeserializeXNode(json);
    xml = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement(obj.GetType().Name, xml.Root));
    return xml.ToString();
}

Good luck!

sky-dev
  • 6,190
  • 2
  • 33
  • 32