Remove JSON string descriptor in C # without using reflection

I am working on a sandbox solution in Sharepoint, important limitations (regarding this issue) due to this is the use of .net 3.5, and there are no reflections.


EXAMPLE 1

If I try to deserialize as a 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 slightly more complex string, I get an error message:

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

A permission request of type 'System.Security.Permissions.ReflectionPermission, mscorlib, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089' failed.


* deserialization

The code I use to deserialize both examples is simply the standard .net:

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

So, for the first example, it seems .net does not use reflection, because it works. So the question is:

Is there a way to deserialize the second example without using .net with internal reflection?

Changing both JSON strings and C # classes is not a problem, I just have to save the structure (the base object with strings, including columns) in some way.

+6
source share
2 answers

I suspect that the biggest problem is actually here:

 public class PictureSetting // public, works 

against

 internal class PageStructure // non-public, permission denied 

it seems to me that the sandbox prevents the reflection of non-public types / members. This would be compatible with most sandboxes like Silverlight: you can use reflection, but only for things that could do fine, i.e. For access to public types / members. Try to make PageStructure , StructureElement and BlockAssigment into public classes.

+3
source

You can use this http://json2csharp.com/ service to create the correct C # clans to parse json.

-1
source

Source: https://habr.com/ru/post/922796/


All Articles