Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON Serializer produces invalid JSON if a value ends with quote #10

Closed
craigdietrich opened this issue Jun 10, 2011 · 3 comments
Closed

Comments

@craigdietrich
Copy link
Contributor

serializers/ARC2_RDFJSONSerializer.php->jsonEscape($v) begins by escaping the passed var using json_encode(). If the var is a string, the result will be a string wrapped by quotes. The function then removes these quotes using trim(),

if (function_exists('json_encode')) return trim(json_encode($v), '"');

However, if a string passed into the function ends with a quote, json_encode() will result in,

"This is a string "ending in a quote/""

trim() will then remove both quotes, resulting in,

This is a string "ending in a quote/

When this string is passed to the JSON output, the result is,

{
"example": "This is a string "ending in a quote/"
}

... where the errant slash at the end escapes the JSON ending quote, resulting in invalid JSON.

This is the update I've made locally, which explicitly removes a single quote from the beginning and end if they exist.

if (function_exists('json_encode')) { // Updated by Craig
$v = json_encode($v);
if ('"'==substr($v,0,1)&&'"'==substr($v,-1,1)) $v = substr($v, 1, -1);
return $v;
}

(Should probably use regex instead.)

Thanks!

@Knurg
Copy link

Knurg commented Jun 14, 2011

Thanks for this!

I fixed the issue in my repository. Please have a look at Knurg@5d9640f

Code:
if (function_exists('json_encode')) {
$v = json_encode($v);
if(preg_match('/^"(.*)"$/', $v, $matches))
$v = $matches[1];
return $v;
}

I hope this does what it should do.

@craigdietrich
Copy link
Contributor Author

Hi Mark,

I've tested your code on my end and all looks well.

Thanks -- appreciated!

@semsol
Copy link
Owner

semsol commented Dec 1, 2011

Thanks guys, fixed in the latest revision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants