Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Why is there a diference between TJsonObject.ToJSON & TJsonObject.ToString?



Permlink Replies: 2 - Last Post: Feb 13, 2018 12:37 PM Last Post By: Clement Doss Threads: [ Previous | Next ]
Clement Doss

Posts: 133
Registered: 9/19/00
Why is there a diference between TJsonObject.ToJSON & TJsonObject.ToString?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 13, 2018 11:17 AM
Hi,

I'm using Tokyo 2.2 and I noticed a diference between the string generated by ToJSON vs ToString.

Given this pair: PhotoPath = http://accweb/emmployees/davolio.bmp

JsonObject.ToString will output { "PhotoPath": "http://accweb/emmployees/davolio.bmp" }
JsonObject.ToJSON will output { "PhotoPath": "http:\/\/accweb\/emmployees\/davolio.bmp"}


Why is there a difference?

When dealing with small strings there's no problem, but when encoding (base64) all the "/" will be encoded as "\/" which will make the output considerably bigger.

Clément
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Why is there a diference between TJsonObject.ToJSON & TJsonObject.ToString? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 13, 2018 11:48 AM   in response to: Clement Doss in response to: Clement Doss
Clement Doss wrote:

Why is there a difference?

Because toString() and toJSON() are used for different purposes, so
they follow different encoding rules.

When dealing with small strings there's no problem, but when encoding
(base64) all the "/" will be encoded as "\/" which will make the
output considerably bigger.

Why is the slash an escapable character in JSON?
https://stackoverflow.com/questions/4264877/

JSON: why are forward slashes escaped?
https://stackoverflow.com/questions/1580647/

In a nutshell, a '/' character is not REQUIRED to be escaped as '\/' in
JSON in general, but it is ALLOWED to be (instead of using the longer
'\u002F' format) in situations where JSON is embedded in a context that
does not allow an unescaped '/' to appear (such as an unescaped '</'
string in JSON embedded inside an HTML <script> tag).

--
Remy Lebeau (TeamB)
Clement Doss

Posts: 133
Registered: 9/19/00
Re: Why is there a diference between TJsonObject.ToJSON & TJsonObject.ToString? [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 13, 2018 12:37 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy,


Why is there a difference?

Because toString() and toJSON() are used for different purposes, so
they follow different encoding rules.

When dealing with small strings there's no problem, but when encoding
(base64) all the "/" will be encoded as "\/" which will make the
output considerably bigger.

Why is the slash an escapable character in JSON?
https://stackoverflow.com/questions/4264877/

JSON: why are forward slashes escaped?
https://stackoverflow.com/questions/1580647/

In a nutshell, a '/' character is not REQUIRED to be escaped as '\/' in
JSON in general, but it is ALLOWED to be (instead of using the longer
'\u002F' format) in situations where JSON is embedded in a context that
does not allow an unescaped '/' to appear (such as an unescaped '</'
string in JSON embedded inside an HTML <script> tag).

Thanks for the links and explanation!

Clément
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02