c# - Data Contract Single Value Structure -
i have structure enum, except has bunch of methods:
public struct componentidentifier : icomparable { private long identifier; ... }
i want serialize such <componentidentifier>1234</componentidentifier> possible?
i want work xml , json.
thanks.
there few options.
- option 1 - add properties in parent objects of componentidentifier handle serialization
- option 2 - user data contract surrogate
- option 3 - use json.net's jsonconverter
drawbacks
- option 1 - code required in classes use componentidentifier & identifier made public
- option 2 - you not able deserialize using method.
- option 3 - non-microsoft library, may require performance tuning
i'm working through problem myself, , i'm leaning towards option #3. i'd love if microsoft rev of datacontractserializer make 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 { { 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; } // 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 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!
Comments
Post a Comment