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?