Flutter Engine
The Flutter Engine
Public Types | Public Member Functions | List of all members
SkJSONWriter Class Reference

#include <SkJSONWriter.h>

Inheritance diagram for SkJSONWriter:
SkNoncopyable NanoJSONResultsWriter

Public Types

enum class  Mode { kFast , kPretty }
 

Public Member Functions

 SkJSONWriter (SkWStream *stream, Mode mode=Mode::kFast)
 
 ~SkJSONWriter ()
 
void flush ()
 
void appendName (const char *name)
 
void beginObject (const char *name=nullptr, bool multiline=true)
 
void endObject ()
 
void beginArray (const char *name=nullptr, bool multiline=true)
 
void endArray ()
 
void appendString (const char *value, size_t size)
 
void appendString (const SkString &value)
 
template<class T , std::enable_if_t< std::is_same_v< T, std::string >, bool > = false>
void appendString (const T &value)
 
template<size_t N>
void appendNString (char const (&value)[N])
 
void appendCString (const char *value)
 
void appendPointer (const void *value)
 
void appendBool (bool value)
 
void appendS32 (int32_t value)
 
void appendS64 (int64_t value)
 
void appendU32 (uint32_t value)
 
void appendU64 (uint64_t value)
 
void appendFloat (float value)
 
void appendDouble (double value)
 
void appendFloatDigits (float value, int digits)
 
void appendDoubleDigits (double value, int digits)
 
void appendHexU32 (uint32_t value)
 
void appendHexU64 (uint64_t value)
 
void appendString (const char *name, const char *value, size_t size)
 
void appendString (const char *name, const SkString &value)
 
template<class T , std::enable_if_t< std::is_same_v< T, std::string >, bool > = false>
void appendString (const char *name, const T &value)
 
template<size_t N>
void appendNString (const char *name, char const (&value)[N])
 
void appendCString (const char *name, const char *value)
 
void appendFloatDigits (const char *name, float value, int digits)
 
void appendDoubleDigits (const char *name, double value, int digits)
 

Detailed Description

Lightweight class for writing properly structured JSON data. No random-access, everything must be generated in-order. The resulting JSON is written directly to the SkWStream supplied at construction time. Output is buffered, so writing to disk (via an SkFILEWStream) is ideal.

There is a basic state machine to ensure that JSON is structured correctly, and to allow for (optional) pretty formatting.

This class adheres to the RFC-4627 usage of JSON (not ECMA-404). In other words, all JSON created with this class must have a top-level object or array. Free-floating values of other types are not considered valid.

Note that all error checking is in the form of asserts - invalid usage in a non-debug build will simply produce invalid JSON.

Definition at line 38 of file SkJSONWriter.h.

Member Enumeration Documentation

◆ Mode

enum class SkJSONWriter::Mode
strong
Enumerator
kFast 

Output the minimal amount of text. No additional whitespace (including newlines) is generated. The resulting JSON is suitable for fast parsing and machine consumption.

kPretty 

Output human-readable JSON, with indented objects and arrays, and one value per line. Slightly slower than kFast, and produces data that is somewhat larger.

Definition at line 40 of file SkJSONWriter.h.

40 {
41 /**
42 * Output the minimal amount of text. No additional whitespace (including newlines) is
43 * generated. The resulting JSON is suitable for fast parsing and machine consumption.
44 */
45 kFast,
46
47 /**
48 * Output human-readable JSON, with indented objects and arrays, and one value per line.
49 * Slightly slower than kFast, and produces data that is somewhat larger.
50 */
51 kPretty
52 };
@ kFast
Faster, but may sample outside the bounds of the source rectangle.

Constructor & Destructor Documentation

◆ SkJSONWriter()

SkJSONWriter::SkJSONWriter ( SkWStream stream,
Mode  mode = Mode::kFast 
)
inline

Construct a JSON writer that will serialize all the generated JSON to 'stream'.

Definition at line 57 of file SkJSONWriter.h.

58 : fBlock(new char[kBlockSize])
59 , fWrite(fBlock)
60 , fBlockEnd(fBlock + kBlockSize)
61 , fStream(stream)
62 , fMode(mode)
63 , fState(State::kStart) {
64 fScopeStack.push_back(Scope::kNone);
65 fNewlineStack.push_back(true);
66 }
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228

◆ ~SkJSONWriter()

SkJSONWriter::~SkJSONWriter ( )
inline

Definition at line 68 of file SkJSONWriter.h.

68 {
69 this->flush();
70 delete[] fBlock;
71 SkASSERT(fScopeStack.size() == 1);
72 SkASSERT(fNewlineStack.size() == 1);
73 }
#define SkASSERT(cond)
Definition: SkAssert.h:116
void flush()
Definition: SkJSONWriter.h:78
int size() const
Definition: SkTArray.h:421

Member Function Documentation

◆ appendBool()

void SkJSONWriter::appendBool ( bool  value)
inline

Definition at line 229 of file SkJSONWriter.h.

229 {
230 this->beginValue();
231 if (value) {
232 this->write("true", 4);
233 } else {
234 this->write("false", 5);
235 }
236 }
uint8_t value

◆ appendCString() [1/2]

void SkJSONWriter::appendCString ( const char *  name,
const char *  value 
)
inline

Definition at line 273 of file SkJSONWriter.h.

273 {
274 this->appendName(name);
275 this->appendString(value, value ? strlen(value) : 0);
276 }
void appendName(const char *name)
Definition: SkJSONWriter.h:90
void appendString(const char *value, size_t size)
Definition: SkJSONWriter.h:176
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32

◆ appendCString() [2/2]

void SkJSONWriter::appendCString ( const char *  value)
inline

Definition at line 224 of file SkJSONWriter.h.

224 {
225 this->appendString(value, value ? strlen(value) : 0);
226 }

◆ appendDouble()

void SkJSONWriter::appendDouble ( double  value)
inline

Definition at line 242 of file SkJSONWriter.h.

242{ this->beginValue(); this->appendf("%g", value); }

◆ appendDoubleDigits() [1/2]

void SkJSONWriter::appendDoubleDigits ( const char *  name,
double  value,
int  digits 
)
inline

Definition at line 301 of file SkJSONWriter.h.

301 {
302 this->appendName(name);
303 this->appendDoubleDigits(value, digits);
304 }
void appendDoubleDigits(double value, int digits)
Definition: SkJSONWriter.h:247

◆ appendDoubleDigits() [2/2]

void SkJSONWriter::appendDoubleDigits ( double  value,
int  digits 
)
inline

Definition at line 247 of file SkJSONWriter.h.

247 {
248 this->beginValue();
249 this->appendf("%.*g", digits, value);
250 }

◆ appendFloat()

void SkJSONWriter::appendFloat ( float  value)
inline

Definition at line 241 of file SkJSONWriter.h.

241{ this->beginValue(); this->appendf("%g", value); }

◆ appendFloatDigits() [1/2]

void SkJSONWriter::appendFloatDigits ( const char *  name,
float  value,
int  digits 
)
inline

Functions for adding named values of various types. These add a name field, so must be called between beginObject() and endObject().

Definition at line 297 of file SkJSONWriter.h.

297 {
298 this->appendName(name);
299 this->appendFloatDigits(value, digits);
300 }
void appendFloatDigits(float value, int digits)
Definition: SkJSONWriter.h:243

◆ appendFloatDigits() [2/2]

void SkJSONWriter::appendFloatDigits ( float  value,
int  digits 
)
inline

Definition at line 243 of file SkJSONWriter.h.

243 {
244 this->beginValue();
245 this->appendf("%.*g", digits, value);
246 }

◆ appendHexU32()

void SkJSONWriter::appendHexU32 ( uint32_t  value)
inline

Definition at line 251 of file SkJSONWriter.h.

251{ this->beginValue(); this->appendf("\"0x%x\"", value); }

◆ appendHexU64()

void SkJSONWriter::appendHexU64 ( uint64_t  value)

Definition at line 29 of file SkJSONWriter.cpp.

29 {
30 this->beginValue();
31 this->appendf("\"0x%" PRIx64 "\"", value);
32}

◆ appendName()

void SkJSONWriter::appendName ( const char *  name)
inline

Append the name (key) portion of an object member. Must be called between beginObject() and endObject(). If you have both the name and value of an object member, you can simply call the two argument versions of the other append functions.

Definition at line 90 of file SkJSONWriter.h.

90 {
91 if (!name) {
92 return;
93 }
94 SkASSERT(Scope::kObject == this->scope());
95 SkASSERT(State::kObjectBegin == fState || State::kObjectValue == fState);
96 if (State::kObjectValue == fState) {
97 this->write(",", 1);
98 }
99 this->separator(this->multiline());
100 this->write("\"", 1);
101 this->write(name, strlen(name));
102 this->write("\":", 2);
103 fState = State::kObjectName;
104 }

◆ appendNString() [1/2]

template<size_t N>
void SkJSONWriter::appendNString ( char const (&)  value[N])
inline

Definition at line 220 of file SkJSONWriter.h.

220 {
221 static_assert(N > 0);
222 this->appendString(value, N-1);
223 }
#define N
Definition: beziers.cpp:19

◆ appendNString() [2/2]

template<size_t N>
void SkJSONWriter::appendNString ( const char *  name,
char const (&)  value[N] 
)
inline

Definition at line 268 of file SkJSONWriter.h.

268 {
269 static_assert(N > 0);
270 this->appendName(name);
271 this->appendString(value, N-1);
272 }

◆ appendPointer()

void SkJSONWriter::appendPointer ( const void *  value)
inline

Definition at line 228 of file SkJSONWriter.h.

228{ this->beginValue(); this->appendf("\"%p\"", value); }

◆ appendS32()

void SkJSONWriter::appendS32 ( int32_t  value)
inline

Definition at line 237 of file SkJSONWriter.h.

237{ this->beginValue(); this->appendf("%d", value); }

◆ appendS64()

void SkJSONWriter::appendS64 ( int64_t  value)

Definition at line 19 of file SkJSONWriter.cpp.

19 {
20 this->beginValue();
21 this->appendf("%" PRId64, value);
22}

◆ appendString() [1/6]

void SkJSONWriter::appendString ( const char *  name,
const char *  value,
size_t  size 
)
inline

Definition at line 254 of file SkJSONWriter.h.

254 {
255 this->appendName(name);
256 this->appendString(value, size);
257 }
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ appendString() [2/6]

void SkJSONWriter::appendString ( const char *  name,
const SkString value 
)
inline

Definition at line 258 of file SkJSONWriter.h.

258 {
259 this->appendName(name);
260 this->appendString(value.c_str(), value.size());
261 }
const char * c_str() const
Definition: SkString.h:133

◆ appendString() [3/6]

template<class T , std::enable_if_t< std::is_same_v< T, std::string >, bool > = false>
void SkJSONWriter::appendString ( const char *  name,
const T value 
)
inline

Definition at line 264 of file SkJSONWriter.h.

264 {
265 this->appendName(name);
266 this->appendString(value.data(), value.size());
267 }

◆ appendString() [4/6]

void SkJSONWriter::appendString ( const char *  value,
size_t  size 
)
inline

Functions for adding values of various types. The single argument versions add un-named values, so must be called either

Definition at line 176 of file SkJSONWriter.h.

176 {
177 this->beginValue();
178 this->write("\"", 1);
179 if (value) {
180 char const * const end = value + size;
181 while (value < end) {
182 char const * next = value;
184 switch (u) {
185 case '"': this->write("\\\"", 2); break;
186 case '\\': this->write("\\\\", 2); break;
187 case '\b': this->write("\\b", 2); break;
188 case '\f': this->write("\\f", 2); break;
189 case '\n': this->write("\\n", 2); break;
190 case '\r': this->write("\\r", 2); break;
191 case '\t': this->write("\\t", 2); break;
192 default: {
193 if (u < 0) {
194 next = value + 1;
195 SkString s("\\u");
196 s.appendHex((unsigned char)*value, 4);
197 this->write(s.c_str(), s.size());
198 } else if (u < 0x20) {
199 SkString s("\\u");
200 s.appendHex(u, 4);
201 this->write(s.c_str(), s.size());
202 } else {
203 this->write(value, next - value);
204 }
205 } break;
206 }
207 value = next;
208 }
209 }
210 this->write("\"", 1);
211 }
static float next(float f)
int32_t SkUnichar
Definition: SkTypes.h:175
struct MyStruct s
glong glong end
SK_SPI SkUnichar NextUTF8(const char **ptr, const char *end)
Definition: SkUTF.cpp:118

◆ appendString() [5/6]

void SkJSONWriter::appendString ( const SkString value)
inline

Definition at line 212 of file SkJSONWriter.h.

212 {
213 this->appendString(value.c_str(), value.size());
214 }

◆ appendString() [6/6]

template<class T , std::enable_if_t< std::is_same_v< T, std::string >, bool > = false>
void SkJSONWriter::appendString ( const T value)
inline

Definition at line 217 of file SkJSONWriter.h.

217 {
218 this->appendString(value.data(), value.size());
219 }

◆ appendU32()

void SkJSONWriter::appendU32 ( uint32_t  value)
inline

Definition at line 239 of file SkJSONWriter.h.

239{ this->beginValue(); this->appendf("%u", value); }

◆ appendU64()

void SkJSONWriter::appendU64 ( uint64_t  value)

Definition at line 24 of file SkJSONWriter.cpp.

24 {
25 this->beginValue();
26 this->appendf("%" PRIu64, value);
27}

◆ beginArray()

void SkJSONWriter::beginArray ( const char *  name = nullptr,
bool  multiline = true 
)
inline

Adds a new array. A name must be supplied when called between beginObject() and endObject(). Calls to beginArray() must be balanced by corresponding calls to endArray(). By default, arrays are written out with one value per line (when in kPretty mode). This can be overridden for a particular array by passing false for multiline, this will keep the entire array on a single line. This can help with readability in some situations. In kFast mode, this parameter is ignored.

Definition at line 146 of file SkJSONWriter.h.

146 {
147 this->appendName(name);
148 this->beginValue(true);
149 this->write("[", 1);
150 fScopeStack.push_back(Scope::kArray);
151 fNewlineStack.push_back(multiline);
152 fState = State::kArrayBegin;
153 }

◆ beginObject()

void SkJSONWriter::beginObject ( const char *  name = nullptr,
bool  multiline = true 
)
inline

Adds a new object. A name must be supplied when called between beginObject() and endObject(). Calls to beginObject() must be balanced by corresponding calls to endObject(). By default, objects are written out with one named value per line (when in kPretty mode). This can be overridden for a particular object by passing false for multiline, this will keep the entire object on a single line. This can help with readability in some situations. In kFast mode, this parameter is ignored.

Definition at line 114 of file SkJSONWriter.h.

114 {
115 this->appendName(name);
116 this->beginValue(true);
117 this->write("{", 1);
118 fScopeStack.push_back(Scope::kObject);
119 fNewlineStack.push_back(multiline);
120 fState = State::kObjectBegin;
121 }

◆ endArray()

void SkJSONWriter::endArray ( )
inline

Ends an array that was previous started with beginArray().

Definition at line 158 of file SkJSONWriter.h.

158 {
159 SkASSERT(Scope::kArray == this->scope());
160 SkASSERT(State::kArrayBegin == fState || State::kArrayValue == fState);
161 bool emptyArray = State::kArrayBegin == fState;
162 bool wasMultiline = this->multiline();
163 this->popScope();
164 if (!emptyArray) {
165 this->separator(wasMultiline);
166 }
167 this->write("]", 1);
168 }

◆ endObject()

void SkJSONWriter::endObject ( )
inline

Ends an object that was previously started with beginObject().

Definition at line 126 of file SkJSONWriter.h.

126 {
127 SkASSERT(Scope::kObject == this->scope());
128 SkASSERT(State::kObjectBegin == fState || State::kObjectValue == fState);
129 bool emptyObject = State::kObjectBegin == fState;
130 bool wasMultiline = this->multiline();
131 this->popScope();
132 if (!emptyObject) {
133 this->separator(wasMultiline);
134 }
135 this->write("}", 1);
136 }

◆ flush()

void SkJSONWriter::flush ( )
inline

Force all buffered output to be flushed to the underlying stream.

Definition at line 78 of file SkJSONWriter.h.

78 {
79 if (fWrite != fBlock) {
80 fStream->write(fBlock, fWrite - fBlock);
81 fWrite = fBlock;
82 }
83 }
virtual bool write(const void *buffer, size_t size)=0

The documentation for this class was generated from the following files: