Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON de-serialization with MonoTouch

there are various ressources on the web explaining how to serialize some C# object to a JSON string. I have been unable to get any of them to work with MonoTouch 3.2.4 though.

Alternatives I came across:

  • the System.Json Namespace
    I am already successfully using the System.Json namespace to de-serialize data. Hence, for simplicity's sake, I'd prefer to stick to that if possible. Unfortunately, I have no idea how to serialize with it (or if it is even possible).

  • System.Runtime.Serialization.Json.DataContractJsonSerializer
    The System.Runtime.Serialization.Json namespace is not available in MonoTouch. I've read somewhere that it is part of .NET 3.5 and that MonoTouch is at .NET 2.0 level. That might explain it.

  • System.Web.Script.Serialization.JavaScriptSerializer
    This one is also missing in MonoTouch (in fact, there only seems to be System.Web.Services available).

  • Json.NET by James Newton-King
    I managed to make this one work on the simulator by including the .NET 2.0 assembly dll and using Newtonsoft.Json.JsonConvert.SerializeObject(). However, this doesn't compile for the device with error "mtouch failed with no output". As soon as I remove the assembly reference, it compiles fine.

Is there any way to make one of those work with MonoTouch? Are there other alternatives to try?

Thanks

like image 827
riha Avatar asked Feb 15 '11 12:02

riha


People also ask

What is the difference between JSON and serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

How do you serialize JSON?

NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer. Serialize method. The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.

Is JSON serialized or Deserialized?

JSON is language independent and because of that, it is used for storing or transferring data in files. The conversion of data from JSON object string is known as Serialization and its opposite string JSON object is known as Deserialization.


1 Answers

I'd recommend using the System.Json library, which is not too difficult to use and wrap in a class to make it strongly typed. It is also included as a default assembly with MonoTouch as it was inherited from Silverlight.

To load it up:

JsonValue value = JsonObject.Load(stream); //There are other overloads here, my stream is off an HttpWebRequest

Here is an example by index:

this.StringValue = value[0];
this.IntValue = value[1];

One my name:

this.StringValue = value["StringValue"];
this.IntValue = value["IntValue"];

Where this is a class with a string property named StringValue and an int property named IntValue. For the most part, the JsonValue class will cast to your types implicitly.

So what I do, is create a class to hold all of the Json info in a strongly typed way, then pass in a JsonValue in the constructor to do the ugly weakly typed stuff.

For other libraries, you will run into problems where MonoTouch strips out types that are seen to be "unused" when created by reflection (as when you use XmlSerializer, for example). I have found myself fighting that issue more than necessary, having to add PreserveAttribute on types, etc.

On this issue, read here for more info on Preserve: MonoTouch Doc

like image 130
jonathanpeppers Avatar answered Sep 29 '22 01:09

jonathanpeppers