views:

71

answers:

3

We just started a new ASP.Net project that uses web services to serialize some CSLA business objects into JSON data for our client javascript/JQuery code. After reviewing the JSON data in the client browser(Firebug in Firefox) we notices that there are a significant number of properties from the business object that we do not need downloaded to the browser.

Is there a way to exclude properties (other than marking them private) from getting serialized by the JSON serializer? We are not calling the JSONSerializer directly, but instead just included a ScriptMethod declaration on the WebMethod.

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public Function getQuestions()

UPDATE We tried the suggestion of adding the attribute on the public property but received an error:

    Error 25 Attribute 'NonSerializedAtrribute' cannot be 
    applied to 'Name' because the attribute is not valid 
    on the is declaration type.

Now if we add NonSerialized to the class then it works but not on the property. However, we do want some properties to be serialized.

Any ideas?

A: 

you could try to place a NonSerializedAttribute on the properties not sure if it works with the Json serializer...

Edit: if you are using .net 4.0 you could try to use the ISerializable interface...

Petoj
I'll give that a shot. I'll let you know.
Jeff V
That didn't work. See error above.
Jeff V
+2  A: 

You should use ScriptIgnore attribute for all properties which should be not serialized.

If you decide to make more customization of data serialization, for example, replacing one properties name with another one or converting some properties in an array and so on you can write a small JavaScriptTypeResolver which do it.

Oleg
I will try this today! Thanks Oleg! I'll let you know.
Jeff V
Looks like it is working as described. Just as a side note we found that we needed to add a reference to System.Web.Extensions. Thanks for your help!
Jeff V
@Jeff V: You welcome Jeff!
Oleg
+1  A: 

ScriptIgnore should do the job for you as sugested by Oleg. Check out this link for a detailed sample

Vinay B R