Added first pass of JSON-LD to TURTLE serialization support.
authorManu Sporny <msporny@digitalbazaar.com>
Fri, 08 Jul 2011 16:52:23 -0400
changeset 54 7dbe2a8f87fe
parent 53 31a80c96ce69
child 55 ca55facec680
Added first pass of JSON-LD to TURTLE serialization support.
playground/index.html
playground/jsonld-turtle.js
playground/playground-examples.js
playground/playground.js
--- a/playground/index.html	Fri Jul 08 14:19:51 2011 -0400
+++ b/playground/index.html	Fri Jul 08 16:52:23 2011 -0400
@@ -18,11 +18,11 @@
       <link rel="shortcut icon" href="favicon.ico" /> 
       <script type="text/javascript" src="jquery-1.6.1.min.js"></script> 
       <script type="text/javascript" src="jquery-ui-1.8.14.custom.min.js"></script>
-      <!-- script type="text/javascript" src="jquery.ui.tabs.min.js"></script --> 
       <script type="text/javascript" src="js_beautify.js"></script>
       <script type="text/javascript" src="prettify.js"></script>
       <script type="text/javascript" src="lang-jsonld.js"></script>
       <script type="text/javascript" src="jsonld.js"></script>
+      <script type="text/javascript" src="jsonld-turtle.js"></script>
       <script type="text/javascript" src="playground.js"></script>
       <script type="text/javascript" src="playground-examples.js"></script>
    </head>
@@ -39,7 +39,9 @@
             <div id="info hidden">
                <p>Play around with JSON-LD markup by typing out some JSON below
                and seeing what gets generated from it at the bottom of the page. 
-               Pick any of the examples below to get started.
+               Pick any of the examples below to get started. The 
+               <a href="http://json-ld.org/spec/latest/">specification</a>
+               is currently a work in progress.
             </div>
 
             <ul id="examples">
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/playground/jsonld-turtle.js	Fri Jul 08 16:52:23 2011 -0400
@@ -0,0 +1,118 @@
+/**
+ * Javascript implementation of TURTLE output for JSON-LD.
+ *
+ * @author Manu Sporny
+ *
+ * Copyright (c) 2011 Digital Bazaar, Inc. All rights reserved.
+ */
+(function()
+{
+var jsonld = forge.jsonld;
+
+function getProperties(obj)
+{
+   var rval = [];
+
+   for(var key in obj)
+   {
+      if(key != "@")
+      {
+         rval.push(key);
+      }
+   }
+
+   return rval;
+};
+
+function isBnode(iri)
+{
+   var bnodePrefix = "_:";
+   return (iri.substring(0, bnodePrefix.length) === bnodePrefix);
+};
+
+function iriToTurtle(iri)
+{
+   var rval = undefined;
+
+   if(isBnode(iri))
+   {
+      rval = iri;
+   }
+   else
+   {
+      rval = "<" + iri + ">";
+   }
+
+   return rval;
+};
+
+function printObject(obj)
+{
+   var rval = undefined;
+
+   // FIXME: Implement printing out arrays of objects
+
+   if(typeof(obj) == "object")
+   {
+      if("@literal" in obj && "@datatype" in obj)
+      {
+         rval = "\"" + obj["@literal"] + "\"^^<" + obj["@datatype"] + ">";
+      }
+      else if("@literal" in obj && "@language" in obj)
+      {
+         rval = "\"" + obj["@literal"] + "\"@" + obj["@language"];
+      }
+      else if("@iri" in obj)
+      {
+         var iri = obj["@iri"];
+         rval = iriToTurtle(iri);
+      }
+   }
+   else
+   {
+      rval = "\"" + obj + "\"";
+   }
+
+   return rval;
+};
+
+jsonld.turtle = function(input)
+{
+   var normalized = jsonld.normalize(input);
+   var rval = "";
+
+   for(s in normalized)
+   {
+      var subject = normalized[s];
+      var iri = subject["@"]["@iri"];
+
+      rval += iriToTurtle(iri) + "\n";
+
+      // get all properties and perform a count on them
+      var properties = getProperties(subject);
+      var numProperties = properties.length;
+
+      // iterate through all properties and serialize them
+      var count = numProperties;
+      for(p in properties)
+      {
+         property = properties[p];
+         rval += "   <" + property + "> " + printObject(subject[property]);
+
+         if(count == 1)
+         {
+            rval += ".\n";
+         }
+         else
+         {
+            rval += ";\n";
+         }
+         count -= 1;
+      }
+   }
+
+   return rval;
+};
+
+})();
+
--- a/playground/playground-examples.js	Fri Jul 08 14:19:51 2011 -0400
+++ b/playground/playground-examples.js	Fri Jul 08 16:52:23 2011 -0400
@@ -73,7 +73,7 @@
    // Add the example of a Product
    playground.examples["Product"] =
    {
-      "@subject": "http://example.org/cars/for-sale#tesla",
+      "@": "http://example.org/cars/for-sale#tesla",
       "a": "gr:Offering",
       "gr:name": "Used Tesla Roadster",
       "gr:description": "Need to sell fast and furiously",
--- a/playground/playground.js	Fri Jul 08 14:19:51 2011 -0400
+++ b/playground/playground.js	Fri Jul 08 16:52:23 2011 -0400
@@ -7,6 +7,17 @@
    window.playground = window.playground || {};
    var playground = window.playground;
 
+   playground.htmlEscape = function (s) 
+   {
+      return s.replace(/([&<>])/g, function (c) {
+         return "&" + {
+             "&": "amp",
+             "<": "lt",
+             ">": "gt"
+         }[c] + ";";
+      });
+   }
+
    playground.init = function()
    {
       $("#tabs").tabs();
@@ -30,32 +41,48 @@
 
    playground.process = function()
    {
+      var input = null;
+      var frame = null;
+
       try
       {
          var input = JSON.parse($("#markup").val());
-         var frame = JSON.parse($("#frame").val());
-         var normalized = forge.jsonld.normalize(input);
-         var expanded = forge.jsonld.removeContext(input);
-         var compacted = forge.jsonld.changeContext(
-            input["@context"] || {}, input);
-         var framed = forge.jsonld.frame(input, frame);
-
-         $("#normalized").html(js_beautify(JSON.stringify(normalized)),
-            { "indent_size": 3, "brace_style": "expand" });
-         $("#compacted").html(js_beautify(JSON.stringify(compacted)),
-            { "indent_size": 3, "brace_style": "expand" });
-         $("#expanded").html(js_beautify(JSON.stringify(expanded)),
-            { "indent_size": 3, "brace_style": "expand" });
-         $("#framed").html(js_beautify(JSON.stringify(framed)),
-            { "indent_size": 3, "brace_style": "expand" });
-
-         prettyPrint();
       }
       catch(e)
       {
          console.log(e);
-         $("#errors").html(e);
+         $("#markup-errors").html(e);
       }
+
+
+      try
+      {
+         var frame = JSON.parse($("#frame").val());
+      }
+      catch(e)
+      {
+         console.log(e);
+         $("#frame-errors").html(e);
+      }
+
+      var normalized = forge.jsonld.normalize(input);
+      var expanded = forge.jsonld.removeContext(input);
+      var compacted = forge.jsonld.changeContext(
+         input["@context"] || {}, input);
+      var framed = forge.jsonld.frame(input, frame);
+      var turtle = forge.jsonld.turtle(input);
+
+      $("#normalized").html(js_beautify(JSON.stringify(normalized)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#compacted").html(js_beautify(JSON.stringify(compacted)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#expanded").html(js_beautify(JSON.stringify(expanded)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#framed").html(js_beautify(JSON.stringify(framed)),
+         { "indent_size": 3, "brace_style": "expand" });
+      $("#turtle").html(playground.htmlEscape(turtle));
+
+      prettyPrint();
    }
    
    playground.populate = function(type)