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!!!

TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
  • Care to show example signature of your service method ? – Ondrej Svejdar Nov 23 '15 at 09:42
  • Possible duplicate of [How to implement custom JSON serialization from ASP.NET web service?](http://stackoverflow.com/questions/159704/how-to-implement-custom-json-serialization-from-asp-net-web-service) – Ondrej Svejdar Nov 23 '15 at 09:57
  • here is answer to your question [Pascal case dynamic properties with Json.NET](http://stackoverflow.com/questions/9247478/pascal-case-dynamic-properties-with-json-net) – makison Nov 23 '15 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. – TheVillageIdiot Nov 23 '15 at 11:39
  • @OndrejSvejdar it is `PageMethod` (or `WebMethod`). I've added sample code. – TheVillageIdiot Nov 23 '15 at 11:39
  • @TheVillageIdiot I understood, I didn't check it myself but according to description it doing exactly what you need – makison Nov 23 '15 at 11:50
  • @AlekseyNosik yes it is definitely doing what I want. Only thing is that either there is no way to plug custom JSON serializer in webforms or I don't know about it at least yet :) – TheVillageIdiot Nov 23 '15 at 11:55
  • @TheVillageIdiot you wrote you are using Newtonsoft.JSON and answer is also about it, it's from description "JSON.NET already provides a ExpandoObjectConverter so with some little adjustments you have what you want." – makison Nov 23 '15 at 12:01
  • @alekseynosik that is when I do serialisation, explicitly calling json. Net seeializer and send objects as string. The problem part is with reverse traffic, as. Net desirialises it automatically and fails. There is a way to bring strings and the desirialise but that will mean doing it manually in many places. – TheVillageIdiot Nov 23 '15 at 12:07
  • @TheVillageIdiot as I understood you are trying to do it with JavaScriptSerialization class, I don't see any reason not to use JSON.NET here – makison Nov 23 '15 at 12:14
  • @TheVillageIdiot sorry missed that you, explicitly calling json, it won't be easy for sure, I see only 3 ways to make it in easier way, first is to change input value to string, second is to move this logic to client and make similar to [this](http://stackoverflow.com/questions/12931828/convert-returned-json-object-properties-to-lower-first-camelcase) and last one is simply to change your c# to firstName and so on – makison Nov 23 '15 at 12:59

2 Answers2

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.

Avinash Jain
  • 7,200
  • 2
  • 26
  • 40
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>.

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89