2014-09-15 10:36:18 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
|
|
|
/* A JSON pretty-printer class. */
|
|
|
|
|
|
|
|
// A typical JSON-writing library requires you to first build up a data
|
|
|
|
// structure that represents a JSON object and then serialize it (to file, or
|
|
|
|
// somewhere else). This approach makes for a clean API, but building the data
|
|
|
|
// structure takes up memory. Sometimes that isn't desirable, such as when the
|
|
|
|
// JSON data is produced for memory reporting.
|
|
|
|
//
|
|
|
|
// The JSONWriter class instead allows JSON data to be written out
|
|
|
|
// incrementally without building up large data structures.
|
|
|
|
//
|
|
|
|
// The API is slightly uglier than you would see in a typical JSON-writing
|
|
|
|
// library, but still fairly easy to use. It's possible to generate invalid
|
|
|
|
// JSON with JSONWriter, but typically the most basic testing will identify any
|
|
|
|
// such problems.
|
|
|
|
//
|
|
|
|
// Similarly, there are no RAII facilities for automatically closing objects
|
|
|
|
// and arrays. These would be nice if you are generating all your code within
|
|
|
|
// nested functions, but in other cases you'd have to maintain an explicit
|
|
|
|
// stack of RAII objects and manually unwind it, which is no better than just
|
|
|
|
// calling "end" functions. Furthermore, the consequences of forgetting to
|
|
|
|
// close an object or array are obvious and, again, will be identified via
|
|
|
|
// basic testing, unlike other cases where RAII is typically used (e.g. smart
|
|
|
|
// pointers) and the consequences of defects are more subtle.
|
|
|
|
//
|
|
|
|
// Importantly, the class does solve the two hard problems of JSON
|
|
|
|
// pretty-printing, which are (a) correctly escaping strings, and (b) adding
|
|
|
|
// appropriate indentation and commas between items.
|
|
|
|
//
|
2014-10-03 05:01:19 +04:00
|
|
|
// By default, every property is placed on its own line. However, it is
|
|
|
|
// possible to request that objects and arrays be placed entirely on a single
|
|
|
|
// line, which can reduce output size significantly in some cases.
|
|
|
|
//
|
2014-09-15 10:36:18 +04:00
|
|
|
// Strings used (for property names and string property values) are |const
|
|
|
|
// char*| throughout, and can be ASCII or UTF-8.
|
|
|
|
//
|
|
|
|
// EXAMPLE
|
|
|
|
// -------
|
|
|
|
// Assume that |MyWriteFunc| is a class that implements |JSONWriteFunc|. The
|
|
|
|
// following code:
|
|
|
|
//
|
|
|
|
// JSONWriter w(MakeUnique<MyWriteFunc>());
|
|
|
|
// w.Start();
|
|
|
|
// {
|
|
|
|
// w.NullProperty("null");
|
|
|
|
// w.BoolProperty("bool", true);
|
|
|
|
// w.IntProperty("int", 1);
|
|
|
|
// w.StartArrayProperty("array");
|
|
|
|
// {
|
2014-10-03 05:01:32 +04:00
|
|
|
// w.StringElement("string");
|
2014-09-15 10:36:18 +04:00
|
|
|
// w.StartObjectElement();
|
|
|
|
// {
|
2014-10-03 05:01:32 +04:00
|
|
|
// w.DoubleProperty("double", 3.4);
|
2014-10-03 05:01:19 +04:00
|
|
|
// w.StartArrayProperty("single-line array", w.SingleLineStyle);
|
|
|
|
// {
|
|
|
|
// w.IntElement(1);
|
|
|
|
// w.StartObjectElement(); // SingleLineStyle is inherited from
|
|
|
|
// w.EndObjectElement(); // above for this collection
|
|
|
|
// }
|
|
|
|
// w.EndArray();
|
2014-09-15 10:36:18 +04:00
|
|
|
// }
|
|
|
|
// w.EndObjectElement();
|
|
|
|
// }
|
|
|
|
// w.EndArrayProperty();
|
|
|
|
// }
|
|
|
|
// w.End();
|
|
|
|
//
|
|
|
|
// will produce pretty-printed output for the following JSON object:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "null": null,
|
|
|
|
// "bool": true,
|
|
|
|
// "int": 1,
|
|
|
|
// "array": [
|
2014-10-03 05:01:32 +04:00
|
|
|
// "string",
|
2014-09-15 10:36:18 +04:00
|
|
|
// {
|
2014-10-03 05:01:32 +04:00
|
|
|
// "double": 3.4,
|
2014-10-03 05:01:19 +04:00
|
|
|
// "single-line array": [1, {}]
|
2014-09-15 10:36:18 +04:00
|
|
|
// }
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The nesting in the example code is obviously optional, but can aid
|
|
|
|
// readability.
|
|
|
|
|
|
|
|
#ifndef mozilla_JSONWriter_h
|
|
|
|
#define mozilla_JSONWriter_h
|
|
|
|
|
2018-02-21 11:15:43 +03:00
|
|
|
#include "double-conversion/double-conversion.h"
|
2018-08-06 17:44:26 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2014-09-15 10:36:18 +04:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
|
|
|
#include "mozilla/PodOperations.h"
|
2020-09-14 05:33:20 +03:00
|
|
|
#include "mozilla/Span.h"
|
2016-08-15 09:43:21 +03:00
|
|
|
#include "mozilla/Sprintf.h"
|
2014-09-15 10:36:18 +04:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/Vector.h"
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
#include <utility>
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// A quasi-functor for JSONWriter. We don't use a true functor because that
|
|
|
|
// requires templatizing JSONWriter, and the templatization seeps to lots of
|
|
|
|
// places we don't want it to.
|
|
|
|
class JSONWriteFunc {
|
|
|
|
public:
|
2020-09-14 05:33:20 +03:00
|
|
|
virtual void Write(const Span<const char>& aStr) = 0;
|
2020-02-13 14:20:27 +03:00
|
|
|
virtual ~JSONWriteFunc() = default;
|
2014-09-15 10:36:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Ideally this would be within |EscapedString| but when compiling with GCC
|
|
|
|
// on Linux that caused link errors, whereas this formulation didn't.
|
|
|
|
namespace detail {
|
|
|
|
extern MFBT_DATA const char gTwoCharEscapes[256];
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace detail
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
class JSONWriter {
|
|
|
|
// From http://www.ietf.org/rfc/rfc4627.txt:
|
|
|
|
//
|
|
|
|
// "All Unicode characters may be placed within the quotation marks except
|
|
|
|
// for the characters that must be escaped: quotation mark, reverse
|
|
|
|
// solidus, and the control characters (U+0000 through U+001F)."
|
|
|
|
//
|
|
|
|
// This implementation uses two-char escape sequences where possible, namely:
|
|
|
|
//
|
|
|
|
// \", \\, \b, \f, \n, \r, \t
|
|
|
|
//
|
|
|
|
// All control characters not in the above list are represented with a
|
|
|
|
// six-char escape sequence, e.g. '\u000b' (a.k.a. '\v').
|
|
|
|
//
|
|
|
|
class EscapedString {
|
2020-09-14 05:33:20 +03:00
|
|
|
// `mStringSpan` initially points at the user-provided string. If that
|
|
|
|
// string needs escaping, `mStringSpan` will point at `mOwnedStr` below.
|
|
|
|
Span<const char> mStringSpan;
|
|
|
|
// String storage in case escaping is actually needed, null otherwise.
|
2018-10-24 04:23:13 +03:00
|
|
|
UniquePtr<char[]> mOwnedStr;
|
2014-09-15 10:36:18 +04:00
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
void CheckInvariants() const {
|
|
|
|
// Either there was no escaping so `mOwnedStr` is null, or escaping was
|
|
|
|
// needed, in which case `mStringSpan` should point at `mOwnedStr`.
|
|
|
|
MOZ_ASSERT(!mOwnedStr || mStringSpan.data() == mOwnedStr.get());
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char hexDigitToAsciiChar(uint8_t u) {
|
|
|
|
u = u & 0xf;
|
|
|
|
return u < 10 ? '0' + u : 'a' + (u - 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2020-09-14 05:33:20 +03:00
|
|
|
explicit EscapedString(const Span<const char>& aStr) : mStringSpan(aStr) {
|
2014-09-15 10:36:18 +04:00
|
|
|
// First, see if we need to modify the string.
|
|
|
|
size_t nExtra = 0;
|
2020-09-14 05:33:20 +03:00
|
|
|
for (const char& c : aStr) {
|
|
|
|
// ensure it can't be interpreted as negative
|
|
|
|
uint8_t u = static_cast<uint8_t>(c);
|
2014-09-15 10:36:18 +04:00
|
|
|
if (u == 0) {
|
2020-09-14 05:33:20 +03:00
|
|
|
// Null terminator within the span, assume we may have been given a
|
|
|
|
// span to a buffer that contains a null-terminated string in it.
|
|
|
|
// We need to truncate the Span so that it doesn't include this null
|
|
|
|
// terminator and anything past it; Either we will return it as-is, or
|
|
|
|
// processing should stop there.
|
|
|
|
mStringSpan = mStringSpan.First(&c - mStringSpan.data());
|
2014-09-15 10:36:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (detail::gTwoCharEscapes[u]) {
|
|
|
|
nExtra += 1;
|
|
|
|
} else if (u <= 0x1f) {
|
|
|
|
nExtra += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
// Note: Don't use `aStr` anymore, as it could contain a null terminator;
|
|
|
|
// use the correctly-sized `mStringSpan` instead.
|
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
if (nExtra == 0) {
|
2020-09-14 05:33:20 +03:00
|
|
|
// No escapes needed. mStringSpan already points at the original string.
|
|
|
|
CheckInvariants();
|
2014-09-15 10:36:18 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escapes are needed. We'll create a new string.
|
2020-09-14 05:33:20 +03:00
|
|
|
mOwnedStr = MakeUnique<char[]>(mStringSpan.Length() + nExtra);
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
size_t i = 0;
|
2020-09-14 05:33:20 +03:00
|
|
|
for (const char c : mStringSpan) {
|
|
|
|
// ensure it can't be interpreted as negative
|
|
|
|
uint8_t u = static_cast<uint8_t>(c);
|
|
|
|
MOZ_ASSERT(u != 0, "Null terminator should have been handled above");
|
2014-09-15 10:36:18 +04:00
|
|
|
if (detail::gTwoCharEscapes[u]) {
|
|
|
|
mOwnedStr[i++] = '\\';
|
|
|
|
mOwnedStr[i++] = detail::gTwoCharEscapes[u];
|
|
|
|
} else if (u <= 0x1f) {
|
|
|
|
mOwnedStr[i++] = '\\';
|
|
|
|
mOwnedStr[i++] = 'u';
|
|
|
|
mOwnedStr[i++] = '0';
|
|
|
|
mOwnedStr[i++] = '0';
|
|
|
|
mOwnedStr[i++] = hexDigitToAsciiChar((u & 0x00f0) >> 4);
|
|
|
|
mOwnedStr[i++] = hexDigitToAsciiChar(u & 0x000f);
|
|
|
|
} else {
|
|
|
|
mOwnedStr[i++] = u;
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 05:33:20 +03:00
|
|
|
MOZ_ASSERT(i == mStringSpan.Length() + nExtra);
|
|
|
|
mStringSpan = Span<const char>(mOwnedStr.get(), i);
|
|
|
|
CheckInvariants();
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
explicit EscapedString(const char* aStr) = delete;
|
2014-09-15 10:36:18 +04:00
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
const Span<const char>& SpanRef() const { return mStringSpan; }
|
2014-09-15 10:36:18 +04:00
|
|
|
};
|
|
|
|
|
2014-10-03 05:01:19 +04:00
|
|
|
public:
|
|
|
|
// Collections (objects and arrays) are printed in a multi-line style by
|
|
|
|
// default. This can be changed to a single-line style if SingleLineStyle is
|
|
|
|
// specified. If a collection is printed in single-line style, every nested
|
|
|
|
// collection within it is also printed in single-line style, even if
|
|
|
|
// multi-line style is requested.
|
2022-07-28 15:41:56 +03:00
|
|
|
// If SingleLineStyle is set in the constructer, all JSON whitespace is
|
|
|
|
// eliminated, including spaces after colons and commas, for the most compact
|
|
|
|
// encoding possible.
|
2014-10-03 05:01:19 +04:00
|
|
|
enum CollectionStyle {
|
|
|
|
MultiLineStyle, // the default
|
|
|
|
SingleLineStyle
|
|
|
|
};
|
|
|
|
|
2015-05-12 00:16:44 +03:00
|
|
|
protected:
|
2020-09-14 05:33:20 +03:00
|
|
|
static constexpr Span<const char> scArrayBeginString = MakeStringSpan("[");
|
|
|
|
static constexpr Span<const char> scArrayEndString = MakeStringSpan("]");
|
2020-09-14 04:48:39 +03:00
|
|
|
static constexpr Span<const char> scCommaString = MakeStringSpan(",");
|
2020-09-14 05:33:20 +03:00
|
|
|
static constexpr Span<const char> scEmptyString = MakeStringSpan("");
|
|
|
|
static constexpr Span<const char> scFalseString = MakeStringSpan("false");
|
2020-09-14 04:48:39 +03:00
|
|
|
static constexpr Span<const char> scNewLineString = MakeStringSpan("\n");
|
2020-09-14 05:33:20 +03:00
|
|
|
static constexpr Span<const char> scNullString = MakeStringSpan("null");
|
|
|
|
static constexpr Span<const char> scObjectBeginString = MakeStringSpan("{");
|
|
|
|
static constexpr Span<const char> scObjectEndString = MakeStringSpan("}");
|
2020-09-14 04:48:39 +03:00
|
|
|
static constexpr Span<const char> scPropertyBeginString =
|
|
|
|
MakeStringSpan("\"");
|
2022-07-28 15:41:56 +03:00
|
|
|
static constexpr Span<const char> scPropertyEndString = MakeStringSpan("\":");
|
2020-09-14 04:48:39 +03:00
|
|
|
static constexpr Span<const char> scQuoteString = MakeStringSpan("\"");
|
|
|
|
static constexpr Span<const char> scSpaceString = MakeStringSpan(" ");
|
2020-09-14 05:33:20 +03:00
|
|
|
static constexpr Span<const char> scTopObjectBeginString =
|
|
|
|
MakeStringSpan("{");
|
2022-07-28 15:41:54 +03:00
|
|
|
static constexpr Span<const char> scTopObjectEndString = MakeStringSpan("}");
|
2020-09-14 05:33:20 +03:00
|
|
|
static constexpr Span<const char> scTrueString = MakeStringSpan("true");
|
|
|
|
|
2022-08-17 10:07:53 +03:00
|
|
|
JSONWriteFunc& mWriter;
|
|
|
|
const UniquePtr<JSONWriteFunc> mMaybeOwnedWriter;
|
2014-09-15 10:36:18 +04:00
|
|
|
Vector<bool, 8> mNeedComma; // do we need a comma at depth N?
|
2014-10-03 05:01:19 +04:00
|
|
|
Vector<bool, 8> mNeedNewlines; // do we need newlines at depth N?
|
2014-09-15 10:36:18 +04:00
|
|
|
size_t mDepth; // the current nesting depth
|
|
|
|
|
|
|
|
void Indent() {
|
|
|
|
for (size_t i = 0; i < mDepth; i++) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scSpaceString);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds whatever is necessary (maybe a comma, and then a newline and
|
|
|
|
// whitespace) to separate an item (property or element) from what's come
|
|
|
|
// before.
|
|
|
|
void Separator() {
|
|
|
|
if (mNeedComma[mDepth]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scCommaString);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
2014-10-03 05:01:19 +04:00
|
|
|
if (mDepth > 0 && mNeedNewlines[mDepth]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scNewLineString);
|
2014-10-03 05:01:19 +04:00
|
|
|
Indent();
|
2022-07-28 15:41:56 +03:00
|
|
|
} else if (mNeedComma[mDepth] && mNeedNewlines[0]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scSpaceString);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
void PropertyNameAndColon(const Span<const char>& aName) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scPropertyBeginString);
|
|
|
|
mWriter.Write(EscapedString(aName).SpanRef());
|
|
|
|
mWriter.Write(scPropertyEndString);
|
2022-07-28 15:41:56 +03:00
|
|
|
if (mNeedNewlines[0]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scSpaceString);
|
2022-07-28 15:41:56 +03:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
void Scalar(const Span<const char>& aMaybePropertyName,
|
|
|
|
const Span<const char>& aStringValue) {
|
2014-09-15 10:36:18 +04:00
|
|
|
Separator();
|
2020-09-14 05:33:20 +03:00
|
|
|
if (!aMaybePropertyName.empty()) {
|
2014-09-15 10:36:18 +04:00
|
|
|
PropertyNameAndColon(aMaybePropertyName);
|
|
|
|
}
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(aStringValue);
|
2014-09-15 10:36:18 +04:00
|
|
|
mNeedComma[mDepth] = true;
|
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
void QuotedScalar(const Span<const char>& aMaybePropertyName,
|
|
|
|
const Span<const char>& aStringValue) {
|
2014-09-15 10:36:18 +04:00
|
|
|
Separator();
|
2020-09-14 05:33:20 +03:00
|
|
|
if (!aMaybePropertyName.empty()) {
|
2014-09-15 10:36:18 +04:00
|
|
|
PropertyNameAndColon(aMaybePropertyName);
|
|
|
|
}
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scQuoteString);
|
|
|
|
mWriter.Write(aStringValue);
|
|
|
|
mWriter.Write(scQuoteString);
|
2014-09-15 10:36:18 +04:00
|
|
|
mNeedComma[mDepth] = true;
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:41:54 +03:00
|
|
|
void NewVectorEntries(bool aNeedNewLines) {
|
2014-10-03 05:01:19 +04:00
|
|
|
// If these tiny allocations OOM we might as well just crash because we
|
|
|
|
// must be in serious memory trouble.
|
2014-10-03 05:01:41 +04:00
|
|
|
MOZ_RELEASE_ASSERT(mNeedComma.resizeUninitialized(mDepth + 1));
|
|
|
|
MOZ_RELEASE_ASSERT(mNeedNewlines.resizeUninitialized(mDepth + 1));
|
2014-09-15 10:36:18 +04:00
|
|
|
mNeedComma[mDepth] = false;
|
2022-07-28 15:41:54 +03:00
|
|
|
mNeedNewlines[mDepth] = aNeedNewLines;
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
void StartCollection(const Span<const char>& aMaybePropertyName,
|
|
|
|
const Span<const char>& aStartChar,
|
2014-10-03 05:01:19 +04:00
|
|
|
CollectionStyle aStyle = MultiLineStyle) {
|
2014-09-15 10:36:18 +04:00
|
|
|
Separator();
|
2020-09-14 05:33:20 +03:00
|
|
|
if (!aMaybePropertyName.empty()) {
|
2018-06-08 20:50:25 +03:00
|
|
|
PropertyNameAndColon(aMaybePropertyName);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(aStartChar);
|
2014-09-15 10:36:18 +04:00
|
|
|
mNeedComma[mDepth] = true;
|
|
|
|
mDepth++;
|
2022-07-28 15:41:54 +03:00
|
|
|
NewVectorEntries(mNeedNewlines[mDepth - 1] && aStyle == MultiLineStyle);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds the whitespace and closing char necessary to end a collection.
|
2020-09-14 05:33:20 +03:00
|
|
|
void EndCollection(const Span<const char>& aEndChar) {
|
2018-08-06 17:44:26 +03:00
|
|
|
MOZ_ASSERT(mDepth > 0);
|
2014-10-03 05:01:19 +04:00
|
|
|
if (mNeedNewlines[mDepth]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scNewLineString);
|
2014-10-03 05:01:19 +04:00
|
|
|
mDepth--;
|
|
|
|
Indent();
|
|
|
|
} else {
|
|
|
|
mDepth--;
|
|
|
|
}
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(aEndChar);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2022-08-17 10:07:53 +03:00
|
|
|
explicit JSONWriter(JSONWriteFunc& aWriter,
|
|
|
|
CollectionStyle aStyle = MultiLineStyle)
|
|
|
|
: mWriter(aWriter), mNeedComma(), mNeedNewlines(), mDepth(0) {
|
|
|
|
NewVectorEntries(aStyle == MultiLineStyle);
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:41:54 +03:00
|
|
|
explicit JSONWriter(UniquePtr<JSONWriteFunc> aWriter,
|
|
|
|
CollectionStyle aStyle = MultiLineStyle)
|
2022-08-17 10:07:53 +03:00
|
|
|
: mWriter(*aWriter),
|
|
|
|
mMaybeOwnedWriter(std::move(aWriter)),
|
|
|
|
mNeedComma(),
|
|
|
|
mNeedNewlines(),
|
|
|
|
mDepth(0) {
|
2022-08-17 10:07:53 +03:00
|
|
|
MOZ_RELEASE_ASSERT(
|
2022-08-17 10:07:53 +03:00
|
|
|
mMaybeOwnedWriter,
|
2022-08-17 10:07:53 +03:00
|
|
|
"JSONWriter must be given a non-null UniquePtr<JSONWriteFunc>");
|
2022-07-28 15:41:54 +03:00
|
|
|
NewVectorEntries(aStyle == MultiLineStyle);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the JSONWriteFunc passed in at creation, for temporary use. The
|
|
|
|
// JSONWriter object still owns the JSONWriteFunc.
|
2022-08-17 10:07:53 +03:00
|
|
|
JSONWriteFunc& WriteFunc() const { return mWriter; }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// For all the following functions, the "Prints:" comment indicates what the
|
2014-10-03 05:01:19 +04:00
|
|
|
// basic output looks like. However, it doesn't indicate the whitespace and
|
2014-09-15 10:36:18 +04:00
|
|
|
// trailing commas, which are automatically added as required.
|
|
|
|
//
|
|
|
|
// All property names and string properties are escaped as necessary.
|
|
|
|
|
|
|
|
// Prints: {
|
2014-10-03 05:01:19 +04:00
|
|
|
void Start(CollectionStyle aStyle = MultiLineStyle) {
|
2020-09-14 05:33:20 +03:00
|
|
|
StartCollection(scEmptyString, scTopObjectBeginString, aStyle);
|
2014-10-03 05:01:19 +04:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
2020-09-14 05:33:20 +03:00
|
|
|
// Prints: } and final newline.
|
2022-07-28 15:41:54 +03:00
|
|
|
void End() {
|
|
|
|
EndCollection(scTopObjectEndString);
|
|
|
|
if (mNeedNewlines[mDepth]) {
|
2022-08-17 10:07:53 +03:00
|
|
|
mWriter.Write(scNewLineString);
|
2022-07-28 15:41:54 +03:00
|
|
|
}
|
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": null
|
2020-09-14 05:33:20 +03:00
|
|
|
void NullProperty(const Span<const char>& aName) {
|
|
|
|
Scalar(aName, scNullString);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void NullProperty(const char (&aName)[N]) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
NullProperty(Span<const char>(aName, N));
|
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: null
|
2020-09-14 05:33:20 +03:00
|
|
|
void NullElement() { NullProperty(scEmptyString); }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": <aBool>
|
2020-09-14 05:33:20 +03:00
|
|
|
void BoolProperty(const Span<const char>& aName, bool aBool) {
|
|
|
|
Scalar(aName, aBool ? scTrueString : scFalseString);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void BoolProperty(const char (&aName)[N], bool aBool) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
BoolProperty(Span<const char>(aName, N), aBool);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prints: <aBool>
|
2020-09-14 05:33:20 +03:00
|
|
|
void BoolElement(bool aBool) { BoolProperty(scEmptyString, aBool); }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": <aInt>
|
2020-09-14 05:33:20 +03:00
|
|
|
void IntProperty(const Span<const char>& aName, int64_t aInt) {
|
2014-09-15 10:36:18 +04:00
|
|
|
char buf[64];
|
2020-05-08 19:24:31 +03:00
|
|
|
int len = SprintfLiteral(buf, "%" PRId64, aInt);
|
2020-09-14 05:33:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(len > 0);
|
|
|
|
Scalar(aName, Span<const char>(buf, size_t(len)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void IntProperty(const char (&aName)[N], int64_t aInt) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
IntProperty(Span<const char>(aName, N), aInt);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prints: <aInt>
|
2020-09-14 05:33:20 +03:00
|
|
|
void IntElement(int64_t aInt) { IntProperty(scEmptyString, aInt); }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": <aDouble>
|
2020-09-14 05:33:20 +03:00
|
|
|
void DoubleProperty(const Span<const char>& aName, double aDouble) {
|
2014-09-15 10:36:18 +04:00
|
|
|
static const size_t buflen = 64;
|
|
|
|
char buf[buflen];
|
|
|
|
const double_conversion::DoubleToStringConverter& converter =
|
|
|
|
double_conversion::DoubleToStringConverter::EcmaScriptConverter();
|
|
|
|
double_conversion::StringBuilder builder(buf, buflen);
|
|
|
|
converter.ToShortest(aDouble, &builder);
|
2020-09-14 05:33:20 +03:00
|
|
|
// TODO: The builder should know the length?!
|
|
|
|
Scalar(aName, MakeStringSpan(builder.Finalize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void DoubleProperty(const char (&aName)[N], double aDouble) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
DoubleProperty(Span<const char>(aName, N), aDouble);
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prints: <aDouble>
|
2020-09-14 05:33:20 +03:00
|
|
|
void DoubleElement(double aDouble) { DoubleProperty(scEmptyString, aDouble); }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": "<aStr>"
|
2020-09-14 05:33:20 +03:00
|
|
|
void StringProperty(const Span<const char>& aName,
|
|
|
|
const Span<const char>& aStr) {
|
|
|
|
QuotedScalar(aName, EscapedString(aStr).SpanRef());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t NN>
|
|
|
|
void StringProperty(const char (&aName)[NN], const Span<const char>& aStr) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StringProperty(Span<const char>(aName, NN), aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t SN>
|
|
|
|
void StringProperty(const Span<const char>& aName, const char (&aStr)[SN]) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StringProperty(aName, Span<const char>(aStr, SN));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t NN, size_t SN>
|
|
|
|
void StringProperty(const char (&aName)[NN], const char (&aStr)[SN]) {
|
|
|
|
// Keep null terminators from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StringProperty(Span<const char>(aName, NN), Span<const char>(aStr, SN));
|
2014-09-15 10:36:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prints: "<aStr>"
|
2020-09-14 05:33:20 +03:00
|
|
|
void StringElement(const Span<const char>& aStr) {
|
|
|
|
StringProperty(scEmptyString, aStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void StringElement(const char (&aName)[N]) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StringElement(Span<const char>(aName, N));
|
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": [
|
2020-09-14 05:33:20 +03:00
|
|
|
void StartArrayProperty(const Span<const char>& aName,
|
2014-10-03 05:01:19 +04:00
|
|
|
CollectionStyle aStyle = MultiLineStyle) {
|
2020-09-14 05:33:20 +03:00
|
|
|
StartCollection(aName, scArrayBeginString, aStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void StartArrayProperty(const char (&aName)[N],
|
|
|
|
CollectionStyle aStyle = MultiLineStyle) {
|
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StartArrayProperty(Span<const char>(aName, N), aStyle);
|
2014-10-03 05:01:19 +04:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: [
|
2014-10-03 05:01:19 +04:00
|
|
|
void StartArrayElement(CollectionStyle aStyle = MultiLineStyle) {
|
2020-09-14 05:33:20 +03:00
|
|
|
StartArrayProperty(scEmptyString, aStyle);
|
2014-10-03 05:01:19 +04:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: ]
|
2020-09-14 05:33:20 +03:00
|
|
|
void EndArray() { EndCollection(scArrayEndString); }
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: "<aName>": {
|
2020-09-14 05:33:20 +03:00
|
|
|
void StartObjectProperty(const Span<const char>& aName,
|
|
|
|
CollectionStyle aStyle = MultiLineStyle) {
|
|
|
|
StartCollection(aName, scObjectBeginString, aStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
void StartObjectProperty(const char (&aName)[N],
|
2014-10-03 05:01:19 +04:00
|
|
|
CollectionStyle aStyle = MultiLineStyle) {
|
2020-09-14 05:33:20 +03:00
|
|
|
// Keep null terminator from literal strings, will be removed by
|
|
|
|
// EscapedString. This way C buffer arrays can be used as well.
|
|
|
|
StartObjectProperty(Span<const char>(aName, N), aStyle);
|
2014-10-03 05:01:19 +04:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: {
|
2014-10-03 05:01:19 +04:00
|
|
|
void StartObjectElement(CollectionStyle aStyle = MultiLineStyle) {
|
2020-09-14 05:33:20 +03:00
|
|
|
StartObjectProperty(scEmptyString, aStyle);
|
2014-10-03 05:01:19 +04:00
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// Prints: }
|
2020-09-14 05:33:20 +03:00
|
|
|
void EndObject() { EndCollection(scObjectEndString); }
|
2014-09-15 10:36:18 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif /* mozilla_JSONWriter_h */
|