PDA

View Full Version : Filter JSON output of webservice


K-Mile
08-21-2007, 06:02 PM
Hi,

I've been running into problems with my JSON code becoming bloated. We use spring.net with ASP.NET AJAX integration. The thing is, that when I send a 'User' with basic information set down the line, the JSON becomes something like:


{
Id: 42,
Name: "John Doe,
Address: null,
PhoneNumber: null,
Password: null,
FavoriteFood: null
}


Well, you get the picture. When I populate a list, I don't need all that extra info, but it is sent anyway. I would like to make a filter that skips all 'null' properties. Our javascript code is set up in such a way that we never rely on the difference between 'null' and 'undefined', so we should be alright there.

I am thinking of creating a simple stream rewriter in ANTLR3, that skips all properties with value 'null'. This should be implemented after ASP.NET AJAX and WebServiceExporter are finished with their processing, but I am not sure where to hook into ASP.NET. The AJAX library provides some configuration, but creating a custom converter for every domain object we have is quite tedious, so I'd rather let ASP.NET AJAX and spring.net do the heavy lifting and then filter the output.

Any thoughts on where to start? All I am looking for is the right hook point to get a string or stream with the generated JSON, but haven't been able to find it yet.

Thanks

Erich Eichinger
09-09-2007, 11:51 AM
Hi,

for filtering output streams, have a look at HttpResponse.Filter (http://msdn2.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx) property.

For filtering AJAX output, you could write your own IHttpModule that applies a filter on BeginRequest to the current Response object. Of course you should first check, if filtering should be done at all to avoid filtering all requests coming in.

-Erich