开发者

JSON serializer and CSLA objects

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 prop开发者_高级运维erties (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?


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.


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


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜