DataContractJsonSerializer - Json serialization built in to .Net 3.5

if you are ever trying to generate javascript objects server side, you should take a look at DataContractJsonSerializer. We used to have this ugly aspx code to spew out a list Urls as a JSON array:

thumbUrls = [<% if(this.ThumbnailUrls.Count > 0) {

for (int i=0; i<this.ThumbnailUrls.Count - 1; i++){ %>

<%= AntiXss.JavaScriptEncode(this.ThumbnailUrls[i]) %>,<% } %>

<%= AntiXss.JavaScriptEncode(this.ThumbnailUrls[this.ThumbnailUrls.Count - 1]) %>

<% } %> ];

This is now replaced with the following in the ascx:

ThumbnailLoader.thumbUrls = <%= this.ThumbnailUrlsToJson %>;

And the following in the cs codebehind:

image 

Which yields the following beautiful, compact encoded JSON all on one line:

thumbUrls = ["https:\/\/sx7ydg.dc1files.livefilestore-int.com\/y1phXFnqIQmEILOwPXTJHzX6mr5hvunDRWHPiA3MfCKmE95hG5ztMSOcy2-rMDc-csXv02Yh5JagAs\/canada20.jpg","https:\/\/sx7ydg.dc1files.livefilestore-int.com\/y1p8bBAlNyOkenz_AOQWBE2uu2h1Ygr-bzb38SFwbB3-S_omOdNheUHkxvzIAsAzygBxS-7yPO8Fw4\/c4877_color.bmp","https:\/\/jo4b3g.dc1files.livefilestore-int.com\/y1pWK3OlwUeHDJ2_hQRsMVMTPA-Ag_ibzOfgWqu9lxi7SDOq3jNzmNsdo2JZgPv6_rO93jqV3W5RJE\/10.tiff","https:\/\/jo4b3g.dc1files.livefilestore-int.com\/y1p04Ff3-o4kOOehSsPJ-AYdvfpje-PaGMfA11CcN9zHEdWJalL-PFT1kTfzR_IbsaQBnAWQ_OBXak\/bcb_carat.gif"]

Published 28 March 08 03:07 by joshzana

Comments

No Comments
Anonymous comments are disabled

Search

This Blog

Syndication

Page view tracker