Badly formed JSON string and Prototype

229 views
Skip to first unread message

Light and Shadow

unread,
Aug 14, 2008, 3:56:44 PM8/14/08
to Prototype & script.aculo.us
I'm working on a class that needs to work independent of any
Javascript library. This script uses the JSON reference implementation
found here to serialize a Javascript object graph to a string.

http://json.org/json2.js

When Prototype is not included, the reference implementation works
fine.

When I include prototype.js, the reference implementation fails to
encode arrays correctly. When I check for the presence of Prototype
and use Object.toJSON(objectGraph) and .evalJSON() instead, I receive
"badly formed JSON string" errors. Currently, the conflicting site is
using Prototype 1.5.1.1.

I've created two test cases to illustrate this issue more clearly....

REFERENCE JSON ONLY
http://www.ahtenindustries.com/json/reference.html

PROTOTYPE & REFERENCE JSON
http://www.ahtenindustries.com/json/prototype.html

Does anyone know if there is a work around for this issue?

Thanks,

- Scott

kangax

unread,
Aug 15, 2008, 1:46:38 AM8/15/08
to Prototype & script.aculo.us
On Aug 14, 3:56 pm, Light and Shadow <lightandshado...@gmail.com>
wrote:
> I'm working on a class that needs to work independent of any
> Javascript library. This script uses the JSON reference implementation
> found here to serialize a Javascript object graph to a string.
>
> http://json.org/json2.js
>
> When Prototype is not included, the reference implementation works
> fine.
>
> When I include prototype.js, the reference implementation fails to
> encode arrays correctly. When I check for the presence of Prototype
> and use Object.toJSON(objectGraph) and .evalJSON() instead, I receive
> "badly formed JSON string" errors. Currently, the conflicting site is
> using Prototype 1.5.1.1.
>
> I've created two test cases to illustrate this issue more clearly....
>
> REFERENCE JSON ONLYhttp://www.ahtenindustries.com/json/reference.html
>
> PROTOTYPE & REFERENCE JSONhttp://www.ahtenindustries.com/json/prototype.html
>
> Does anyone know if there is a work around for this issue?

1.5.1 is a pretty old release.
Could you upgrade to 1.6.0.2?

If you still have problems, take a look at this patch
http://dev.rubyonrails.org/ticket/11524

--
kangax

Light and Shadow

unread,
Aug 16, 2008, 1:42:02 PM8/16/08
to Prototype & script.aculo.us
Kangax,

Unfortunately,I don't have control over which version of Prototype is
in use. My class will be used with many pre-existing sites which may
or may not be using Prototype (or any Javascript library). However,
I've upgraded to 1.6.x with the same results.

Without the reference JSON implementation, Prototype's JSON operations
work fine, but I can't count on it being present.

http://www.ahtenindustries.com/json/prototype-only.html

Nor can I use Prototype instead of the reference implementation as
Prototype conflicts with other sites using which are currently using
jQuery.

A temporary work around is to add a check for Object.toJSON to the if
statement around the reference JSON implementation and conditionally
use Prototype's methods when present.

if ((!this.JSON) && !(Object.toJSON)) {

/ / / / / / / / / / / / / / / / / / / / / / / / / / /

encodedObjectGraph = '';

if(Object.toJSON){
encodedObjectGraph = Object.toJSON(this.jarRoot);
} else {
encodedObjectGraph = JSON.stringify(this.jarRoot);
}


- Scott

kangax

unread,
Aug 17, 2008, 12:44:47 PM8/17/08
to Prototype & script.aculo.us
On Aug 16, 1:42 pm, Light and Shadow <lightandshado...@mac.com> wrote:
> Kangax,
>
> Unfortunately,I don't have control over which version of Prototype is
> in use. My class will be used with many pre-existing sites which may
> or may not be using Prototype (or any Javascript library). However,
> I've upgraded to 1.6.x with the same results.
>
> Without the reference JSON implementation, Prototype's JSON operations
> work fine, but I can't count on it being present.
>
> http://www.ahtenindustries.com/json/prototype-only.html

The json string in your example actualy is invalid:

"{quickLinkData: {links: [{url: http://www.ahtenindustries.com, name:
Ahten Industries : Home}]}}"

should be:

'{"quickLinkData": {"links": [{"url": "http://
www.ahtenindustries.com", "name": "Ahten Industries"}]}}'

>
> Nor can I use Prototype instead of the reference implementation as
> Prototype conflicts with other sites using which are currently using
> jQuery.
>
> A temporary work around is to add a check for Object.toJSON to the if
> statement around the reference JSON implementation and conditionally
> use Prototype's methods when present.
>
> if ((!this.JSON) && !(Object.toJSON)) {
>
> / / / / / / / / / / / / / / / / / / / / / / / / / / /
>
> encodedObjectGraph = '';
>
> if(Object.toJSON){
> encodedObjectGraph = Object.toJSON(this.jarRoot);} else {
>
> encodedObjectGraph = JSON.stringify(this.jarRoot);
>
> }

That would work, though it might be safer to check if `Object.toJSON`
actually works as expected:

var toJSON,
testee = {a: true},
exemplar = '{"a": true}';

if (Object.toJSON(testee) === exemplar) {
toJSON = Object.toJSON;
}
else if(JSON.stringify(testee) === exemplar) {
toJSON = JSON.stringify;
}
else {
toJSON = function(s) {
return eval('(' + s + ')');
}
}

--
kangax

kangax

unread,
Aug 17, 2008, 12:48:57 PM8/17/08
to Prototype & script.aculo.us
On Aug 17, 12:44 pm, kangax <kan...@gmail.com> wrote:

[ snip ]

> else {
>   toJSON = function(s) {
>     return eval('(' + s + ')');
>   }

Oops. Instead of `eval`, this should be your own `toJSON`
implementation (in case none of the libraries are available).

--
kangax
Reply all
Reply to author
Forward
0 new messages