Deserialize JSON string in C# without using reflection

Viewed 2047

I'm working on a sandbox solution in Sharepoint, the important restrictions (regarding to this question) due to that are the use of .net 3.5 and there are no reflections allowed.


EXAMPLE 1

If I try to deserialize as JSON string into a simple class like this it works fine:

JSON STRING

{"field":"Picture1","url":"whatever"}

C# CLASS

public class PictureSetting
{
    public string field { get; set; }
    public string url { get; set; }
}

EXAMPLE 2

But if I try to deserialize a bit more complex string I get an error:

JSON STRING

{
  "Rows": [
    {
      "Columns": [
        {
          "Width": "100"
        }
      ]
    }    
  ]
}

C# CLASSES

internal class PageStructure
{
    public List<StructureElement> Rows { get; set; }
    public PageStructure()
    {
        Rows = new List<StructureElement>();
    }
}

internal class StructureElement
{
    public List<BlockAssigment> Columns { get; set; }
    public StructureElement()
    {
        Columns = new List<BlockAssigment>();
    }
}

internal class BlockAssigment
{
    public string Width { get; set; }
}

ERROR

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


*DESERIALIZATION

The code I use for deserialization of both examples is just standard .net:

var serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<PageStructure>("jsonstring");

So it seems that for the first example .net is not using reflection because that works. So the question is:

Is there a way to deserialize the second example without having .net using reflection internally?

Modifying both JSON string and C# classes are no problem, I just have to keep the structure (base object with rows including columns) somehow.

2 Answers
Related