Serializing anonymous types in Silverlight using json.net

I'm having trouble serializing an anonymous type only on the Silverlight platform. I have code on .net 4.0 and .netcf that works fine.

This line is right here.

Newtonsoft.Json.JsonConvert.SerializeObject(new { Something = "yup" }); 

throws an aptly named guy, JsonSerializationException:

 Error getting value from 'Something' on '<>f__AnonymousType0`1[System.String]'. 

I tried 4.0r1 and 4.0r2 - Am I doing something wrong or am I taking crazy pills?

+7
source share
5 answers

The problem is that anonymous types are defined by the compiler as inner classes. JSON.NET relies on reflection in work, and in Silverlight reflection on assembly boundaries works only for public types (when using partially trusted assemblies such as this one).

I think the DataContractJsonSerializer, as mentioned in the previous answer, is the way to go in this case, since it is part of the structure and should have additional privileges.

Another thing to try is to use dictionaries or ExpandoObject instead of anonymous types, but YMMV.

+8
source

The answer is simple;) Add [assembly: InternalsVisibleTo ("Newtonsoft.Json")] to AssemblyInfo.cs and voila ... I have exactly the same problem and this attribute solved my serialization / deserialization problem.

AssemblyInfo.cs

 [assembly: InternalsVisibleTo("Newtonsoft.Json")] 
+4
source

Is there a specific reason why you want to use Json.NET? If not, you can try the built-in serializer (in the System.Runtime.Serialization namespace). I must admit, I have never tried this with anonymous types, so I'm not sure if this will be useful to you. Anyway, here is the class I'm using for serialization / deserialization:

 using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.IO; using System.Text; using System.Runtime.Serialization.Json; namespace GLS.Gui.Helper { public static class SerializationHelper { public static string SerializeToJsonString(object objectToSerialize) { using (MemoryStream ms = new MemoryStream()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType()); serializer.WriteObject(ms, objectToSerialize); ms.Position = 0; using (StreamReader reader = new StreamReader(ms)) { return reader.ReadToEnd(); } } } public static T Deserialize<T>(string jsonString) { using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return (T)serializer.ReadObject(ms); } } } } 
+3
source

Perhaps look at http://whydoidoit.com/silverlight-serializer/ , as I used it to serialize many objects in Silverlight, although I cannot remember anonymous types with it.

0
source

To complement the other answers with another workaround, note that reflection (and therefore serialization of anonymous types) will succeed when working with increased trust.

0
source

All Articles