Escaping strings

  • Marc Noirot

    Marc Noirot - 2009-10-22


    using mjson 1.1, I'm creating JSON trees from existing data, and then using `json_tree_to_string` to display the aforementionned tree.

    The problem I'm having is that strings don't seem to be properly escaped in the output.

    I would like to know whether I'm supposed to manually escape my strings before passing them to `json_new_string` and `json_insert_pair_into_object`, or if it is a bug in the library.


  • Anonymous - 2009-10-23

    Hi, noirotm

    Yes, if the programmer wants the strings to be escaped then he must escape them accordingly. That is due to the JSON format supporting strings composed of Unicode characters (which means that it isn't necessary to escape them), which means that the string escape procedure would be, in some cases, needlessly dragging down the whole parsing procedure and wasting memory.

    To avoid that source of inefficiency, the responsibility of escaping and unescaping the strings was left to those programmers who decide that they need to escape/unescape them.

    Nonetheless, could you provide an example where it is possible to replicate the behaviour you suspect to be a bug?

  • Marc Noirot

    Marc Noirot - 2009-11-02

    The main problem with that approach is mainly about the " (double quote) character, which must be escaped in all cases, else it will cause the reminder of the string to be printed as if not part of the string.

    I also had a problem with the line-feed character (0x0A) which causes the validator at to fail to validate my generated JSON data.

  • Anonymous - 2009-11-02

    The reason behind that choice is that if a user is hand-crafting a JSON document tree by hand then he will surely know what strings he must escape and what strings are safe to just pop in. If the library forced every programmer to escape each and every single string, independent of whether that operation was needed or not, then it would be forcing a relatively expensive operation on all cases, which isn't a nice thing to do.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks