Josef Stromsky
Josef Stromsky

Reputation:

How to serialize only selected properties when using web services in ASP.NET application

In most cases we are using ASP.NET web services (System.Web.Script.Services.ScriptService) in our AJAX based web applications. This approach brings a major advantage providing automatically all the server-side classes (used in the web service methods) on client side in form of JavaScript alternatives (JavaScript classes with appropriate namespace).

However, in our applications we are working with classes based on a 3rd party framework. These classes inherit many unserializable properties that makes them unusable for the automatic web service serialization into JSON and back once sent to the client side.

Is there a way to define the list of properties to serialize to JSON and back (once send to client and back) for appropriate class?

Upvotes: 0

Views: 206

Answers (1)

Randy Levy
Randy Levy

Reputation: 22655

There are two approaches I can think of:

  1. Wrap your 3rd party classes in your own classes where you can control what properties get exposed to the serializer. This has the benefit that it should allow you to expose the data with any web service implementation (e.g. JSON, XML).

  2. Create a JavaScriptConverter class that will let you implement custom serialization and deserialization of the third party classes.

Upvotes: 2

Related Questions