Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #31547 > unrolled thread

String concat with JSON

Started byAndrew Poulos <ap_prog@hotmail.com>
First post2016-10-10 10:44 +1100
Last post2016-10-10 12:35 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  String concat with JSON Andrew Poulos <ap_prog@hotmail.com> - 2016-10-10 10:44 +1100
    Re: String concat with JSON Andrew Poulos <ap_prog@hotmail.com> - 2016-10-10 10:47 +1100
    Re: String concat with JSON Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-10-10 12:35 +0100

#31547 — String concat with JSON

FromAndrew Poulos <ap_prog@hotmail.com>
Date2016-10-10 10:44 +1100
SubjectString concat with JSON
Message-ID<4eKdnfDJCYfNS2fKnZ2dnUU7-YXNnZ2d@westnet.com.au>
I'm trying to communicate between the elearning piece I'm building and 
the learning management system (LMS).

The elearning creates a separate JSON string for each section but the 
LMS stores them all in the same place in whatever order it decides is 
appropriate. To be able to identify them correctly I'm appending 
"Page_1=", "Page_2=", "Page_3="... to each JSON string. (I'm going to 
read back the entire string and then split it on "Page_x=" and then 
JSON.parse it.)

With a simple test

var tmp = JSON.stringify(currentPageData);
// currentPageData is a one dim array with strings for elements
LMSSetValue("cmi.comments", "Page_1=" + tmp);

The LMS complains thus

Exception:
SyntaxError: Unexpected identifier at LMSSetValueMethod

The relevant bits of the LMS code (over which I have no control) are

setString = "this." + element + ".cmivalue =\"" + value + "\";";
var result = eval(setString);

It's trying to set value as "Page_1=["Test Data"]"

Why does adding a string to a JSON string give me an invalid string?

How do I get a valid string?

Andrew Poulos

[toc] | [next] | [standalone]


#31548

FromAndrew Poulos <ap_prog@hotmail.com>
Date2016-10-10 10:47 +1100
Message-ID<4eKdnfPJCYexSmfKnZ2dnUU7-YWdnZ2d@westnet.com.au>
In reply to#31547
On 10/10/2016 10:44 AM, Andrew Poulos wrote:
> I'm trying to communicate between the elearning piece I'm building and
> the learning management system (LMS).
>
> The elearning creates a separate JSON string for each section but the
> LMS stores them all in the same place in whatever order it decides is
> appropriate. To be able to identify them correctly I'm appending
> "Page_1=", "Page_2=", "Page_3="... to each JSON string. (I'm going to
> read back the entire string and then split it on "Page_x=" and then
> JSON.parse it.)
>
> With a simple test
>
> var tmp = JSON.stringify(currentPageData);
> // currentPageData is a one dim array with strings for elements
> LMSSetValue("cmi.comments", "Page_1=" + tmp);
>
> The LMS complains thus
>
> Exception:
> SyntaxError: Unexpected identifier at LMSSetValueMethod
>
> The relevant bits of the LMS code (over which I have no control) are
>
> setString = "this." + element + ".cmivalue =\"" + value + "\";";
> var result = eval(setString);
>
> It's trying to set value as "Page_1=["Test Data"]"
>
> Why does adding a string to a JSON string give me an invalid string?
>
> How do I get a valid string?

I just ran another test without adding the string

var tmp = JSON.stringify(currentPageData);
LMSSetValue("cmi.comments", tmp);

and it gives the same error with the value as "["Test Data"]"

Andrew Poulos

[toc] | [prev] | [next] | [standalone]


#31549

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-10-10 12:35 +0100
Message-ID<1786064.oMNUckLgyt@PointedEars.de>
In reply to#31547
Andrew Poulos wrote:

> The relevant bits of the LMS code (over which I have no control) are
> 
> setString = "this." + element + ".cmivalue =\"" + value + "\";";

Has “setString” been declared a local variable?  If not, this is not only 
bad style (undeclared variable, will break in strict mode; identifier for a 
variable looking like that for a setter), but another security leak: A 
script running in the background could potentially overwrite the value of 
“setString” before it is used in the subsequent eval() call.

> var result = eval(setString);

One proper way to do this, provided that the value of “element” does not 
contain property accessor syntax (otherwise the proper solution is slightly 
more complex), is:

  try
  {
    this[element].cmivalue = value;
  }
  catch (e)
  {
    if (!(element in this))
    {
      /* Handle bad property access */
    }
  }

That the LMS uses eval() like this is not only very inefficient, it is a 
potential security leak as it allows for code injection.  You should edit 
the LMS source code if you can, and report this as a bug.
 
> It's trying to set value as "Page_1=["Test Data"]"

Probably you mean “Page_1=["Test Data"]” or "Page_1=[\"Test Data\"]", as the 
above is not a valid string literal.
  
> Why does adding a string to a JSON string give me an invalid string?

This has nothing to do with JSON.  With a value of "foo" for “element”, and 
that value for “value”, the above is equivalent to

 this.foo.cmivalue = "Page_1=["Test Data"]";
-------------------------------^
Uncaught SyntaxError: Unexpected identifier

<http://PointedEars.de/scripts/faq/cljs/#debugging>

> How do I get a valid string?

At minimum:

  value = value.replace(/"/g, "\\$&");

But AISB, using eval() is inefficient and unsafe.  This replacement is 
insufficient to address that issue.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web