[x3d-public] second round of X3D JSON conversion support using X3dToJson.xslt

Don Brutzman brutzman at nps.edu
Mon Mar 9 08:28:49 PDT 2015


On 3/9/2015 2:46 AM, Roy Walmsley wrote:
> Don,
>
> Great that you are making progress.

Thanks Roy.  It is definitely challenging but appears to be do-able.

Douglas Crockford's "JavaScript: The Good Parts" is a fundamentally important reference.

> I have two questions, if I may. Apologies in advance if it simply my lack of
> knowledge and understanding.

All help is much appreciated, questions are really important.  Thanks.

btw if you find anyone with full knowledge and understanding, please invite them over immediately!  8)

> 1)  Why does a container field need to have a different prefix to the other
> attributes (i.e. "-" as opposed to "@")?

The @ symbol is commonly used in XML XPath, XSLT etc. to indicate the name of an attribute.  Didn't want to overload that semantic usage further.

I applied the - symbol to prefix containerField labels for node arrays to see how it looks and to possibly simplify processing.  Without that distinguishing symbol, a parser trying to reconstruct an X3D scene graph from JSON might need deeper knowledge of how X3D works.  So it is experimental.

Organizing child arrays by using the field names of X3D nodes is an asset we have that allows each node to be handled as a uniquely keyed object - the "O" in JSON.  (This approach is not possible in other XML to JSON patterns, where generally XML child elements don't have such a unique key or label.)

I suspect that the - symbol prefix might be able to go away, or be further improved, when we get to using these a bit more.  Not clear yet.

Meanwhile (after briefly sleeping on it) I've added the rest of what is needed for converting the X3D header.  Non scene-graph nodes (X3D, head, component, meta, unit, Scene) are simple JSON objects that are now included.  Note that they have no containerField and in some cases no attributes at all, so the design pattern here is simpler and a little bit different than the rest of the scene graph.

Completely converted HelloWorld.json attached.  Looks like a pair of slightly different design patterns (first for header and then for scene graph) are required and holding together OK so far.  Still passes jslint in its strictest mode.  http://www.jslint.com

Incidentally the need for a slight hybrid approach like this design may be part of why a single approach has been a bit elusive so far.  Still haven't compared it to interesting design alternatives previously posted by Cecile, Yvonne, Kristian, John and others.  Continued exploration should help a "best of breed" emerge, the problem space continues to shrink.

Conversion stylesheet updates committed to
https://sourceforge.net/p/x3d/code/HEAD/tree/www.web3d.org/x3d/stylesheets/X3dToJson.xslt

Extra: added some design comments on whether a special JSON header is needed (probably not) or DOCTYPE information is needed (not).
=======================================================================
7. Apparently not possible or not needed.

- Embedded JSON comments are specifically disallowed by JSON specification
   and so round-trippable inclusion must be a custom feature of this encoding.
   http://www.quora.com/How-do-I-write-comments-inside-a-JSON-document
- No JSON-unique header. Can optionally use X3D/head/meta name=value pairs
   (if capturing full document) or comments in a scene-graph fragment
   or even an X3D Metadata node.  However for anything intended to be
   interoperable/reusable, consistency and repeatability is needed.
   Leaving the output JSON as simple as possible avoids code problems and
   provides programmers with the greatest possible flexibility.  Further
   analysis will show if a JSON header is needed, probably a non-problem.
- DOCTYPE information is dropped.  This is commonplace for XSLT and does
   not appear to be needed in the JSON encoding.  If actually needed by someone,
   such information can be reconstructed simply by using the X3D version number.
   http://www.web3d.org/x3d/content/examples/X3dSceneAuthoringHints.html#Validation
=======================================================================

> 2) How is it with routes and prototypes, including 'IS' statements?
>
> Roy

Great question, that is the next level. Script, field, CDATA source blocks, ProtoDeclare, IS/connect, ExternProtoDeclare and fieldValue might require some special handling in the stylesheet, though I suspect that these patterns will be able to handle them just fine.  I'll need another session (and more strong coffee!) to confirm that satisfactorily.

Given that this hybrid approach is now different from other XML-to-JSON converters, we'll probably also need to write a Javascript program to parse and then write the JSON back out as XML .x3d in order to confirm the lossless round-trip requirement.  JsonToX3d.js I suppose.

If nothing else, all of this design variety illustrates the value of finding an acceptable JSON encoding that can be reused consistently.

all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       brutzman at nps.edu
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics http://faculty.nps.edu/brutzman
-------------- next part --------------
[
  { "X3D":
    {
      "@profile":"Immersive",
      "@version":3.3,
      "@xsd:noNamespaceSchemaLocation":"http://www.web3d.org/specifications/x3d-3.3.xsd",
      "head":[
        { "meta":
          {
            "@content":"HelloWorld.x3d",
            "@name":"title"
          }
        },
        { "meta":
          {
            "@content":"Simple X3D scene example: Hello World!",
            "@name":"description"
          }
        },
        { "meta":
          {
            "@content":"30 October 2000",
            "@name":"created"
          }
        },
        { "meta":
          {
            "@content":"8 March 2015",
            "@name":"modified"
          }
        },
        { "meta":
          {
            "@content":"Don Brutzman",
            "@name":"creator"
          }
        },
        { "meta":
          {
            "@content":"HelloWorld.tall.png",
            "@name":"Image"
          }
        },
        { "meta":
          {
            "@content":"http://en.wikipedia.org/wiki/Hello_world",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://en.wikipedia.org/wiki/Hello#\"Hello,_World\"_computer_program",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://en.wikibooks.org/w/index.php?title=Computer_Programming/Hello_world",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://www.HelloWorldExample.net",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://www.web3D.org",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://www.web3d.org/realtime-3d/news/internationalization-x3d",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://www.web3d.org/x3d/content/examples/HelloWorld.x3d",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://X3dGraphics.com/examples/X3dForAdvancedModeling/HelloWorldScenes",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"http://X3dGraphics.com/examples/X3dForWebAuthors/Chapter01-TechnicalOverview/HelloWorld.x3d",
            "@name":"identifier"
          }
        },
        { "meta":
          {
            "@content":"http://www.web3d.org/x3d/content/examples/license.html",
            "@name":"license"
          }
        },
        { "meta":
          {
            "@content":"X3D-Edit 3.3, https://savage.nps.edu/X3D-Edit",
            "@name":"generator"
          }
        },
        { "#comment":"Alternate encodings: VRML97, X3D ClassicVRML Encoding, X3D Compressed Binary Encoding (CBE), X3DOM, JSON" },
        { "meta":
          {
            "@content":"HelloWorld.wrl",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"HelloWorld.x3dv",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"HelloWorld.x3db",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"HelloWorld.xhtml",
            "@name":"reference"
          }
        },
        { "meta":
          {
            "@content":"HelloWorld.json",
            "@name":"reference"
          }
        }
      ],
      "Scene":[
        { "#comment":"Example scene to illustrate X3D nodes and fields (XML elements and attributes)" },
        { "Group":
          {
            "@bboxCenter":[0, 0, 0 ],
            "@bboxSize":[-1, -1, -1 ],
            "-children":[
              { "Viewpoint":
                {
                  "@DEF":"ViewUpClose",
                  "@centerOfRotation":[0, -1, 0 ],
                  "@description":"Hello world!",
                  "@position":[0, -1, 7 ],
                  "@fieldOfView":0.7854,
                  "@jump":true,
                  "@orientation":[0, 0, 1, 0 ],
                  "@retainUserOffsets":false
                }
              },
              { "Transform":
                {
                  "@rotation":[0, 1, 0, 3 ],
                  "@center":[0, 0, 0 ],
                  "@scale":[1, 1, 1 ],
                  "@scaleOrientation":[0, 0, 1, 0 ],
                  "@translation":[0, 0, 0 ],
                  "@bboxCenter":[0, 0, 0 ],
                  "@bboxSize":[-1, -1, -1 ],
                  "-children":[
                    { "Shape":
                      {
                        "@bboxCenter":[0, 0, 0 ],
                        "@bboxSize":[-1, -1, -1 ],
                        "-geometry":[
                          { "Sphere":
                            {
                              "@radius":1,
                              "@solid":true
                            }
                          }
                        ],
                        "-appearance":[
                          { "Appearance":
                            {
                              "-material":[
                                { "Material":
                                  {
                                    "@DEF":"MaterialLightBlue",
                                    "@diffuseColor":[0.1, 0.5, 1 ],
                                    "@ambientIntensity":0.2,
                                    "@emissiveColor":[0, 0, 0 ],
                                    "@shininess":0.2,
                                    "@specularColor":[0, 0, 0 ],
                                    "@transparency":0
                                  }
                                }
                              ],
                              "-texture":[
                                { "ImageTexture":
                                  {
                                    "@DEF":"ImageCloudlessEarth",
                                    "@url":["earth-topo.png", "earth-topo.jpg", "earth-topo-small.gif", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg", "http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif" ],
                                    "@repeatS":true,
                                    "@repeatT":true
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              { "Transform":
                {
                  "@translation":[0, -2, 0 ],
                  "@center":[0, 0, 0 ],
                  "@rotation":[0, 0, 1, 0 ],
                  "@scale":[1, 1, 1 ],
                  "@scaleOrientation":[0, 0, 1, 0 ],
                  "@bboxCenter":[0, 0, 0 ],
                  "@bboxSize":[-1, -1, -1 ],
                  "-children":[
                    { "Shape":
                      {
                        "@bboxCenter":[0, 0, 0 ],
                        "@bboxSize":[-1, -1, -1 ],
                        "-geometry":[
                          { "Text":
                            {
                              "@DEF":"TextMessage",
                              "@string":["Hello", "world!" ],
                              "@maxExtent":0.0,
                              "@solid":false,
                              "-fontStyle":[
                                { "FontStyle":
                                  {
                                    "@justify":["MIDDLE", "MIDDLE" ],
                                    "@family":["SERIF" ],
                                    "@horizontal":true,
                                    "@leftToRight":true,
                                    "@size":1.0,
                                    "@spacing":1.0,
                                    "@style":"PLAIN",
                                    "@topToBottom":true
                                  }
                                }
                              ]
                            }
                          }
                        ],
                        "-appearance":[
                          { "Appearance":
                            {
                              "-material":[
                                { "Material":
                                  {
                                    "@USE":"MaterialLightBlue",
                                    "@ambientIntensity":0.2,
                                    "@diffuseColor":[0.8, 0.8, 0.8 ],
                                    "@emissiveColor":[0, 0, 0 ],
                                    "@shininess":0.2,
                                    "@specularColor":[0, 0, 0 ],
                                    "@transparency":0
                                  }
                                }
                              ]
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
]


More information about the x3d-public mailing list