Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Member Functions | List of all members
dart::JSONWriter Class Reference

#include <json_writer.h>

Inheritance diagram for dart::JSONWriter:
dart::ValueObject

Public Member Functions

 JSONWriter (intptr_t buf_size=256)
 
TextBufferbuffer ()
 
const char * ToCString ()
 
void Steal (char **buffer, intptr_t *buffer_length)
 
void PrintCommaIfNeeded ()
 
void AppendBytes (const uint8_t *buffer, intptr_t buffer_length)
 
void AppendBytesInBase64 (const uint8_t *bytes, intptr_t length)
 
void AppendSerializedObject (const char *serialized_object)
 
void AppendSerializedObject (const char *property_name, const char *serialized_object)
 
void OpenObject (const char *property_name=nullptr)
 
void CloseObject ()
 
void UncloseObject ()
 
void OpenArray (const char *property_name=nullptr)
 
void CloseArray ()
 
void Clear ()
 
void PrintValueNull ()
 
void PrintValueBool (bool b)
 
void PrintValue (intptr_t i)
 
void PrintValue64 (int64_t i)
 
void PrintValue (double d)
 
void PrintValueBase64 (const uint8_t *bytes, intptr_t length)
 
void PrintValue (const char *s)
 
void PrintValue (const char *s, intptr_t len)
 
void PrintValueNoEscape (const char *s)
 
void PrintfValue (const char *format,...) PRINTF_ATTRIBUTE(2
 
void void VPrintfValue (const char *format, va_list args)
 
bool PrintValueStr (const String &s, intptr_t offset, intptr_t count)
 
void PrintPropertyBool (const char *name, bool b)
 
void PrintProperty (const char *name, intptr_t i)
 
void PrintProperty64 (const char *name, int64_t i)
 
void PrintProperty (const char *name, double d)
 
void PrintPropertyBase64 (const char *name, const uint8_t *bytes, intptr_t length)
 
void PrintProperty (const char *name, const char *s)
 
bool PrintPropertyStr (const char *name, const String &s, intptr_t offset=0, intptr_t count=-1)
 
void PrintPropertyNoEscape (const char *name, const char *s)
 
void PrintfProperty (const char *name, const char *format,...) PRINTF_ATTRIBUTE(3
 
void void VPrintfProperty (const char *name, const char *format, va_list args)
 
void PrintPropertyName (const char *name)
 
void PrintNewline ()
 
void AddEscapedUTF8String (const char *s)
 
void AddEscapedUTF8String (const char *s, intptr_t len)
 

Detailed Description

Definition at line 15 of file json_writer.h.

Constructor & Destructor Documentation

◆ JSONWriter()

dart::JSONWriter::JSONWriter ( intptr_t  buf_size = 256)
explicit

Definition at line 35 of file json_writer.cc.

36 : open_objects_(0), buffer_(buf_size) {}

Member Function Documentation

◆ AddEscapedUTF8String() [1/2]

void dart::JSONWriter::AddEscapedUTF8String ( const char *  s)

Definition at line 348 of file json_writer.cc.

348 {
349 if (s == nullptr) {
350 return;
351 }
352 intptr_t len = strlen(s);
354}
void AddEscapedUTF8String(const char *s)
struct MyStruct s

◆ AddEscapedUTF8String() [2/2]

void dart::JSONWriter::AddEscapedUTF8String ( const char *  s,
intptr_t  len 
)

Definition at line 356 of file json_writer.cc.

356 {
357 if (s == nullptr) {
358 return;
359 }
360 buffer_.AddEscapedUTF8(s, len);
361}
void AddEscapedUTF8(const char *s, intptr_t len)

◆ AppendBytes()

void dart::JSONWriter::AppendBytes ( const uint8_t *  buffer,
intptr_t  buffer_length 
)

Definition at line 38 of file json_writer.cc.

38 {
39 buffer_.AddRaw(buffer, buffer_length);
40}
void AddRaw(const uint8_t *buffer, intptr_t buffer_length)
TextBuffer * buffer()
Definition json_writer.h:19

◆ AppendBytesInBase64()

void dart::JSONWriter::AppendBytesInBase64 ( const uint8_t *  bytes,
intptr_t  length 
)

Definition at line 46 of file json_writer.cc.

46 {
47 ASSERT(bytes != nullptr);
48 intptr_t odd_bits = length % 3;
49 intptr_t even_bits = length - odd_bits;
50 for (intptr_t i = 0; i < even_bits; i += 3) {
51 intptr_t triplet = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
52 buffer_.AddChar(base64_digits[triplet >> 18]);
53 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
54 buffer_.AddChar(base64_digits[(triplet >> 6) & 63]);
55 buffer_.AddChar(base64_digits[triplet & 63]);
56 }
57 if (odd_bits == 1) {
58 intptr_t triplet = bytes[even_bits] << 16;
59 buffer_.AddChar(base64_digits[triplet >> 18]);
60 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
61 buffer_.AddChar(base64_pad);
62 buffer_.AddChar(base64_pad);
63 } else if (odd_bits == 2) {
64 intptr_t triplet = (bytes[even_bits] << 16) | (bytes[even_bits + 1] << 8);
65 buffer_.AddChar(base64_digits[triplet >> 18]);
66 buffer_.AddChar(base64_digits[(triplet >> 12) & 63]);
67 buffer_.AddChar(base64_digits[(triplet >> 6) & 63]);
68 buffer_.AddChar(base64_pad);
69 }
70}
void AddChar(char ch)
#define ASSERT(E)
size_t length
static const char base64_pad
static const char base64_digits[65]

◆ AppendSerializedObject() [1/2]

void dart::JSONWriter::AppendSerializedObject ( const char *  property_name,
const char *  serialized_object 
)

Definition at line 77 of file json_writer.cc.

78 {
80 PrintPropertyName(property_name);
81 buffer_.AddString(serialized_object);
82}
void AddString(const char *s)
void PrintCommaIfNeeded()
void PrintPropertyName(const char *name)

◆ AppendSerializedObject() [2/2]

void dart::JSONWriter::AppendSerializedObject ( const char *  serialized_object)

Definition at line 72 of file json_writer.cc.

72 {
74 buffer_.AddString(serialized_object);
75}

◆ buffer()

TextBuffer * dart::JSONWriter::buffer ( )
inline

Definition at line 19 of file json_writer.h.

19{ return &buffer_; }

◆ Clear()

void dart::JSONWriter::Clear ( )

Definition at line 84 of file json_writer.cc.

84 {
85 buffer_.Clear();
86 open_objects_ = 0;
87}

◆ CloseArray()

void dart::JSONWriter::CloseArray ( )

Definition at line 121 of file json_writer.cc.

121 {
122 ASSERT(open_objects_ > 0);
123 open_objects_--;
124 buffer_.AddChar(']');
125}

◆ CloseObject()

void dart::JSONWriter::CloseObject ( )

Definition at line 106 of file json_writer.cc.

106 {
107 ASSERT(open_objects_ > 0);
108 open_objects_--;
109 buffer_.AddChar('}');
110}

◆ OpenArray()

void dart::JSONWriter::OpenArray ( const char *  property_name = nullptr)

Definition at line 112 of file json_writer.cc.

112 {
114 if (property_name != nullptr) {
115 PrintPropertyName(property_name);
116 }
117 open_objects_++;
118 buffer_.AddChar('[');
119}

◆ OpenObject()

void dart::JSONWriter::OpenObject ( const char *  property_name = nullptr)

Definition at line 89 of file json_writer.cc.

89 {
91 open_objects_++;
92 if (property_name != nullptr) {
93 PrintPropertyName(property_name);
94 }
95 buffer_.AddChar('{');
96}

◆ PrintCommaIfNeeded()

void dart::JSONWriter::PrintCommaIfNeeded ( )

Definition at line 320 of file json_writer.cc.

320 {
321 if (NeedComma()) {
322 buffer_.AddChar(',');
323 }
324}

◆ PrintfProperty()

void dart::JSONWriter::PrintfProperty ( const char *  name,
const char *  format,
  ... 
)

Definition at line 269 of file json_writer.cc.

269 {
270 va_list args;
273 va_end(args);
274}
void void VPrintfProperty(const char *name, const char *format, va_list args)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
uint32_t uint32_t * format
va_start(args, format)
va_end(args)
const char *const name

◆ PrintfValue()

void dart::JSONWriter::PrintfValue ( const char *  format,
  ... 
)

Definition at line 195 of file json_writer.cc.

195 {
196 va_list args;
199 va_end(args);
200}
void void VPrintfValue(const char *format, va_list args)

◆ PrintNewline()

void dart::JSONWriter::PrintNewline ( )

Definition at line 316 of file json_writer.cc.

316 {
317 buffer_.AddChar('\n');
318}

◆ PrintProperty() [1/3]

void dart::JSONWriter::PrintProperty ( const char *  name,
const char *  s 
)

Definition at line 244 of file json_writer.cc.

244 {
246 PrintValue(s);
247}
void PrintValue(intptr_t i)

◆ PrintProperty() [2/3]

void dart::JSONWriter::PrintProperty ( const char *  name,
double  d 
)

Definition at line 239 of file json_writer.cc.

239 {
241 PrintValue(d);
242}
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19

◆ PrintProperty() [3/3]

void dart::JSONWriter::PrintProperty ( const char *  name,
intptr_t  i 
)

Definition at line 229 of file json_writer.cc.

229 {
231 PrintValue(i);
232}

◆ PrintProperty64()

void dart::JSONWriter::PrintProperty64 ( const char *  name,
int64_t  i 
)

Definition at line 234 of file json_writer.cc.

234 {
236 PrintValue64(i);
237}
void PrintValue64(int64_t i)

◆ PrintPropertyBase64()

void dart::JSONWriter::PrintPropertyBase64 ( const char *  name,
const uint8_t *  bytes,
intptr_t  length 
)

Definition at line 249 of file json_writer.cc.

251 {
253 PrintValueBase64(b, len);
254}
void PrintValueBase64(const uint8_t *bytes, intptr_t length)
static bool b

◆ PrintPropertyBool()

void dart::JSONWriter::PrintPropertyBool ( const char *  name,
bool  b 
)

Definition at line 224 of file json_writer.cc.

224 {
227}
void PrintValueBool(bool b)

◆ PrintPropertyName()

void dart::JSONWriter::PrintPropertyName ( const char *  name)

Definition at line 307 of file json_writer.cc.

307 {
308 ASSERT(name != nullptr);
310 buffer_.AddChar('"');
312 buffer_.AddChar('"');
313 buffer_.AddChar(':');
314}

◆ PrintPropertyNoEscape()

void dart::JSONWriter::PrintPropertyNoEscape ( const char *  name,
const char *  s 
)

Definition at line 264 of file json_writer.cc.

264 {
267}
void PrintValueNoEscape(const char *s)

◆ PrintPropertyStr()

bool dart::JSONWriter::PrintPropertyStr ( const char *  name,
const String s,
intptr_t  offset = 0,
intptr_t  count = -1 
)

Definition at line 256 of file json_writer.cc.

259 {
261 return PrintValueStr(s, offset, count);
262}
int count
bool PrintValueStr(const String &s, intptr_t offset, intptr_t count)
Point offset

◆ PrintValue() [1/4]

void dart::JSONWriter::PrintValue ( const char *  s)

Definition at line 166 of file json_writer.cc.

166 {
168 buffer_.AddChar('"');
170 buffer_.AddChar('"');
171}

◆ PrintValue() [2/4]

void dart::JSONWriter::PrintValue ( const char *  s,
intptr_t  len 
)

Definition at line 173 of file json_writer.cc.

173 {
175 buffer_.AddChar('"');
177 buffer_.AddChar('"');
178}

◆ PrintValue() [3/4]

void dart::JSONWriter::PrintValue ( double  d)

Definition at line 149 of file json_writer.cc.

149 {
150 // Max length of a double in characters (including \0).
151 // See double_conversion.cc.
152 const size_t kBufferLen = 25;
153 char buffer[kBufferLen];
154 DoubleToCString(d, buffer, kBufferLen);
156 buffer_.Printf("%s", buffer);
157}
intptr_t Printf(const char *format,...) PRINTF_ATTRIBUTE(2
void DoubleToCString(double d, char *buffer, int buffer_size)

◆ PrintValue() [4/4]

void dart::JSONWriter::PrintValue ( intptr_t  i)

Definition at line 137 of file json_writer.cc.

137 {
138 EnsureIntegerIsRepresentableInJavaScript(static_cast<int64_t>(i));
140 buffer_.Printf("%" Pd "", i);
141}
#define Pd
Definition globals.h:408

◆ PrintValue64()

void dart::JSONWriter::PrintValue64 ( int64_t  i)

Definition at line 143 of file json_writer.cc.

143 {
144 EnsureIntegerIsRepresentableInJavaScript(i);
146 buffer_.Printf("%" Pd64 "", i);
147}
#define Pd64
Definition globals.h:416

◆ PrintValueBase64()

void dart::JSONWriter::PrintValueBase64 ( const uint8_t *  bytes,
intptr_t  length 
)

Definition at line 159 of file json_writer.cc.

159 {
161 buffer_.AddChar('"');
163 buffer_.AddChar('"');
164}
void AppendBytesInBase64(const uint8_t *bytes, intptr_t length)

◆ PrintValueBool()

void dart::JSONWriter::PrintValueBool ( bool  b)

Definition at line 132 of file json_writer.cc.

132 {
134 buffer_.Printf("%s", b ? "true" : "false");
135}

◆ PrintValueNoEscape()

void dart::JSONWriter::PrintValueNoEscape ( const char *  s)

Definition at line 190 of file json_writer.cc.

190 {
192 buffer_.Printf("%s", s);
193}

◆ PrintValueNull()

void dart::JSONWriter::PrintValueNull ( )

Definition at line 127 of file json_writer.cc.

127 {
129 buffer_.Printf("null");
130}

◆ PrintValueStr()

bool dart::JSONWriter::PrintValueStr ( const String s,
intptr_t  offset,
intptr_t  count 
)

Definition at line 180 of file json_writer.cc.

182 {
184 buffer_.AddChar('"');
185 bool did_truncate = AddDartString(s, offset, count);
186 buffer_.AddChar('"');
187 return did_truncate;
188}

◆ Steal()

void dart::JSONWriter::Steal ( char **  buffer,
intptr_t *  buffer_length 
)

Definition at line 300 of file json_writer.cc.

300 {
301 ASSERT(buffer != nullptr);
302 ASSERT(buffer_length != nullptr);
303 *buffer_length = buffer_.length();
304 *buffer = buffer_.Steal();
305}
intptr_t length() const
Definition text_buffer.h:36

◆ ToCString()

const char * dart::JSONWriter::ToCString ( )
inline

Definition at line 20 of file json_writer.h.

20{ return buffer_.buffer(); }
char * buffer() const
Definition text_buffer.h:35

◆ UncloseObject()

void dart::JSONWriter::UncloseObject ( )

Definition at line 98 of file json_writer.cc.

98 {
99 intptr_t len = buffer_.length();
100 ASSERT(len > 0);
101 ASSERT(buffer_.buffer()[len - 1] == '}');
102 open_objects_++;
103 buffer_.set_length(len - 1);
104}
void set_length(intptr_t len)
Definition text_buffer.h:67

◆ VPrintfProperty()

void dart::JSONWriter::VPrintfProperty ( const char *  name,
const char *  format,
va_list  args 
)

Definition at line 276 of file json_writer.cc.

278 {
280
281 va_list measure_args;
282 va_copy(measure_args, args);
283 intptr_t len = Utils::VSNPrint(nullptr, 0, format, measure_args);
284 va_end(measure_args);
285
286 MaybeOnStackBuffer mosb(len + 1);
287 char* p = mosb.p();
288
289 va_list print_args;
290 va_copy(print_args, args);
291 intptr_t len2 = Utils::VSNPrint(p, len + 1, format, print_args);
292 va_end(print_args);
293 ASSERT(len == len2);
294
295 buffer_.AddChar('"');
296 AddEscapedUTF8String(p, len);
297 buffer_.AddChar('"');
298}
static int static int VSNPrint(char *str, size_t size, const char *format, va_list args)

◆ VPrintfValue()

void dart::JSONWriter::VPrintfValue ( const char *  format,
va_list  args 
)

Definition at line 202 of file json_writer.cc.

202 {
204
205 va_list measure_args;
206 va_copy(measure_args, args);
207 intptr_t len = Utils::VSNPrint(nullptr, 0, format, measure_args);
208 va_end(measure_args);
209
210 MaybeOnStackBuffer mosb(len + 1);
211 char* p = mosb.p();
212
213 va_list print_args;
214 va_copy(print_args, args);
215 intptr_t len2 = Utils::VSNPrint(p, len + 1, format, print_args);
216 va_end(print_args);
217 ASSERT(len == len2);
218
219 buffer_.AddChar('"');
220 AddEscapedUTF8String(p, len);
221 buffer_.AddChar('"');
222}

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