Encode string as json string

tcak via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 15 06:09:04 PST 2016


Other than generating normal JSON content (stringify), JSON 
string is used for Javascript string expressions as well.

To create a properly encoded Javascript string, the shortest way 
is

JSONValue("this'\\is//the\"text").toString();

Yes, it works, but it is uncomfortable to write the code as 
above. Two issues:

1. It creates a struct, and there are extra processing in the 
background (check the source code for this.)

2. It is really long.

std.json already has a string encoding function in it, but it is 
not exposed outside.

https://github.com/D-Programming-Language/phobos/blob/master/std/json.d#L1002

It would be quite useful to expose this function for ease of use. 
Any thoughts?


More information about the Digitalmars-d mailing list