[x3d-public] MFString quoting [was: interesting sample] JSONescaping example

yottzumm at gmail.com yottzumm at gmail.com
Sat May 6 03:32:03 PDT 2017


I don’t know if using XML entities in Java is kosher.  I think UTF-8 would be better.  What do you think?

John

Sent from Mail for Windows 10

From: Don Brutzman
Sent: Saturday, May 6, 2017 12:55 AM
To: Roy Walmsley; 'John Carlson'
Cc: x3d-public at web3d.org
Subject: Re: [x3d-public] MFString quoting [was: interesting sample] JSONescaping example



On 4/27/2017 3:22 AM, Roy Walmsley wrote:
> Hi,
> 
> The trick when considering translating from XML encoding to JSON encoding is to think of the what is needed in the middle, i.e. what is loaded into memory, or, equivalently, what would be displayed on the screen. So, considering the examples below one at a time:
> 
> 	X3D  "quote mark  \"  is ""
> 
> I presume that the term X3D implies that we looking at an XML  encoding of a string. So, what we want to see on the screen is
> 
> 	quote mark " is "
> 
> The escaping rules for JSON are simple. An easy to understand diagram is at  http://www.json.org/.

| A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

http://www.json.org/string.gif (open source, attached as JsonStringRailroadTrackDiagram.gif)

> So translating this memory representation to the JSON encoding gives:
> 
> 	"quote mark \" is ""
> 
> Notice that the ampersand shall not be escaped in the JSON encoding. In fact, only the quotation mark " and the reverse solidus \ have to be escaped.

agreed

> The escaping of the forward solidus / is optional.

It looks like the figure requires such escaping - unless you are looking at the path through the central box?

	+==============================+
	|Any UNICODE character except |
	| " or \ or control character  |
	+==============================+

Please confirm...

Next part is very helpful:
> Now let's look at the second example.
> 
> 	JSON "empty string \\\\\"\\\\\" skips a line:",
> 
> This is already a JSON encoding. So a parser will load this and display the following string
> 
> 	empty string \\"\\" skips a line:
> 
> Clearly that is not what is desired. What you want to see is:
> 
> 	empty string "" skips a line:
> 
> So, encoded into JSON this becomes
> 
> 	"empty string \"\" skips a line:"
> 
> Similarly, encoded into the XML encoding gives
> 
> 	"empty string \"\" skips a line:"
> 
> So yes, I would conclude that you are far too zealous in adding all those backslashes.
> 
> Remember, you are not trying to write an XML encoded string as a JSON encoding. You are going via computer memory, where loading the XML encoding undoes all the escaping. You then only have to do the new escaping on the unescaped string.
> 
> Hope this helps. Happy to talk if you want. Especially as I haven't involved stylesheets in the discussion above!
> 
> All the best,
> 
> Roy

Paraphrase:  the analysis correctly diagnoses that the X3dToJson.xslt stylesheet is escaping the already-escaped XML version.  Hence too many backslashes.  Instead it should produce escaped JSON that matches the intended display string.

Re-paraphrase: the X3D (.x3d XML) To Json stylesheet should produce JSON corresponding to the expected display text that gets shown.

Collecting and confirming excerpts above:

[show]	empty string "" skips a line:

[.x3d]	"empty string \"\" skips a line:"

[JSON]	"empty string \"\" skips a line:"

Gulp: .x3d and JSON MF/SFString values are identical.  Wow pretty ironic.  Apparently working too hard, much escapism for no reason??!!  :0

Hmm hmmm.. I think that appears correct for MF/SFString values.  Please review!

Meanwhile, though, XML .x3d comments can have unescaped quotation marks, so those still must be escaped in JSON.  So X3D and JSON comment escaping is not identical.

Modified scene and conversion attached, excerpts follow.

================================================================

[show]	☺ = smiley face emoticon, and "" means skip a line

[.x3d]	<!-- ☺ = smiley face emoticon, and "" means skip a line -->

[JSON]

{ "Shape":
   {
	"-children":[
	  {
		"#comment":"☺ = smiley face emoticon, and \"\" means skip a line"
	  }
	],

================================================================

[show]	Have a " quote character
[show]	with X3D!
[show]	
[show]	:)   ☺

[.x3d]	<Text solid='false' string='"Have a \" quote character" "with X3D!" "" ":)   ☺"'>

[JSON]	"-geometry":
	  { "Text":
		{
		  "@string":["Have a \" quote character","with X3D!","",":) ☺"],

================================================================

meanwhile, on the other other other hand, .java escapes both \ and " characters as \\ and \" within a quoted String, and the comment/MFString example above looks like

[Java]
.addChild(new ShapeObject()
   .addComments(" ☺ = smiley face emoticon, and \"\" means skip a line ")
   .setGeometry(new TextObject().setString(new MFStringObject("\"Have a \\\" quote character\" \"with X3D!\" \"\" \":)   ☺\""))

which compiles and (based on other examples) converts OK, apparently.  thank goodness.

================================================================

are we having fun yet?!!

am hoping we have finally cornered this one.  added another path to X3dToJson.xslt for MF/SFString, seems to work.  will run a full set of json conversions tonight and we will see what else pops up.

q. "what do you like most about computer science?"
a. "you only have to get something right once, and it stays that way."


> -----Original Message-----
> From: x3d-public [mailto:x3d-public-bounces at web3d.org] On Behalf Of Don Brutzman
> Sent: 27 April 2017 03:30
> To: John Carlson <yottzumm at gmail.com>
> Cc: x3d-public at web3d.org
> Subject: Re: [x3d-public] MFString quoting [was: interesting sample] JSON escaping example
> 
> On 4/26/2017 4:02 PM, yottzumm at gmail.com wrote:
>> I do have a problem with the JSON encoding for the attached file (translated by X3dToJson.xslt).  Let me know if you think otherwise.  Look at all the \\\\\.
> 
> i suspect the issue is illustrated by the following SFString within an MFString
> 
> 	X3D  "quote mark  \"  is &quot;"
> 	JSON "empty string \\\\\"\\\\\" skips a line:",
> 
> will try to break it down.
> 
> double-backslash escaping going on here for JSON; so internal content-only correspondences should likely be
> 
> 	[X3D ]	\
> 	[JSON]	\\
> 
> 	[X3D ]	"
> 	[JSON]	\"
> 
> 	[X3D ]	\"
> 	[JSON]	\\\"
> 
> 	[X3D ]	quote mark  \"  is &quot;
> 	[JSON]	quote mark \\\" is "
> 
> please count.  does that look right?
> 
> if so, then it looks like the X3dToJson.xslt is overzealously prepending 2 extra backslashes in this particular case (one of several).
> 
> if not, then please advise what the right answer is.
> 
> also please re-advise, where are the JSON backslash-related rules?
> 
> if we can agree on what the right answer looks like, then i will dive back into the independently XML/XSLT/XPath escaped (and obtw recursive) stylesheet template that consumes/produces this stuff to try doing the job correctly.  (gulp!)
> 
> p.s. if anyone out there isn't dizzy yet, congrats!
> 
> 
>> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
>>
>> *From: *Andreas Plesch <mailto:andreasplesch at gmail.com>
>> *Sent: *Wednesday, April 26, 2017 5:21 PM
>> *To: *John Carlson <mailto:yottzumm at gmail.com>
>> *Cc: *Don Brutzman <mailto:brutzman at nps.edu>; X3D Graphics public mailing list <mailto:x3d-public at web3d.org>
>> *Subject: *Re: [x3d-public] MFString quoting [was: interesting sample]
>>
>> On Wed, Apr 26, 2017 at 3:55 PM, <yottzumm at gmail.com <mailto:yottzumm at gmail.com>> wrote:
>>
>>      It’s
>>
>>      http://x3dgraphics.com/slidesets/X3dForWebAuthors/Chapter02GeometryPrimitives.pdf
>>
>>      Got caught in the grand renaming.  Also the parent page is wrong.
>>
>> Thanks.
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 --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20170506/175661b8/attachment-0001.html>


More information about the x3d-public mailing list