<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 1/4/2016 5:06 AM, John Carlson
      wrote:<br>
    </div>
    <blockquote
      cite="mid:13307E12-E5E6-4F9A-A883-7412F4CE21C4@gmail.com"
      type="cite">
      <pre wrap="">If all else fails, we can think about adding an index to the comment that shows where it is.  Or #precomment and #postcomment.</pre>
    </blockquote>
    <br>
    I have not been following this thread as closely as I probably
    should. <br>
    <br>
    I think what is being discussed is the location of comments in the
    JSON encoding compared their original location in the XML encoding.
    If that is not the case, then ignore the rest of this message and
    let me know.<br>
    <br>
    <br>
    In all translators/parsers dealing with hash (aka objects in
    JavaScript), I have always seen the disclaimer that the order of the
    original data cannot be guaranteed to be preserved in the data
    structure after translating/parsing. For example (using PHP and Perl
    because they are not XML or JavaScript):<br>
    <br>
    PHP (PHP calls all collections of elements arrays, whether they are
    indedxed or not)<br>
    $hash = array('z' => 1, 'x' => 'c');<br>
    foreach ($hash as $key) {<br>
        print "hash[$key] = $hash[$key]\n";<br>
    }<br>
    <br>
    hash[x] = c<br>
    hash[z] = 1<br>
    <br>
    <br>
    on conversion to Perl and using the code before for display<br>
    <br>
    Perl<br>
    # Create data the same way as PHP<br>
    my $hash = {'z' => 1, 'x' => 'c'};<br>
    foreach (my $key keys(%hash)) {<br>
        print "hash{$key} = $hash{$key}\n";<br>
    }<br>
    <br>
    hash{z} = 1<br>
    hash{x} = c<br>
    <br>
    <br>
    If the order of items is important, than an indexed array MUST be
    used.<br>
    <br>
    This does go back to a point I raised earlier. By creating data
    elements in JSON for the XML comments, you are changing the data
    model stored in the file. Things that were not data nor parsed (XML
    comments) are now appearing as data. It also means that
    full-escaping and character translating to the allowed JSON
    character set must be done to ensure that the result can be parsed
    in JavaScript. You are trying to create something that does not
    exist in the language (comments). I feel that this is not the right
    way to go.<br>
    <br>
    <br>
    Leonard Daly<br>
    <br>
    <br>
    <br>
    <blockquote
      cite="mid:13307E12-E5E6-4F9A-A883-7412F4CE21C4@gmail.com"
      type="cite">
      <pre wrap="">

John


</pre>
      <blockquote type="cite">
        <pre wrap="">On Jan 4, 2016, at 12:26 AM, Don Brutzman <a class="moz-txt-link-rfc2396E" href="mailto:brutzman@nps.edu"><brutzman@nps.edu></a> wrote:

OK this sounds great - thanks John.  We will look at your attachments further during the morning call.  Will later proceed with trying to achieve the refactoring since you are ready to track along - that tandem progress is really valuable to make sure we are getting things right (and not digging a gigantic hole).  BCNU.

On 1/3/2016 4:28 PM, John Carlson wrote:
</pre>
        <blockquote type="cite">
          <pre wrap="">First example, Viewpoints are not the same.  Otherwise roundtrip is fine.  Second example doesn’t parse—it’s a fragment.  HelloWorld JSON converted to HelloWorldCommented.x3d2 and attached for your review.  No changed to X3D JSON Loader code yet—It appears like some comments are moved upward.  I believe this is due to the XML -> JSON conversion, not the JSON -> XML conversion.  Node.js script to convert JSON to X3D also attached.  I don’t see a problem with the new format as long as we can work out the XML -> JSON conversion.  I can’t magically figure out how to reattach comments in the old place when it’s not in the JSON document. I can make any necessary changes to the loader as long as I don’t run out of spare time. John


</pre>
          <blockquote type="cite">
            <pre wrap="">On Dec 19, 2015, at 4:09 PM, Don Brutzman <a class="moz-txt-link-rfc2396E" href="mailto:brutzman@nps.edu"><brutzman@nps.edu></a> wrote:

1.  Am thinking about how we might improve the X3D JSON encoding by treating #comment and #ROUTE as objects in an array that is contained by parent.  This would reduce problems with the partial resorting that currently occurs.

Doing it as part of "-children" isn't particularly difficult; the trick will be doing it cleanly inside of nodes that contain other non-children nodes, e.g. Shape, Appearance, singleton nodes, etc.  The path to success may lead through always treating any  child content as part of an array.

Continuing this line of inquiry: we could put them inside of a "-children" JSON field consistently throughout the scene graph. Use cases:
a. Within "-children":[array] of Scene (which is already produced)
b. Within "-children":[array] of grouping nodes (which is already produced)
c. Within "-children":[array] of any node, even when they don't have a /children/ field

2.  So let's look at an example, in detail.

Example scene created is named HelloWorldCommented.x3d with XML source, current (original) JSON encoding, and alternative JSON encoding all attached.

First excerpt follows, with the focus just for regular children (use cases 1.a and 1.b from above).

=========================================
*XML encoding*
=========================================
<X3D>
<Scene>
  <!-- Example scene to illustrate comments interspersed among X3D nodes and fields (XML elements and attributes) -->
  <!-- WorldInfo begin -->
  <WorldInfo title='Hello world!'/>
  <!-- WorldInfo complete, Group begin -->
  <Group>
    <!-- Viewpoint begin -->
    <Viewpoint DEF='ViewUpClose' centerOfRotation='0 -1 0' description='Hello world!' position='0 -1 7'/>
    <!-- Viewpoint complete, Transform begin -->
    <Transform/>
    <!-- Transform complete, Transform begin -->
    <Transform/>
    <!-- Transform complete -->
  </Group>
  <!-- Group complete -->
</Scene>
</X3D>
=========================================

Now two corresponding JSON examples using #comment attached.  (Net effect with #ROUTE would be the same.)

Notice how comments are pulled up together in the current encoding (which partially re-sorts and thus changes the original order), but then comments are interspersed as expected in the second encoding:
=========================================
*current JSON encoding*
=========================================
{ "X3D": {
  "Scene": {
      "#comment":[
        "Example scene to illustrate comments interspersed among X3D nodes and fields (XML elements and attributes)",
        "WorldInfo begin",
        "WorldInfo complete, Group begin",
        "Group complete"
      ],
      "-children":[
        { "WorldInfo":
          {
            "@title":"Hello world!"
          }
        },
        { "Group":
          {
            "#comment":[
              "Viewpoint begin",
              "Viewpoint complete, Transform begin",
              "Transform complete, Transform begin",
              "Transform complete"
            ],
            "-children":[
              { "Viewpoint": { }
              },
              { "Transform": { }
              },
              { "Transform": { }
              }
            ]
          }
        }
      ]
  }
}
}
=========================================
*alternative JSON encoding*
=========================================
{ "X3D": {
  "Scene": {
      "-children":[
        { "#comment":"Example scene to illustrate comments interspersed among X3D nodes and fields (XML elements and attributes)"},
        { "#comment":"WorldInfo begin"},
        { "WorldInfo":
          {
            "@title":"Hello world!"
          }
        },
        { "#comment":"WorldInfo complete, Group begin"},
        { "Group":
          {
            "-children":[
              { "#comment":"Viewpoint begin"},
              { "Viewpoint": { }
              },
              { "#comment":"Viewpoint complete, Transform begin"},
              { "Transform": { }
              },
              { "#comment":"Transform complete, Transform begin"},
              { "Transform": { }
              },
              { "#comment":"Transform complete"}
            ]
          }
        },
        { "#comment":"Group complete"}
      ]
  }
}
}
=========================================

2.  So far so good.  Now let's look at how this approach ripples through a Shape node, as an example of use case 1.c above.

=========================================
*XML encoding*
=========================================
<!-- Shape begin -->
<Shape>
<!-- Text begin -->
<Text DEF='TextMessage' string='"Hello" "world!"'>
  <!-- FontStyle begin -->
  <FontStyle justify='"MIDDLE" "MIDDLE"'/>
  <!-- FontStyle complete -->
</Text>
<!-- Text complete, Appearance begin -->
<Appearance>
  <!-- Material begin -->
  <Material USE='MaterialLightBlue'/>
  <!-- Material complete -->
</Appearance>
<!-- Material complete -->
</Shape>
<!-- Shape complete -->
=========================================

Once again the current/original X3D JSON encoding partially re-sorts the comments:
=========================================
*current JSON encoding*
=========================================
"#comment":[
"Shape begin",
"Shape complete"
],
"-children":[
{ "Shape":
  {
    "#comment":[
      "Text begin",
      "Text complete, Appearance begin",
      "Material complete"
    ],
    "-geometry":[
      { "Text":
        {
          "@DEF":"TextMessage",
          "@string":["Hello","world!"],
          "#comment":[
            "FontStyle begin",
            "FontStyle complete"
          ],
          "-fontStyle":[
            { "FontStyle":
              {
                "@justify":["MIDDLE","MIDDLE"]
              }
            }
          ]
        }
      }
    ],
    "-appearance":[
      { "Appearance":
        {
          "#comment":[
            "Material begin",
            "Material complete"
          ],
          "-material":[
            { "Material":
              {
                "@USE":"MaterialLightBlue"
              }
            }
          ]
        }
      }
    ]
  }
}
]
=========================================

And once again the alternative intersperses the comments as part of a "-children" JSON key.   Not so very different.

Notable is that Shape and Appearance do not have /children/ fields, but they do have child ("-children") comments.  Thus scene graph structure is better (though not perfectly) preserved.

Some re-sorting of comments prior to peer nodes still appears to be unavoidable, but it is reduced overall.

The pattern for encoding comments (and ROUTEs) is fully consistent, so parsing should still be straightforward.
=========================================
*alternative JSON encoding*
=========================================
"-children":[
{ "#comment":"Shape begin"},
{ "Shape":
  {
    "#comment":[
      "Sphere begin",
      "Sphere complete, Appearance begin",
      "Appearance complete"
    ],
    "-geometry":[
      { "Sphere":
        {
        }
      }
    ],
    "-appearance":[
      { "Appearance":
        {
          "-children":[
           { "#comment":"Viewpoint begin"},
           { "#comment":"Material begin"},
           { "#comment":"Material complete, ImageTexture begin"},
           { "#comment":"ImageTexture complete"}
          ],
          "-material":[
            { "Material":
              {
                "@DEF":"MaterialLightBlue",
                "@diffuseColor":[0.1,0.5,1]
              }
            }
          ],
          "-texture":[
            { "ImageTexture":
              {
                "@DEF":"ImageCloudlessEarth",
                "@url":["earth-topo.png","earth-topo.jpg","earth-topo-small.gif",<a class="moz-txt-link-rfc2396E" href="http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png">"http://www.web3d.org/x3d/content/examples/Basic/earth-topo.png"</a>,<a class="moz-txt-link-rfc2396E" href="http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg">"http://www.web3d.org/x3d/content/examples/Basic/earth-topo.jpg"</a>,<a class="moz-txt-link-rfc2396E" href="http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif">"http://www.web3d.org/x3d/content/examples/Basic/earth-topo-small.gif"</a>]
              }
            }
          ]
        }
      }
    ]
  }
},
{ "#comment":"Shape complete"}
]
=========================================

3.  Analysis and next steps.

I think that this alternative approach further reduces the special-case handling of #comment and #ROUTE which exists in the current/original JSON encoding.

A more consistent JSON encoding simplifies parsing rules and hopefully makes production/consumption easier to accomplish.

Treating #comment and #ROUTE in the same fashion as other nodes, rather than as field keys, also is more consistent with the primary design pattern of the overall X3D JSON encoding.

The alternative-encoding example was manually edited and validated using jslint in X3D-Edit.  If group review indicates that this approach looks superior, I'll work on a refactoring of the X3dToJson.xslt stylesheet and re-build all the examples to support  continued in-depth testing, implementation and evaluation.

Thanks in advance for all scrutiny and review.

all the best, Don
--
Don Brutzman  Naval Postgraduate School, Code USW/Br       <a class="moz-txt-link-abbreviated" href="mailto:brutzman@nps.edu">brutzman@nps.edu</a>
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy roboticshttp://faculty.nps.edu/brutzman
<HelloWorldCommented.x3d><HelloWorldCommentedOriginalEncoding.json><HelloWorldCommentedAlternativeEncoding.json>
</pre>
          </blockquote>
          <pre wrap="">
</pre>
        </blockquote>
        <pre wrap="">

all the best, Don
-- 
Don Brutzman  Naval Postgraduate School, Code USW/Br       <a class="moz-txt-link-abbreviated" href="mailto:brutzman@nps.edu">brutzman@nps.edu</a>
Watkins 270,  MOVES Institute, Monterey CA 93943-5000 USA   +1.831.656.2149
X3D graphics, virtual worlds, navy robotics <a class="moz-txt-link-freetext" href="http://faculty.nps.edu/brutzman">http://faculty.nps.edu/brutzman</a>
</pre>
      </blockquote>
      <pre wrap="">

_______________________________________________
x3d-public mailing list
<a class="moz-txt-link-abbreviated" href="mailto:x3d-public@web3d.org">x3d-public@web3d.org</a>
<a class="moz-txt-link-freetext" href="http://web3d.org/mailman/listinfo/x3d-public_web3d.org">http://web3d.org/mailman/listinfo/x3d-public_web3d.org</a>
</pre>
    </blockquote>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <font class="tahoma,arial,helvetica san serif" color="#333366">
        <font size="+1"><b>Leonard Daly</b></font><br>
        3D Systems & Cloud Consultant<br>
        X3D Co-Chair on Sabbatical<br>
        LA ACM SIGGRAPH Chair<br>
        President, Daly Realism - <i>Creating the Future</i>
      </font></div>
  </body>
</html>