0

I've got c# objects that I need references to in javascript for default population. Currently I'm maintaining 2 different objects which is not that maintainable.

For example ( simplified for demo purposes ):

C#

public class Text
{
     public string Name {get;set;}
}

JSON

{
     'text': {
          name: undefined
     }
}

I know there is a number of ways to accomplish this but wondering if anyone has a recommended solution. Thanks!

amcdnl
  • 8,470
  • 12
  • 63
  • 99

3 Answers3

7

I personally recommend json.NET. Getting the json of any object is as simple as;

 using Newtonsoft.Json;

 string json = JsonConvert.SerializeObject(new Text { Name = "test" });

There are a lot of other options but I've been using it since before there was json serilization support in .NET and I strongly prefer it over what is there now. In fact I think it's better in every way, if you want a big robust data layer I like it more and it's vastly superior for one off serilizations.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
2

If you are using .NET 4.0 or above, you can use DataContractJsonSerializer class.

addy2601
  • 387
  • 2
  • 12
0

I recommend you to look at this benchmark http://theburningmonk.com/2013/09/binary-and-json-serializer-benchmarks-updated/

UnusMundus
  • 21
  • 1