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
    15
    votes
    1 answer

    JavaScriptSerializer UTC DateTime issues

    Our client wanted to show the date and time values in the browser exactly as they are in the database, and we are storing them as UTC in the database. At first we had some problems with the serialization and Javascript side. The DateTime values got…
    JustAMartin
    • 13,165
    • 18
    • 99
    • 183
    14
    votes
    1 answer

    serializing data using json.net size limit?

    I have developed an asp.net web application and initially serialized some data to a json string using the built in javascript serializer. This became problematic as I discovered later on that there was an issue about the amount of data I was able to…
    John D
    • 639
    • 1
    • 10
    • 20
    14
    votes
    3 answers

    JSON serializing an object with function parameter

    I have this C# object: var obj = new { username = "andrey", callback = "function(self) { return function() {self.doSomething()} (this) }" } I need to JSON serialize it to pass to the browser in ajax call. I use JavascriptSerializer, but it…
    Andrey
    • 20,487
    • 26
    • 108
    • 176
    14
    votes
    1 answer

    JSON deserialization to inherited types

    I have a data table in my database where I store various settings. Since they are of any type (even complex object graphs) I decided to store their values as serialized JSON strings. Let's say that I serialized a List. Serialized string…
    Robert Koritnik
    • 103,639
    • 52
    • 277
    • 404
    14
    votes
    7 answers

    From DataTable in C# .NET to JSON

    I am pretty new at C# and .NET, but I've made this code to call a stored procedure, and I then want to take the returned DataTable and convert it to JSON. SqlConnection con = new SqlConnection("connection string here"); SqlDataAdapter da =…
    cc0
    • 1,960
    • 7
    • 40
    • 57
    14
    votes
    4 answers

    How to get JSON response from a 3.5 asmx web service

    I have the following method: using System.Web.Services; using System.Web.Script.Services; using System.Web.Script.Serialization; using Newtonsoft.Json; using System.Collections; [WebService(Namespace =…
    Mansinh
    • 1,365
    • 4
    • 19
    • 47
    14
    votes
    2 answers

    How to serialize a raw json field?

    I have a field in the db that store a json string and I want that when I return it in a json result that will be returned as json raw data and not warped with quotes as string. UPDATE 1(More Info): if you looking at the images field it contain a…
    ygaradon
    • 2,198
    • 2
    • 21
    • 27
    13
    votes
    6 answers

    ASP.NET WebService is Wrapping my JSON response with XML tags

    I'm not sure where I'm going wrong of what I'm missing. I'm building an ASP.NET 2.0 (on the .Net 3.5 framework) Web application and I am including a webservice. Note that this is not an MVC project. I wish to expose a method which will return a…
    Mike
    • 5,568
    • 11
    • 31
    • 45
    13
    votes
    2 answers

    JavaScriptSerializer circular reference when using ScriptIgnore

    I have my Entity Framework Entities split out into a separate class library from my web project and data access layer. In my controller I make a call to my repository to get an IEnumerable and then try to serialize into json…
    12
    votes
    2 answers

    Binding DateTime to knockout view model with default JavaScriptSerializer

    I've just started using knockout and I'm running into trouble with DateTime Serialization and Deserialization using the JavaScriptSerializer. I've updated the gifts model in Steves koListEditor example from his blog to include a Modified DateTime…
    CraftyFella
    • 7,520
    • 7
    • 47
    • 61
    11
    votes
    2 answers

    convert json to c# list of objects

    Json string: {"movies":[{"id":"1","title":"Sherlock"},{"id":"2","title":"The Matrix"}]} C# class: public class Movie { public string title { get; set; } } C# converting json to c# list of Movie's: JavaScriptSerializer jss = new…
    Ian Davis
    • 19,091
    • 30
    • 85
    • 133
    11
    votes
    1 answer

    JSON Maximum length problem with ASP.NET

    I am creating a asp.net 2.0 webservice which give json as output and there's a very large, can't be break down, dataset which exceed the max length limit I have search on the internet, and there's solution on .net 3.5 & 4, but not 2.0. Can any tell…
    Chito Cheng
    • 540
    • 2
    • 9
    • 25
    11
    votes
    1 answer

    How do you get System.Web.Script.javascriptSerializer to ignore a property?

    [Serializable] public class ModelResource:ISerializable { public Int64 Ore { get; private set; } public Int64 Crystal { get; private set; } public Int64 Hydrogen { get; private set; } //needs to be ignored public Int64 Total { get {…
    Maslow
    • 18,464
    • 20
    • 106
    • 193
    10
    votes
    5 answers

    Serializing dictionaries with JavaScriptSerializer

    Apparently, IDictionary is serialized as an array of KeyValuePair objects (e.g., [{Key:"foo", Value:"bar"}, ...]). Is is possible to serialize it as an object instead (e.g., {foo:"bar"})?
    Daniel
    • 47,404
    • 11
    • 101
    • 179
    10
    votes
    1 answer

    "<" character in JSON data is serialized to \u003c

    I have a JSON object where the value of one element is a string. In this string there are the characters "". I take this entire JSON object and in my ASP.NET server code, I perform the following to take the object named rpc_response and add…
    gnychis
    • 7,289
    • 18
    • 75
    • 113
    1
    2
    3
    27 28