0

I'm trying to serialize an object to JSON string as shown below:

new JavaScriptSerializer().Serialize(person)

Here, person is the object which is having lot of attributes such as name, address, city, state, etc but I have decorated the class as shown below so that it'll serialize only name and address.

using System;
using System.Runtime.Serialization;

namespace DataAccess.Models
{
    [Serializable]
    [DataContract]
    public class Person
    {        
    public string Id { get; set; }

    [DataMember(Name = "full-name")]
    public string Name { get; set; }

    [DataMember(Name = "address")]
    public string Address { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Zip { get; set; }

    }
}

But when I run the program, the new JavaScriptSerializer().Serialize(person) gives me JSON with all the data including Id, City, State, Zip.

Why is it not giving me only full-name & address? It seems like it is completely ignoring these DataMember attributes.

When I use JsonConvert.SerializeObject(person) from Newtonsoft, everything works perfect & it serializes only Name & Address but JavascriptSerializer is giving all data.

Can any one tell me what could be the issue?

Freephone Panwal
  • 1,547
  • 4
  • 21
  • 39

1 Answers1

1

Consider using the [ScriptIgnore()] Attribute on those properties you don't want to be serialized.

http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute(v=vs.100).aspx

See here for a detailed list of how the JavaScriptSerializer will handle different types: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx

Edit:
Note that the JavascriptSerializer is not aware of the DataMember attributes and they therefore will not be used by the JavascriptSerializer (JSON.net will use them, but JSON.Net also defines it's own constructs for this: [JsonIgnore()] [JsonObject] and many other attributes support custom-naming). To accomplish this, try using the DataContractJsonSerializer in the System.Runtime.Serialization.Json namespace with some draw-backs.

See this question and answer for additional information:

JavaScriptSerializer.Deserialize - how to change field names

Community
  • 1
  • 1
xDaevax
  • 2,012
  • 2
  • 25
  • 36
  • But even without ScriptIgnore, it is giving me name attribute with Name as string in Json instead of full-name as key. That means it is not recognizing datamember at all. – Freephone Panwal Jun 10 '14 at 17:35
  • ScriptIgnore is not helpful as I mentioned that it only ignores those attributes but I still get the Name & Address as keys in Json where as it should have been small case full-name & address keys in JSON which means serialization is being ignored by JavascriptSerializer for some reason. – Freephone Panwal Jun 10 '14 at 17:45
  • Yes, I believe the edit to my answer and the linked SO question / answer explains why the JavaScriptSerializer misses those a DataMember attributes and causes the serializer to just use the raw property names. – xDaevax Jun 10 '14 at 17:46
  • Are you suggesting me to use JSON.net? As I mentioned I tried newtonsoft & that works perfectly fine. So, if I've to use 3rd party, probably I'll stick with newtonsoft. Is Json.net 3rd party? – Freephone Panwal Jun 10 '14 at 17:48
  • JSON.NET (Newtonsoft) is third party, but has wide adoption and community support. You could also use the DataContractJsonSerializer (part of the .NET framework and not third party) instead of the JavascriptSerializer and it will be able to read your attributes to correctly serialize your data. – xDaevax Jun 10 '14 at 17:50