json serializer in Sharepoint
By Anatoly Mironov
There is a very handy JSON lib for serializing javascript objects. It is hosted on github under douglas crockford. Download json2.js and serialize with JSON.stringify function: EDIT: There is actually this function in core javascript. It exists since javascript 1.7 and ecmascript 5. So you don’t have to add anything to get this functionality:
var t = { name: "dev", city: "new york" };
JSON.stringify(t);
```There are actually built-in goodies for serializing javascript objects in [ASP.NET](http://msdn.microsoft.com/en-us/library/bb310857.aspx): Sys.Serialization.JavaScriptSerializer.serialize
var t = { name: “dev”, city: “new york” }; Sys.Serialization.JavaScriptSerializer.serialize(t);
[![](https://sharepointkunskap.files.wordpress.com/2011/12/json-serialize.png "json-serialize")](https://sharepointkunskap.files.wordpress.com/2011/12/json-serialize.png) I discovered it when I "studied" the javascript code which follows with every page. Will see if more goodies can be discovered there:
var _spWebPermMasks = { High: 2147483647, Low: 4294967295 }; var _spPageStateActionControlId = ‘ctl00_PageStateActionButton’; function GetPageNormalizedUrl() { alert(‘Denne koblingen for SharePoint Merker og kommentarer-verkt\u00F8yet er utdatert. Klikk OK for \u00E5 hente det nyeste Merker og kommentarer-verkt\u00F8yet. Klikk koblingen Mine merker i dialogboksen Merker og notatomr\u00E5det. Her f\u00E5r du tilgang det nye SharePoint Merker og kommentarer-verkt\u00F8yet.’); return document.location.href; } var _openTagDialog_DialogUrl = ‘’; function OpenTagDialog(dialogUrl) { _openTagDialog_DialogUrl = dialogUrl; SafeRunFunction(_OpenTagDialogInternal, ‘SocialData.js’, ‘TagDialogOpener’); } function _OpenTagDialogInternal() { var normalizer = PageUrlNormalizer.GetInstance(); normalizer.GetUrl(‘http:\u002f\u002fdev\u002fSider\u002fhei.aspx’, function (normalizedUrl) { var url = _openTagDialog_DialogUrl + ‘?Title=’ + encodeURIComponent(document.title) + ‘&Src=’ + ‘bm’ + ‘&DisplayUrl=’ + encodeURIComponent(document.location.href) + ‘&Url=’ + encodeURIComponent(normalizedUrl); var options = ’toolbar=0,status=0,resizable=0,scrollbars=1,width=650,height=400’; if (!window.open(url, null, options)) { document.location.href = url; } }); } function InitializeSocialRibbonComponentWrapper_ctl00_ctl34() { SafeRunFunction(InitializeSocialRibbonComponent_ctl00_ctl34, ‘sp.ui.socialribbon.js’, ‘SP.UI.SocialData.TRCRibbonComponent’); } function InitializeSocialRibbonComponent_ctl00_ctl34() { SP.UI.SocialData.TRCRibbonComponent.RegisterWithPageManager(); var ribbonComponent = SP.UI.SocialData.TRCRibbonComponent.get_Instance(); ribbonComponent[‘EnableSocialComment’] = true; ribbonComponent[‘EnableSocialTag’] = true; ribbonComponent[‘ListID’] = ‘{59b06168-7ff3-4a36-9af4-e4fe3156b22a}’; ribbonComponent[‘ResAddMessage’] = ‘Legg til merke…’; ribbonComponent[‘ResVerifyTypeMessage’] = ‘Kontrollerer…’; ribbonComponent.AddQuickTag = function (quickTagId) { SafeRunFunction(function () { var adder = new SocialQuickTag(‘ctl00_ctl34’); adder.title = document.title; adder.url = ‘http:\u002f\u002fdev\u002fSider\u002fhei.aspx’; adder.tagged_text = ‘Merket siden med Jeg liker det.’; adder.failure_text = ‘Kan ikke merke siden.’; adder.AddQuickTag(quickTagId); }, ‘SocialData.js’, ‘SocialQuickTag’); }; ribbonComponent.AddQuickTagToListItem = function (quickTagId) { var notificationId = addNotification(“Legg til merke…”, true); var listAndItemIds = this.SelectedListAndItemIds().split(’|’); var request = new Object(); request.listid = listAndItemIds[0]; request.itemids = listAndItemIds[1]; request.id = quickTagId; WebForm_DoCallback(‘ctl00$ctl34’, Sys.Serialization.JavaScriptSerializer.serialize(request), Function.createDelegate(ribbonComponent, ribbonComponent._OnSucceededCallback), notificationId, Function.createDelegate(ribbonComponent, ribbonComponent._OnFailedCallback), true); }; return ribbonComponent; } ExecuteOrDelayUntilScriptLoaded(InitializeSocialRibbonComponentWrapper_ctl00_ctl34, ‘sp.ribbon.js’); function ctl00_ctl34_SocialNavControl_insertMyProfileMenu() { var menus = document.getElementsByTagName(‘menu’); if (menus == null) return; var menu = null; for (var i = 0, len = menus.length; i < len; i++) { if (menus[i].id.lastIndexOf(‘PersonalActionMenu’) != -1) { menu = menus[i]; break; } } if (menu == null) return; var elm = document.createElement(‘ie:menuitem’); elm.setAttribute(‘menugroupid’, ‘50’); elm.setAttribute(‘description’, ‘Vis og administrer profilen.’); elm.setAttribute(’text’, ‘Min profil’); elm.setAttribute(‘onmenuclick’, ‘STSNavigate2(event,\‘http:\u002f\u002fdev:80\u002fmy\u002fPerson.aspx?accountname=CONTOSO\u00255CAdministrator\’)’); elm.setAttribute(‘id’, ‘ID_MySiteLinksMenu’); var elm2 = document.createElement(‘ie:menuitem’); elm2.setAttribute(‘menugroupid’, ‘50’); elm2.setAttribute(‘description’, ‘\u00C5pne den personlige hjemmesiden’); elm2.setAttribute(’text’, ‘Mitt omr\u00E5de’); elm2.setAttribute(‘onmenuclick’, ‘STSNavigate2(event,\‘http:\u002f\u002fdev:80\u002fmy\u002f\’)’); elm2.setAttribute(‘id’, ‘ID_MySiteMenu’); var sep = null; for (var i = 0, len = menu.childNodes.length; i < len; i++) { if (menu.childNodes[i].id == undefined || menu.childNodes[i].id == ‘’) { sep = menu.childNodes[i]; break; } } if (sep != null) menu.insertBefore(sep.cloneNode(true), menu.firstChild); menu.insertBefore(elm, menu.firstChild); if (sep != null) menu.insertBefore(sep.cloneNode(true), menu.firstChild); menu.insertBefore(elm2, menu.firstChild); }; _spBodyOnLoadFunctionNames.push(‘ctl00_ctl34_SocialNavControl_insertMyProfileMenu’); function ctl00_ctl38_AddQuickTag() { SafeRunFunction(function () { var o = new SocialQuickTag(‘ctl00_ctl38’); o.url = ‘http:\u002f\u002fdev\u002fSider\u002fhei.aspx’; o.title = document.title; o.tagged_text = ‘Merket siden med Jeg liker det.’; o.failure_text = ‘Kan ikke merke siden.’; o.replaceImg_style = ‘border:0; left:-0px !important;top:-332px !important;position:absolute;’; o.AddQuickTag(0); }, ‘SocialData.js’, ‘SocialQuickTag’); } function ctl00_ctl38_GetSocialNotification() { SafeRunFunction(function () { var o = new SocialTagsAndNotes(‘ctl00_ctl38’); o.url = ‘http:\u002f\u002fdev\u002fSider\u002fhei.aspx’; o.tooltip = ‘Merker hjelper deg \u00E5 huske koblinger og klassifisere siden. Kommentarene er offentlige. Merker og kommentarer legges inn i nyhetsfeeden og fungerer p\u00E5 tvers av forskjellige omr\u00E5der.’; o.notify_text = ‘{0}-merker eller kommentarer p\u00E5 denne siden. ‘; o.replaceImg_style = ‘border:0; left:-0px !important;top:-332px !important;position:absolute;’; o.GetSocialNotification(); }, ‘SocialData.js’, ‘SocialTagsAndNotes’); }