There are a few options.
- Option 1 - Add properties in all parent objects of ComponentIdentifier that handle serialization
- Option 2 - User data contract surrogate
- Option 3 - Use JSON.NET's JsonConverter
Drawbacks
- Option 1 - Code required in all classes that use ComponentIdentifier & identifier made public
- Option 2 - You will not be able to deserialize using this method.
- 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!