Json string to IJsonWritable object

Nov 8, 2011 at 9:18 PM

I want to call the BayuexConnection.Publish() method with a JSON-formatted string. However, this method takes an ISonWritable object. How can I convert a JSON-formatted string to an IJsonWritable object?

Nov 9, 2011 at 8:50 PM


Long story short - you can't.

The data you are supposed to pass as arguments of Publish() method, are just a part of the bigger JSON message. To have a full control over what is sent to the server (+error checks, and JSON validation on client) it doesn't allow to inject unknown strings into the Bayeux message.

The designated pattern is following:
 * model classes inherit from IJSonWritable interface
 * they serialize the state using the IJSonWriter API (provided via IJSonWritable.Write() method) as dictionary objects, arrays, and values
 * they pass serialization requests to all referenced other instances

Such a class could look like:

class MyArguments : IJSonWritable
 private string _fieldA;
 private int _fieldB;
 private OtherClass _fieldC;

 public void Write(IJSonWriter output)

    using (output.WriteObject())
       output.WriteMember("fieldA", _fieldA);
       output.WriteMember("counter", _fieldB);

And the OtherClass implements as well the IJSonWritable interface in similar style.


Hope this helps.