Questions tagged [javascriptserializer]

System.Web.Script.Serialization.JavaScriptSerializer is a class which provides JSON serialization and deserialization functionality for applications and web sites targetting the .NET Framework. Use this tag for questions about this specific class only. For more general JavaScript/JSON serialization questions use the [json] and [serialisation] tags.

System.Web.Script.Serialization.JavaScriptSerializer is a class which provides JSON serialization and deserialization functionality for applications and web sites targetting the .NET Framework.

Use this tag for questions about the Microsoft class. For more general JSON serialization questions use both the and tags.

MSDN Remarks

The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server. You cannot access that instance of the serializer. However, this class exposes a public API. Therefore, you can use the class when you want to work with JavaScript Object Notation (JSON) in managed code.

To serialize an object, use the Serialize method. To deserialize a JSON string, use the Deserialize or DeserializeObject methods. To serialize and deserialize types that are not natively supported by JavaScriptSerializer, implement custom converters by using the JavaScriptConverter class. Then register the converters by using the RegisterConverters method.

Example

public abstract class AsSerializable
{
 public string AsJson()
 {
  var serializer = new JavaScriptSerializer();
  return serializer.Serialize(this);
 }
}

public class Foo : AsSerializable
{
 public string Bar { get; set; }
}

string JSON = Foo.AsJson();

MSDN Article

  • JavaScriptSerializer

    JavaScriptSerializer Code

  • Code

    Performance Benchamrks

  • http://james.newtonking.com/archive/2008/10/27/json-net-3-5-beta-1-big-performance-improvements-compact-framework-support-and-more.aspx

  • http://www.servicestack.net/benchmarks/
  • 420 questions
    1409
    votes
    31 answers

    JavaScriptSerializer - JSON serialization of enum as string

    I have a class that contains an enum property, and upon serializing the object using JavaScriptSerializer, my json result contains the integer value of the enumeration rather than its string "name". Is there a way to get the enum as a string in my…
    Omer Bokhari
    • 57,458
    • 12
    • 44
    • 58
    363
    votes
    19 answers

    How do I get formatted JSON in .NET using C#?

    I am using .NET JSON parser and would like to serialize my config file so it is readable. So instead of: {"blah":"v", "blah2":"v2"} I would like something nicer like: { "blah":"v", "blah2":"v2" } My code is something like this: using…
    user34537
    74
    votes
    9 answers

    JavaScriptSerializer.Deserialize - how to change field names

    Summary: How do I map a field name in JSON data to a field name of a .Net object when using JavaScriptSerializer.Deserialize ? Longer version: I have the following JSON data coming to me from a server API (Not coded in .Net) {"user_id":1234,…
    Anthony
    • 5,176
    • 6
    • 65
    • 87
    68
    votes
    17 answers

    How to not serialize the __type property on JSON objects

    Every object I return from a WebMethod of a ScriptService is wrapped into a JSON object with the data in a property named d. That's ok. But I don't want the additional __type property to be served to the client, since I do manual processing with…
    Robert
    47
    votes
    8 answers

    error CS0234: The type or namespace name 'Script' does not exist in the namespace 'System.Web'

    I am trying to use JavaScriptSerializer in my application. I initially received Cannot find JavaScriptSerializer and I solved it by adding: using System.Web.Script.Serialization; But then the sub-keyword Script is underlined with a blue…
    scatmoi
    • 1,958
    • 4
    • 18
    • 32
    39
    votes
    7 answers

    Can JavaScriptSerializer exclude properties with null/default values?

    I'm using JavaScriptSerializer to serialize some entity objects. The problem is, many of the public properties contain null or default values. Is there any way to make JavaScriptSerializer exclude properties with null or default values? I would like…
    frankadelic
    • 20,543
    • 37
    • 111
    • 164
    37
    votes
    6 answers

    Deserializing a JSON file with JavaScriptSerializer()

    the json file's structure which I will deserialize looks like below; { "id" : "1lad07", "text" : "test", "url" : "http:\/\/twitpic.com\/1lacuz", "width" : 220, "height" : 84, "size" : 8722, "type" : "png", "timestamp"…
    tugberk
    • 57,477
    • 67
    • 243
    • 335
    34
    votes
    4 answers

    Serializing a decimal to JSON, how to round off?

    I have a class public class Money { public string Currency { get; set; } public decimal Amount { get; set; } } and would like to serialize it to JSON. If I use the JavaScriptSerializer I get {"Currency":"USD","Amount":100.31000} Because…
    Halvard
    • 3,891
    • 6
    • 39
    • 51
    27
    votes
    4 answers

    How to set formatting with JavaScriptSerializer when JSON serializing?

    I am using JavaScriptSerializer for serializing objects to the file to the JSON format. But the result file has no readable formatting. How can I allow formating to get a readable file?
    Peter17
    • 3,052
    • 9
    • 47
    • 77
    24
    votes
    4 answers

    How to add `System.Web.Extensions` assembly to .net core project in vscode

    I have installed c# support for vscode(version is 1.15.0) and created a HelloWorld project via dotnet new console. Now in Program.cs, I would like to use JavaScriptSerializer class which depends on System.Web.Extensions assembly. I typed using…
    39ecneret
    • 661
    • 3
    • 7
    • 17
    21
    votes
    3 answers

    JavaScriptSerializer. How to ignore property

    I know about ScriptIgnoreAttribute. But what if I want to ignore a property based on criteria. For example how to ignore a nullable property on serialization only if it's null and doesn't contain any value?
    iLemming
    • 34,477
    • 60
    • 195
    • 309
    20
    votes
    3 answers

    JavaScriptSerializer is subtracting one day from date

    I am using JavaScriptSerializer for serializing DateTime, but when I deserialize it show one day less from the date it get serialize: Here is test: DateTime startDate=new DateTime(2012,1,20);//set the 20th of January JavaScriptSerializer …
    Zaheer Ahmed
    • 28,160
    • 11
    • 74
    • 110
    16
    votes
    2 answers

    How can I do System.Web.Script.Serialization in C#?

    How can I do this in C# modern UI ? var url = "http://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fwww.digg.com%2Frss%2Findex.xml&v=1.0"; var wc = new WebClient(); var rawFeedData = wc.DownloadString(url); //You can use…
    tovaz
    • 193
    • 1
    • 2
    • 8
    16
    votes
    5 answers

    JavaScriptSerializer.MaxJsonLength exceeded. What's the best practice for handling this?

    I've got a large amound of data I'm sending down to the client using jQuery's $.ajax() function. I'm calling a method in a ASP.NET web service that returns the JSON data. Everything is great for most searches, but when I've got a large data set to…
    Tim Scarborough
    • 1,270
    • 1
    • 11
    • 22
    15
    votes
    5 answers

    JavaScriptSerializer namespace issue

    I am having a problem trying to implement the JavaScriptSerializer to parse a JSON string received from a server. I implemented the following code: responseFromServer = readStream.ReadLine(); JavaScriptSerializer ser = new…
    Jeff
    • 167
    • 1
    • 1
    • 8
    1
    2 3
    27 28