Implement a Wrapper for XmlObjectSerializer

Dec 7, 2010 at 3:33 PM

Hi,

has anyone cerated a wrapper for WCF with the baseclass XmlObjectSerializer? Until now i use the DataContractJsonSerializer but I like to use the JSON.NET implementation. Here is what I did:

     public class WrappedJsonDataContractSerializer : XmlObjectSerializer
    {
        public override bool IsStartObject(System.Xml.XmlDictionaryReader reader)
        {
                throw new NotImplementedException();
        }

        public override object ReadObject(System.Xml.XmlDictionaryReader reader, bool verifyObjectName)
        {
            return null;
        }

        public override void WriteEndObject(System.Xml.XmlDictionaryWriter writer)
        {
            writer.WriteEndElement();
        }

        public override void WriteObjectContent(System.Xml.XmlDictionaryWriter writer, object graph)
        {

            var resultString = Newtonsoft.Json.JsonConvert.SerializeObject(graph);

            var result = resultString.ToCharArray();
            writer.WriteString(resultString);

        }

        public override void WriteStartObject(System.Xml.XmlDictionaryWriter writer, object graph)
        {
            writer.WriteStartElement("root");
        }

    }

 The result from Json.NET ist right but the wcf stack escaped the string so that the client will get: "\"{\\\"Purchase\\\":{\\\"testnode\\\":{\\\"childnode1\\\":\\\"Test\\\",\\\"childnode2\\\":\\\"Test1\\\"}},\\\"Status\\\":200,\\\"StatusMessage\\\":null}\""

 

Apr 12, 2011 at 9:43 PM

I came up with an alternative approach to subclassing XmlObjectSerializer.

See my post here: http://json.codeplex.com/discussions/209865