SerializeJSON without outputing backslash

923 views
Skip to first unread message

Peter Pham

unread,
Dec 15, 2013, 5:34:04 PM12/15/13
to ra...@googlegroups.com
Is it possible to output serialize JSON without write backslash for escaping forward slash? 
I tried using function serialise() also but this, instead, quoted values with single quotes instead of double quotes. 

Example code:

<cfscript>

st = {"link" = "/example/example1"};
json1 = serializeJSON(st);
json2 = serialize(st);

dump(json1); //  {"link":"\/example\/example1"}
dump(json2); //  {'link':'/example/example1'}

</cfscript>

From the above example code, my desired output is:
{"link":"/example/example1"}

Is it possible to achieve this?

Peter



Peter Boughton

unread,
Dec 15, 2013, 7:30:10 PM12/15/13
to ra...@googlegroups.com
> Is it possible to output serialize JSON without write backslash for escaping forward slash?

No, because then that's not JSON.

JSON is a specific format with specific rules (as described at http://json.org/)

You can of course use string manipulation functions on the output of the serializeJson function to alter results, though of course you'll then be creating something that is not JSON (and thus may not be deserializable).

What is it you are *actually* trying to do?

Peter Pham

unread,
Dec 15, 2013, 9:59:25 PM12/15/13
to ra...@googlegroups.com
I try to replicate PHP option JSON_UNESCAPED_SLASHES as in
json_encode($var, JSON_UNESCAPED_SLASHES)

Michael Offner

unread,
Dec 16, 2013, 3:13:08 AM12/16/13
to ra...@googlegroups.com
no, there is no option like "JSON_UNESCAPED_SLASHES" in Railo, just to understand why a argument like this could be usable, for what you need that behavior?

Micha


2013/12/15 Peter Pham <vdung...@gmail.com>

--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/railo/55762317-c662-492d-86da-5b0db5c3a522%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
/micha

Michael Offner CTO Railo Technologies GmbH

Adam Cameron

unread,
Dec 16, 2013, 3:41:06 AM12/16/13
to ra...@googlegroups.com

On Monday, 16 December 2013 00:30:10 UTC, Peter Boughton wrote:
> Is it possible to output serialize JSON without write backslash for escaping forward slash?

No, because then that's not JSON.

JSON is a specific format with specific rules (as described at http://json.org/)

I think you're misreading the spec, Peter. The slash doesn't NEED to be escaped in JSON. The rule - as per the spec you link to, and borne out in the real world (including, I hasten to add: Railo) - is "any unicode character except double quote or backslash" is a char. A fwd slash is not a special char needing to be escaped. However *if* the backslash is present, then one of the acceptable following characters is a fwd slash. But this is not to say that the fwd slash NEEDS to be escaped. It just *can be* escaped. 

I would think any baseline / automated implementation (just as serializeJson() implemented) should NOT do unnecessary escaping? I can only presume that Railo is following CF's lead here. However I think that's a facile approach in this case: Railo should follow the rules of JSON, not CF's interpretation of them.

As an aside... ColdFusion fails on this code, Railo does not:
<cfset json = '{"link" = "/example/example1"}'>
<cfdump var="#deserializeJson(json)#">

Note that I am incorrectly (and entirely by accident: copy and paste from the original code) using "=" instead of ":" as the "assignment operator" in that string. That's not valid. Railo should error on that input. CF gets it right (so does OBD, FWIW). If I replace the "=" with a ":" to make it valid JSON, all three engines parse it fine. Note that the fwd slashes are not unnecessarily escaped.

-- 
Adam

Peter Boughton

unread,
Dec 16, 2013, 8:46:07 AM12/16/13
to ra...@googlegroups.com
Adam wrote:
> I think you're misreading the spec, Peter. The slash doesn't NEED to be escaped in JSON.

Doh! I actually checked two things last night - both that page and a custom function which I thought existed specifically to not escape the backslash - misread the first and found the second to include backslash, and stupidly reached the wrong conclusion. :(


> I would think any baseline / automated implementation (just as
> serializeJson() implemented) should NOT do unnecessary escaping?

Yep - definitely agree.

There should of course be an option to be CF-compatible, but following the JSON definition would ideally be the default.

Michael Offner

unread,
Dec 19, 2013, 2:51:56 AM12/19/13
to ra...@googlegroups.com


2013/12/16 Adam Cameron <camero...@gmail.com>

--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages