3

Well it can be done in MVC and WebAPI. But is there a way to change default serializer in ASP.Net Web Forms? We are using Newtonsoft.JSON to send objects over the wire. It uses CamelCasePropertyName so FirstName in C# becomes firstName in JSON. Now when we need to pass objects back to server for web methods to consume, we have to create new object with FirstName property instead of firstName otherwise ASP.Net complains and request fails.

I sincerely hope that there is something very easy and straight-forward that I'm missing.

EDIT:- Well that all is well and good to overcome these issues by implementing custom converts for types or by using web service or wcf or moving to MVC/WebAPI. What we are trying to achieve is by using page method with classic (if it is right to say so) ASP.Net web-forms.

I was looking for some way to plug in JSON.Net serializer (Newtonsoft one) in place of default JavaScriptSerializer as we can do in MVC/WebAPI stack.

Sample code:

//class
public class Student
{
    public string FirstName{get;set;}
}

//WebMethod in ASPX code-behind
[WebMethod]
public static string UpdateStudent(Student s)
{
    //code to update object and send some response back.
}

MAIN ISSUE

When getting data from browser in json format with camel case, default JavaScriptSerialization is not able to handle property names.

But if I send custom object like:

var o={FirstName:'John'};

it works!!!

11
  • Care to show example signature of your service method ? Commented Nov 23, 2015 at 9:42
  • Possible duplicate of How to implement custom JSON serialization from ASP.NET web service? Commented Nov 23, 2015 at 9:57
  • here is answer to your question Pascal case dynamic properties with Json.NET Commented Nov 23, 2015 at 10:11
  • @AlekseyNosik It is already pascal cased when sending as I'm using JSON.Net to serialize objects. The issue is when browser is sending data to PageMethod, it is not able to properly convert properties from camel case to pascal case. Commented Nov 23, 2015 at 11:39
  • @OndrejSvejdar it is PageMethod (or WebMethod). I've added sample code. Commented Nov 23, 2015 at 11:39

2 Answers 2

0

Both C# and Javascript are case sensitive language. So FirstName and firstName treated differently.

If you don't want to use Newtonsoft.JSON then, You can use DataContractJsonSerialize to Serialize and Deserialize JSON Data into C# type. To do this you should have matching C# type.

Please check the following MSDN links that will help you.

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

2 Comments

ummm, isn't that about WCF?
the serialization logic is standard logic and can be use in ASP.Net, MVC, WCF, c# etc
0

If you're not interested in WCF (which would be my personal preference over page methods) and converters (agree on that - setting something complex like that for serialization of json is overkill), all left to do is hack:

[WebMethod]
public static string UpdateStudent(Dictionary<string, object> studentContent)
{
  // serialize generic json dictionary to json - once again :)
  string json = JsonConvert.SerializeObject(studentContent);
  // now you have string you can "properly" deserialize
  var student = JsonConvert.DeserializeObject<Student>(json);
  // ...
  return string.Empty;
}    

Pretty much any json object can be described as Dictionary<string, object> where value can be of either string type or another Dictionary<string, object>.

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.