GithubHelp home page GithubHelp logo

Comments (2)

j4cbo avatar j4cbo commented on May 21, 2024

Is there a particular bug you're seeing? json11 is intended to handle UTF8 properly throughout, which is usually transparent, but not in a few cases:

  • This is not specified by the JSON standard, but some browsers require \u2028 and \u2029 to be escaped:

    json11/json11.cpp

    Lines 90 to 97 in 8452587

    } else if (static_cast<uint8_t>(ch) == 0xe2 && static_cast<uint8_t>(value[i+1]) == 0x80
    && static_cast<uint8_t>(value[i+2]) == 0xa8) {
    out += "\\u2028";
    i += 2;
    } else if (static_cast<uint8_t>(ch) == 0xe2 && static_cast<uint8_t>(value[i+1]) == 0x80
    && static_cast<uint8_t>(value[i+2]) == 0xa9) {
    out += "\\u2029";
    i += 2;
  • JSON only provides \uXXXX escapes, which can't encode characters outside the BMP. The recommendation is to use surrogate pairs (UTF-16), so we need to pay special attention during decode in order to produce valid UTF-8 instead of CESU-8:

    json11/json11.cpp

    Lines 520 to 534 in 8452587

    // JSON specifies that characters outside the BMP shall be encoded as a pair
    // of 4-hex-digit \u escapes encoding their surrogate pair components. Check
    // whether we're in the middle of such a beast: the previous codepoint was an
    // escaped lead (high) surrogate, and this is a trail (low) surrogate.
    if (in_range(last_escaped_codepoint, 0xD800, 0xDBFF)
    && in_range(codepoint, 0xDC00, 0xDFFF)) {
    // Reassemble the two surrogate pairs into one astral-plane character, per
    // the UTF-16 algorithm.
    encode_utf8((((last_escaped_codepoint - 0xD800) << 10)
    | (codepoint - 0xDC00)) + 0x10000, out);
    last_escaped_codepoint = -1;
    } else {
    encode_utf8(last_escaped_codepoint, out);
    last_escaped_codepoint = codepoint;
    }

from json11.

szatmary avatar szatmary commented on May 21, 2024

Yeah, I may have jumped the gun there and imprudently blamed json11, Still investigating. but will close this in the mean time.

from json11.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.