Json.Net and couchdb and XAML

Jul 10, 2009 at 12:57 AM

Hey all!

I'm new to Json.Net - but have been testing it with CouchDB and SharpCouch as of late.

Right now I'm having a good deal of success in pulling a Json string from Couch, Serializing it into an XElement and then binding it to a XAML form.

Where I'm having the problem is in saving the xml back to CouchDB.

To serialize to an XElement - it requires a root element to be added - which is no problem, but to save it back, I can't have my data reside in a root element.  That takes away some of the point of the indexing goodness of Couch. 

Any good recommendations?

I've tried:

JObject myJObject = JObject.Read(oldJObject.Property("root").Value.ToString()); 

but that mangles the text and isn't read correctly as a new object.

So any advice on getting rid of that "root" property for the JObject would be really helpful.

Otherwise - any tips on getting Json.Net to bind directly to a XAML form would be really helpful!

Thanks, and when I get up to speed I'd love to help out!.



Jul 10, 2009 at 8:53 AM

That should work. What is the JSON in oldJObject?


You can just do something like this:

JObject myJObject = (JObject)oldJObject["root"];

Jul 13, 2009 at 6:44 PM

That works, but I'm realizing only gets me part way there.

I'm looking to create a bindable property collection, when I get it done, could I submit my code back to you - to see if it could be integrated, or done a better way?

I know making something an observable colleciton creates much overhead, but I figure if we had an addition method called "BindableProperties" then we could create "BindableProperties" objects that could be bound to XAML forms, from the source JSON, but wouldn't be used otherwise.  Does this sound like a viable option for binding JSON to XAML?

Jul 14, 2009 at 10:55 AM

I haven't done much WPF but it seems to me that working with untyped JSON objects in XAML is a bit hacky.

I would probably create a simple strongly typed object of my data and then either use the serializer or a LINQ query to convert my JSON into a collection that object. I'd then use that collection to create an observable collection.

Jul 14, 2009 at 3:09 PM

Well that would defeat the purpose now wouldn't it.  :-)

My requirements are to serve up different XAML forms to different customers in an RIA - each one depending on a potentially different data source object (stored in Json)- but all within the same "one click app".

I guess my question is... why is it hacky if you can already do almost the same thing in XML?  I guess I can go back to my XML conversion - but still not sure why its okay to bind directly to XML, but to bind to a JSON object is hacky?


Jul 14, 2009 at 10:30 PM

I think binding XML directly to the UI is also hacky :)

Jul 14, 2009 at 10:42 PM

I think the proper solution is to implement a ICustomTypeDescriptor for the LINQ to JSON objects. I'll do this at some point but probably not anytime soon.