How can I serialize namespaces from an XElement using json.net
===

I'm experimenting with Json.NET, trying to find an automatic way to translate the Xml documents generated by our business layer into JSON for the javascript on our web client.

`JsonConvert.SerializeXObject()` will serialize attributes, but not namespaces (even through they're really just another attribute)

We generate wrappers for XElements using Linq to Xsd, which generates schema conforming Xml.

So the test code goes like this:

            input.Type = "Bob the type";
            input.InputPayload = new InputPayload();
            string test = JsonConvert.SerializeXNode(input.Untyped);

Which generates the following XML:

	<Input xmlns="http://tempuri.org/Test.xsd">
	  <Type>Bob the type</Type>
	  <InputPayload xmlns="http://www.test.com/BL.xsd" />
	</Input>

But the following JSON:

	{"Input":{"Type":"Bob the type","InputPayload":null}}

However, if I try to load that back into our wrappers, we get nothing in the wrapper's properties because the namespaces are missing.

BUT, if I do THIS:

            XDocument output =
              new XDocument(
                new XElement("ComplexType",
                    new XAttribute("xmlns", "http://www.attributetest.com"),
                  new XElement("CurrentDateTimeLocal", "test"),
                  new XElement("CurrentDateTimeUTC", "Test"),
                  new XElement("LocalTimeZone", "Test")
                  )
                );

I get this Xml:

	<ComplexType testattr="http://www.attributetest.com">
	  <CurrentDateTimeLocal>test</CurrentDateTimeLocal>
	  <CurrentDateTimeUTC>Test</CurrentDateTimeUTC>
	  <LocalTimeZone>Test</LocalTimeZone>
	</ComplexType>

And this JSON:

	{"ComplexType":{"@testattr":"http://www.attributetest.com","CurrentDateTimeLocal":"test","CurrentDateTimeUTC":"Test","LocalTimeZone":"Test"}}

Which uses an @ notation to denote attributes.

This correctly de-serializes the XDocument, so it can handle attributes, just doesn't recognize namespaces.

Does anybody know how to make this work with the existing Json.NET library or will I have to write support for this myself?

Thanks,

J.


