7

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

This line 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?

5 Answers 5

9

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

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Gave up - created concrete classes. Not ideal but neither is Silverlight.
I've used ExpandoObjects successfully with JSON.NET. The syntax is slightly more verbose since you can't use object initializers, but definitely better than having to define named classes.
5

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")] 

Comments

3

Is there a specific reason why you want to use Json.NET? If not, you might want to try the built-in serializer (in the System.Runtime.Serialization namespace). I have to admit, I have never tried it with anonymous types, so I am not sure if this will be useful to you. Anyway, here is the class I use 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);
            }
        }

    }
}

1 Comment

Good suggestion, but I have class libraries that I build for multiple platforms and json.net seems to have the most support across the various framework versions. I've abandoned hope for anonymous type serialization - ended up creating classes. Silverlight, you win again.
0

Maybe have a look at this http://whydoidoit.com/silverlight-serializer/ as I have used this to serialize many objects in Silverlight, although I cant remember if i did anonymous types with it.

Comments

0

To supplement the other answers with another workaround, note that the reflection (and so the serialization of anonymous types) will succeed when running with elevated trust.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.