NewComer
NewComer

Reputation: 305

Cannot find System.Runtime.Serialization.Json in MonoTouch

I am a beginner in Monotouch.
I would like to deserialize Json data using DataContractJsonSerializer. But I cannot reference System.Runtime.Serialization.Json(Only .Formatters under System.Runtime.Serialization) in MonoDevelop. I have referenced System.Runtime.Serialization. My config and installation sequences are: 1. iPhone SDK 4.2 2. Mono 2.8.2 (not CSDK version) 3. Monotouch 3.2.4 Eval 4. MonoDevelop 2.4

What is the problem?

Upvotes: 5

Views: 2397

Answers (2)

Omri Gazitt
Omri Gazitt

Reputation: 3518

If you're like me and are trying to use DataContractJsonSerializer in a cross-platform codebase, it is easy enough to wrap the JSON.NET API (aka Newtonsoft.Json) in a DataContractJsonSerializer:

using System;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;

namespace System.Runtime.Serialization.Json
{
public class DataContractJsonSerializer
{
    private Type type;
    private JsonSerializer js;

    public DataContractJsonSerializer (Type t)
    {
        this.type = t;
        this.js = new JsonSerializer();
    }

    public object ReadObject(Stream stream)
    {
        StreamReader reader = new StreamReader(stream);     
        return js.Deserialize(reader, type);
    }

    public void WriteObject(Stream stream, object o)
    {
        StreamWriter writer = new StreamWriter(stream);
        js.Serialize(writer, o);    
        writer.Flush ();
    }
}
}

Of course, that begs the question of why not switch to using the JSON.NET API everywhere... my personal experience with that API is that it can be slower than using DCJS (at least in my informal tests on Windows Phone).

Hope that helps!

Upvotes: 0

miguel.de.icaza
miguel.de.icaza

Reputation: 32694

MonoTouch does not ship with a DataContractJSonSerializer as simple as this serializer looks, it brings in a large set of libraries.

You can use either the System.Json API or you can try NewtonSoft's JSon library.

Upvotes: 7

Related Questions