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
    4
    votes
    3 answers

    Json deserialization issues with Tuples?

    I have a class I use for configuration that holds a 3 tuple of all integers. public class Configuration { public List> MyThreeTuple { get; set; } public Configuration() { MyThreeTuple = new List
    KDecker
    • 6,928
    • 8
    • 40
    • 81
    4
    votes
    1 answer

    Deserialize json string back to expando using JavaScriptSerializer?

    I'm creating a json object on the fly ( without Json.net ) via : dynamic expando = new ExpandoObject(); expando.Age = 42; expando.Name = "Royi"; expando.Childrens = new ExpandoObject(); expando.Childrens.First = "John"; Which looks like : And…
    Royi Namir
    • 144,742
    • 138
    • 468
    • 792
    4
    votes
    3 answers

    How do I resolve an exception for exceeding maxJsonLength in JavaScriptSerializer?

    I have found a few answers that address this issue in the context of a large response, but none for my context of a large request. I get the following exception when posting a large file to an action method: Error during serialization or…
    ProfK
    • 49,207
    • 121
    • 399
    • 775
    4
    votes
    2 answers

    Empty JSON objects ([{},{},..]) are written to file

    im trying to write a simple list to json. no errors, executes fine but i get this output [{},{},{}] here is a snippet of my code. studentList is a list of objects of Student class. public void jsonRead() { string json =…
    Sohaib Jamal
    • 109
    • 12
    4
    votes
    1 answer

    DayLight Saving Time issue of DateTime from JavaScriptSerializer.Serialize to ToLocalTime

    Previously I was facing this issue and solved as described in the post. Currently after DayLight Saving implementation I observed the issue that if I am selecting DateTime startDate=new DateTime(2012,1,20); //Eastern Timezone (UTC -5:00) after…
    Zaheer Ahmed
    • 28,160
    • 11
    • 74
    • 110
    4
    votes
    2 answers

    .NET's JavaScriptSerializer.Deserialize() ignores decimal period in numbers from JSON

    I'm using JavaScriptSerializer.Deserialize() to get data from a JSON file. But it ignores the decimal period, although using .GetType() on the value, returns System.Decimal. This is the C# code: JavaScriptSerializer jss = new…
    Petruza
    • 11,744
    • 25
    • 84
    • 136
    4
    votes
    2 answers

    How to troubleshoot KeyNotFoundException

    I am using the following to deserialize a JSON string into my own class: JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary x = (Dictionary)serializer.DeserializeObject(json); MyJsonStruct data =…
    Not So Sharp
    • 1,149
    • 2
    • 10
    • 20
    4
    votes
    1 answer

    This seems like a bug in .NET's JavaScript deserialize... is it?

    Basically, I found that a JSON string that is a JavaScript object (associative array) with an empty string for one of its properties/keys will cause the built in .NET Serializer to throw an exception. For example this code: string json = "{ \"\" :…
    lucidquiet
    • 6,124
    • 7
    • 51
    • 88
    4
    votes
    3 answers

    In C# how can I deserialize this json when one field might be a string or an array of strings?

    I have an asp.net-mvc website and i am reading in Json string from a Database. Here is the following json in a DB. It could look like this: {"description": "Test", "contacts": ["joe@gmail.com", "bill@yahoo.com"], "enabled": true} or…
    leora
    • 188,729
    • 360
    • 878
    • 1,366
    3
    votes
    2 answers

    Parsing Unknown JSON using JavascriptSerializer in C#

    How can I use JavaScriptSerializer to parse some unknown dynamic JSON. In particular, I'm writing my own wrapper for the Google Calendar API. An event has an object called extendedProperties with both a private object and shared object containing an…
    Jason Butera
    • 2,376
    • 3
    • 29
    • 46
    3
    votes
    1 answer

    ASP.NET MVC JSON over Fluent Nhibernate Model

    I am trying to return JsonResult using MVC controller standard Json(object) method. My object of type Model1 is built by Fluent NHibernate. Model1 has property of type Model2. In debug mode I see that the environment creates a proxy descendant class…
    3
    votes
    2 answers

    Azure time zone and javascriptserializer object

    I have a predictions-based app that's up on Windows Azure ( http://ipredikt.com ). From what I can tell Azure's clock is synchronized to the GMT time zone. Here is a problem that I am encountering: Let's say I have a DB field called CreateDate of…
    Archil Kublashvili
    • 696
    • 1
    • 8
    • 20
    3
    votes
    1 answer

    Make Entity Framework object into generic class via extension class?

    I have an EF object called PagePreference. The corresponding table contains three fields: UserName(PK, varchar(200), not null), PageName(PK, varchar(700), not null), and PageState(text, null). The PageState field contains a serialized (json)…
    Trevor
    • 13,085
    • 13
    • 76
    • 99
    3
    votes
    1 answer

    Create JSON object from string in C#

    I am trying to create a JSON string which contains one container and one array. I can do this by using a stringbuilder but I am trying to find a better way to get the JSON string; I want: { "message":{ "text":"test sms"},"endpoints":["+9101234"]}…
    Dilip
    • 697
    • 6
    • 16
    • 35
    3
    votes
    1 answer

    How to parse a JSON string in to a name value pair in C#

    I have to parse the JSON string in to a name value pair list : {"vars":[ {"name":"abcd","value":"true"}, {"name":"efgh","value":"false"}, {"name":"xyz","value":"sring1"}, {"name":"ghi","value":"string2"}, {"name":"jkl","value":"num1"} …
    VaasLolla
    • 51
    • 1
    • 1
    • 5