0

I am trying to do a JSON schema validation dynamically. We are taking the JSON from an uploaded file and trying to validate this is exact JSON or not. This is an On premise application.I cannot use JSON.net. I cannot install any other third party tools to validate this. I have tried two ways as below.

1.I used System.Web.Helper and Using the below code, i am getting the error

code :

var jsonstring = "{\"user\":{\"name\":\"asdf\",\"age\":\"26\",\"teamname\":\"b\",\"email\":\"c\",\"players\":[\"1\",\"2\"]}}";

            var jsonObject = Json.Decode(jsonstring);

Error is "Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed". I searched for solutions and found that we need to uncheck a check box in debug properties of solution.But this only works in Console application for me.But not working in our VS 2012 solution. Is there any way to solve this issue?

  1. Next is, i tried to use JavaScriptSerializer as given below and successfully got the results in my objJson.

JavaScriptSerializer serializer = new JavaScriptSerializer();

string inputContent;

StreamReader inputStreamReader = new StreamReader(FileUploadControl.PostedFile.InputStream);

inputContent = inputStreamReader.ReadToEnd();

inputContent = inputContent.Replace("\r", "").Replace("\n", "").Replace("\t", "");

var objJson= serializer.Deserialize>(inputContent);

But when i am trying to get values of this resulted key values pair, i am getting error as in image. Error is Dynamic operations can only be performed in homogenous AppDomain. for this the solution found was to change the attribute "legacyCasModel=true" to "legacyCasModel=false" in web.config file. But as this is OnPremise application i cannot change this attribute.Please help on this.

Lintu
  • 1
  • 2

1 Answers1

0

Suggest you to have a look at this stackoverflow post
In nutshell what you want to do is :

  1. Perform some basic checks i.e. if JSON string starts with either
'{'
    or '['
  1. Try to parse JSON string using JToken.Parse if there is an exception while parsing simply log or display this to end user and move on ! you are just trying to validate JSON string not attempting to clean it right ?

PS : Please do refer the linked question for greater details.

Ankit
  • 5,733
  • 2
  • 22
  • 23
  • This is an On premise application.I cannot use JSON.net. I cannot install any other third party tools to validate this. – Lintu Aug 02 '17 at 15:02
  • Json.NET is a popular high-performance JSON framework for .NET. It is open source and free ! you can very simply install via nuget . check this link for details : https://stackoverflow.com/questions/4444903/how-to-install-json-net-using-nuget – Ankit Aug 02 '17 at 15:14