Instructions on handling JsonEncodedMessage

Jan 24, 2012 at 5:54 PM
Edited Jan 24, 2012 at 5:56 PM

A blurb on where to find the transmitted sent might be helpful. To get to my data, should I be using MessageText? JsonEncodedMessage? Args[0]? Did you parse it already with JSON.NET?

I want to pull it into an object using JsonConvert.DeserializeObject.

Jan 24, 2012 at 6:09 PM

Upon further research, it seems that Args[0] is the JObject.  I'm ended up with

                    IEnumerable<JToken> results = msg.JsonEncodedMessage.Args[0]["Results"].Children();

                    IList<IOImageResult> imageResults = new List<IOImageResult>();
                    foreach (JToken result in results)
                        IOImageResult imageResult = JsonConvert.DeserializeObject<IOImageResult>(result.ToString());
                    ImagesReceived(this, new MessageEventArgs(msg));


Anyone know how to go from currency ($1.99) to decimal? I get "Could not convert string to decimal: $3.08. Line 5, position 19." as a JsonReaderException. Perhaps I can only deserialize that to a string...

Jan 24, 2012 at 9:21 PM


You should be able do this:

socket.On("some event", (data) =>
    IOImageResult imageResult = data.JsonEncodedMessage.GetFirstArgAs<IOImageResult >();

See the last section of the doc's page here, as well as JsonEncodedMessage.GetFristArgsAs<T> and/or JsonEncodedMessage.GetArgsAs<T> for IEnumerated items

The other ways to show the values:

socket.On("news", (data) =>
				Console.WriteLine("Recv'd 'news' event! ");
				Console.WriteLine("  raw message:      {0}", data.RawMessage);
				Console.WriteLine("  string message:   {0}", data.MessageText);
				Console.WriteLine("  json data string: {0}", data.JsonEncodedMessage.ToJsonString());
				Console.WriteLine("  json raw:         {0}", data.JsonEncodedMessage.Args[0]);