[mono-android] DataContractJsonSerializer cannot convert to array.

Tomasz Cielecki tomasz at ostebaronen.dk
Wed Jan 4 04:03:51 EST 2012


I am working with some JSON at the moment to get some simple strings
such as URLs into an object and have discovered a strange thing.

Consider the following code:

    string json = "[\"Test1\",\"Test2\"]"; //Some dull json array

    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
    {
        DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(string[]));
        var item = serializer.ReadObject(ms);
    }

This will throw an exception at serializer.ReadObject(ms) saying that
it cannot convert the source to the destination. This is possible in a
normal .NET environment.

If you change the type you want to convert to, to something like a
List or IEnumerable like this:

        DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(IEnumerable<string>));

The Json gets converted just fine into a generic list.

Is this normal behaviour?

-- 
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk


More information about the Monodroid mailing list