This project has moved and is read-only. For the latest updates, please go here.

permission errors using Json.NET on

Feb 2, 2010 at 5:43 PM
Edited Feb 2, 2010 at 6:03 PM

I'm getting a System.Security.SecurityException (Request for the permission of type System.Security.Permissions.ReflectionPermission) failed error when I run a simple test app that calls SerializeObject on an IEnumerable. Am I screwed, will not work in medium trust? Is there some way to serialize my data without using reflection?

Feb 2, 2010 at 6:03 PM

Hrm. I found the post on using JsonTextWriter instead of SerializeObject. Note the cleanest solution by FAR but a bit better than rolling my own I guess.

Feb 2, 2010 at 10:14 PM

In medium trust I would use the LINQ to JSON objects and then do a ToString to get the JSON text from them. That is a much easier way to create JSON than manually using the JsonTextWriter.

Feb 5, 2010 at 6:58 AM

Nope, no luck using LINQ to JSON:

Newtonsoft.Json.JsonSerializationException in Error getting value from 'data' on '<>f__AnonymousType1`1[System.Linq.IQueryable`1[<>f__AnonymousType0`3[System.String,System.String,System.String]]]'.

System.Security.SecurityException in Request for the permission of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I assume you meant doing a normal LINQ-to-SQL query to get the data from my database then converting it with JObject.FromObject()?

Feb 5, 2010 at 7:32 AM

No. FromObject is just a wrapper over the JsonSerializer. I mean something like this (taken from the Json.NET documentation):

List<Post> posts = GetPosts();
JObject rss =
  new JObject(
    new JProperty("channel",
      new JObject(
        new JProperty("title", "James Newton-King"),
        new JProperty("link", ""),
        new JProperty("description", "James Newton-King's blog."),
        new JProperty("item",
          new JArray(
            from p in posts
            orderby p.Title
            select new JObject(
              new JProperty("title", p.Title),
              new JProperty("description", p.Description),
              new JProperty("link", p.Link),
              new JProperty("category",
                new JArray(
                  from c in p.Categories
                  select new JValue(c)))))))));
//  "channel": {
//    "title": "James Newton-King",
//    "link": "",
//    "description": "James Newton-King's blog.",
//    "item": [
//      {
//        "title": "Json.NET 1.3 + New license + Now on CodePlex",
//        "description": "Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex",
//        "link": "",
//        "category": [
//          "Json.NET",
//          "CodePlex"
//        ]
//      },
//      {
//        "title": "LINQ to JSON beta",
//        "description": "Annoucing LINQ to JSON",
//        "link": "",
//        "category": [
//          "Json.NET",
//          "LINQ"
//        ]
//      }
//    ]
//  }
It isn't automatic like the serializer but it beats the hell out of JsonTextWriter or building JSON manually.

Feb 11, 2010 at 2:36 AM

Gotcha! Yes this is MUCH better, thank you. Works great :)