<!DOCTYPE html>
<html lang="en-AU">
<head>
 <meta charset="utf-8">
 <title>JSON-ND Example Parser: Javascript</title>
<script>
  const JSONND = 
  {
      Parse : function(ndObj, strict) 
	  {
        var failed = false;
		var failedMessage= "";
		var obj = JSON.parse(ndObj);     
		for (var propertyName in obj) {
			var p = propertyName.indexOf(":");
			var v = obj[propertyName];
			if (p > 0) {
				//specific check for <Big>int (not number) ECMAScript 2020
				if (propertyName.substr(p+1,3)==='int') 
				{
					var nv=parseInt(v); // it will truncate floats,
					if (strict) 
					{
					  if (!nv || (parseFloat(v)-nv!=0) )
					  {
					    failedMessage += '"'+propertyName+'" value <'+v+'> not a valid Integer;';  
					    failed=true;
						v=null;
					  }
					} else {
					  v=nv;
					} 
				}; // add more validation.
				obj[propertyName.substr(0, p)] = v;
				delete(obj[propertyName]);
			}
		};
		if (failed) throw failedMessage;
		return obj;
	  }
  };
</script>
</head>
<body>
  <h1>Example demonstration of JSON-ND parser.</h1>
  <script>
     var nd = '{"abc:string":"abc contains a string", "def:integer" : "not a number"}'; 
     document.write("<h3>Original 'nd' object:<pre>",nd,"<pre></h3>");  
     document.write("<p>See that there is no 'nd.abc' property: abc=<b>" ,nd.abc,"</b></p>");
	 document.write("<p>Attempting accessing 'nd.abc:string' property is not valid Javascript</p>");
     document.write("<h3>Then validate with JSON_ND_Parse:<pre>",JSON.stringify(JSONND.Parse(nd)),"</pre></h3>");
     document.write("<p style='color: red'>Note that 'def' failed validation, but not <i>strict</i> so the error is ignored</p>");
	 
	 document.write("<h3>With <b>strict</b> operator in place:");
	 try
	 {
	   var strictNd = '{"abc:string":"abc contains a string", "def:integer" : "20.5"}'; 
       document.write("<h3>'StrictNd' object:<pre>",strictNd,"<pre></h3>");  	   
	   document.write("<pre>",JSON.stringify(JSONND.Parse(strictNd,true)),"</pre>");
	 } catch (e)
	 {
	   document.write(" Caught exception: <pre>",e,"</pre>");
	 }
  </script>
</body>    
</html>