Skip to content

Instantly share code, notes, and snippets.

@leekelleher
Last active March 16, 2016 10:19
Show Gist options
  • Save leekelleher/ef052edb72cf2f698b20 to your computer and use it in GitHub Desktop.
Save leekelleher/ef052edb72cf2f698b20 to your computer and use it in GitHub Desktop.
Umbraco: Converting and namespacing the JSON data to XML (for use with XSLT/XPath)
<root id="-1">
<HomePage id="1234" parentID="-1" level="1" creatorID="1" sortOrder="0" createDate="2014-12-16T00:00:00" updateDate="2014-12-16T00:00:00" nodeName="Home" urlName="home" path="-1,1156" isDoc="" nodeType="1111" creatorName="admin" writerName="admin" writerID="1" template="0" nodeTypeAlias="HomePage">
<archetypeLinks><![CDATA[{"fieldsets":[{"properties":[{"alias":"linkUrl","value":"http://www.example.com/"},{"alias":"linkLabel","value":"Website"}],"alias":"externalLink","disabled":false},{"properties":[{"alias":"linkUrl","value":"http://www.example2.com/"},{"alias":"linkLabel","value":"Webshop"}],"alias":"externalLink","disabled":false}]}]]></archetypeLinks>
<json:archetypeLinks xmlns:json="http://pimpmyxslt.com/json">
<fieldsets>
<properties>
<alias>linkUrl</alias>
<value>http://www.example.com/</value>
</properties>
<properties>
<alias>linkLabel</alias>
<value>Website</value>
</properties>
<alias>externalLink</alias>
<disabled>false</disabled>
</fieldsets>
<fieldsets>
<properties>
<alias>linkUrl</alias>
<value>http://www.example2.com/</value>
</properties>
<properties>
<alias>linkLabel</alias>
<value>Webshop</value>
</properties>
<alias>externalLink</alias>
<disabled>false</disabled>
</fieldsets>
</json:archetypeLinks>
</HomePage>
</root>
// Dumping this on GitHub gist, for reference. (Else I'll probably lose it) [LK]
public class JsonXmlXsltEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
umbraco.content.AfterContentCacheLoadNodeFromDatabase += (sender, e) =>
{
if (!sender.HasChildNodes)
return;
foreach (System.Xml.XmlNode item in sender.ChildNodes)
{
if (item.NodeType == System.Xml.XmlNodeType.Element)
{
if (item.HasChildNodes)
{
var inner = item.FirstChild;
if (inner.NodeType == System.Xml.XmlNodeType.CDATA)
{
var text = inner.InnerText;
if (text.DetectIsJson())
{
if (text.StartsWith("["))
{
text = "{\"arrayitem\":" + text + "}";
}
try
{
var jsonXml = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(text, item.Name, true);
var newElement = item.OwnerDocument.CreateElement("json", item.Name, "http://pimpmyxslt.com/json");
newElement.InnerXml = jsonXml.DocumentElement.InnerXml;
var newChild = item.OwnerDocument.ImportNode(newElement, true);
item.ParentNode.InsertAfter(newChild, item);
}
catch { /* do nothing */ }
}
}
}
}
}
};
}
}
@leekelleher
Copy link
Author

The work around the JsonConvert.DeserializeXmlNode call is a rough prototype... it needs further research and development. For example, it would need to handle nested JSON structures; (e.g. Nested Archetypes are escaped).

@greystate
Copy link

Would this work from the App_Code folder if one were to add the necessary @using statements?

@leekelleher
Copy link
Author

@greystate - in answer to your question, (over a year later), yes it would work from App_Code 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment