/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
using mozilla::JSONWriteFunc; using mozilla::JSONWriter; using mozilla::MakeStringSpan; using mozilla::MakeUnique; using mozilla::Span;
// This writes all the output into a big buffer. struct StringWriteFunc final : public JSONWriteFunc {
std::string mString;
void Write(const mozilla::Span<constchar>& aStr) final {
mString.append(aStr.data(), aStr.size());
}
};
void Check(JSONWriter& aWriter, constchar* aExpected) {
JSONWriteFunc& func = aWriter.WriteFunc(); const std::string& actual = static_cast<StringWriteFunc&>(func).mString; if (strcmp(aExpected, actual.c_str()) != 0) {
fprintf(stderr, "---- EXPECTED ----\n<<<%s>>>\n" "---- ACTUAL ----\n<<<%s>>>\n",
aExpected, actual.c_str());
MOZ_RELEASE_ASSERT(false, "expected and actual output don't match");
}
}
// Note: to convert actual output into |expected| strings that C++ can handle, // apply the following substitutions, in order, to each line. // - s/\\/\\\\/g # escapes backslashes // - s/"/\\"/g # escapes quotes // - s/$/\\n\\/ # adds a newline and string continuation char to each line
void TestStringEscaping() { // This test uses hexadecimal character escapes because UTF8 literals cause // problems for some compilers (see bug 1069726). constchar* expected = "\
{\n\
\"ascii\": \"\x7F~}|{zyxwvutsrqponmlkjihgfedcba`_^]\\\\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#\\\"! \\u001f\\u001e\\u001d\\u001c\\u001b\\u001a\\u0019\\u0018\\u0017\\u0016\\u0015\\u0014\\u0013\\u0012\\u0011\\u0010\\u000f\\u000e\\r\\f\\u000b\\n\\t\\b\\u0007\\u0006\\u0005\\u0004\\u0003\\u0002\\u0001\",\n\
\"\xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7 \xD9\x87\xD9\x86\xD8\xA7\xD9\x83\": true,\n\
\"\xD5\xA2\xD5\xA1\xD6\x80\xD5\xA5\xD6\x82 \xD5\xB9\xD5\xAF\xD5\xA1\": -123,\n\
\"\xE4\xBD\xA0\xE5\xA5\xBD\": 1.234,\n\
\"\xCE\xB3\xCE\xB5\xCE\xB9\xCE\xB1 \xCE\xB5\xCE\xBA\xCE\xB5\xCE\xAF\": \"\xD8\xB3\xD9\x84\xD8\xA7\xD9\x85\",\n\
\"hall\xC3\xB3 \xC3\xBE" "arna\": 4660,\n\
\"\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF\": {\n\
\"\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82\": [\n\
]\n\
}\n\
}\n\ ";
JSONWriter w(MakeUnique<StringWriteFunc>());
// Test the string escaping behaviour.
w.Start();
{ // Test all 127 ascii values. Do it in reverse order so that the 0 // at the end serves as the null char. char buf[128]; for (int i = 0; i < 128; i++) {
buf[i] = 127 - i;
}
w.StringProperty("ascii", buf);
// Test lots of unicode stuff. Note that this file is encoded as UTF-8.
w.BoolProperty( "\xD9\x85\xD8\xB1\xD8\xAD\xD8\xA8\xD8\xA7 " "\xD9\x87\xD9\x86\xD8\xA7\xD9\x83", true);
w.IntProperty( "\xD5\xA2\xD5\xA1\xD6\x80\xD5\xA5\xD6\x82 \xD5\xB9\xD5\xAF\xD5\xA1",
-123);
w.DoubleProperty("\xE4\xBD\xA0\xE5\xA5\xBD", 1.234);
w.StringProperty( "\xCE\xB3\xCE\xB5\xCE\xB9\xCE\xB1 \xCE\xB5\xCE\xBA\xCE\xB5\xCE\xAF", "\xD8\xB3\xD9\x84\xD8\xA7\xD9\x85");
w.IntProperty( "hall\xC3\xB3 \xC3\xBE" "arna",
0x1234);
w.StartObjectProperty( "\xE3\x81\x93\xE3\x82\x93\xE3\x81\xAB\xE3\x81\xA1\xE3\x81\xAF");
{
w.StartArrayProperty("\xD0\xBF\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82");
w.EndArray();
}
w.EndObject();
}
w.End();
w.Start();
{ staticconstint n = 10; for (int i = 0; i < n; i++) {
w.StartArrayProperty("a");
w.StartObjectElement();
} for (int i = 0; i < n; i++) {
w.EndObject();
w.EndArray();
}
}
w.End();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.