| 1 | // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | #ifndef RUNTIME_VM_JSON_WRITER_H_ |
| 6 | #define RUNTIME_VM_JSON_WRITER_H_ |
| 7 | |
| 8 | #include "platform/allocation.h" |
| 9 | #include "platform/text_buffer.h" |
| 10 | |
| 11 | namespace dart { |
| 12 | |
| 13 | class String; |
| 14 | |
| 15 | class JSONWriter : ValueObject { |
| 16 | public: |
| 17 | explicit JSONWriter(intptr_t buf_size = 256); |
| 18 | |
| 19 | TextBuffer* buffer() { return &buffer_; } |
| 20 | const char* ToCString() { return buffer_.buffer(); } |
| 21 | |
| 22 | void Steal(char** buffer, intptr_t* buffer_length); |
| 23 | |
| 24 | void PrintCommaIfNeeded(); |
| 25 | |
| 26 | // Append |serialized_object| to the stream. |
| 27 | void AppendSerializedObject(const char* serialized_object); |
| 28 | |
| 29 | // Append |buffer| to the stream. |
| 30 | void AppendSerializedObject(const uint8_t* buffer, intptr_t buffer_length); |
| 31 | |
| 32 | // Append |serialized_object| to the stream with |property_name|. |
| 33 | void AppendSerializedObject(const char* property_name, |
| 34 | const char* serialized_object); |
| 35 | |
| 36 | void OpenObject(const char* property_name = NULL); |
| 37 | void CloseObject(); |
| 38 | void UncloseObject(); |
| 39 | |
| 40 | void OpenArray(const char* property_name = NULL); |
| 41 | void CloseArray(); |
| 42 | |
| 43 | void Clear(); |
| 44 | |
| 45 | void PrintValueNull(); |
| 46 | void PrintValueBool(bool b); |
| 47 | void PrintValue(intptr_t i); |
| 48 | void PrintValue64(int64_t i); |
| 49 | void PrintValue(double d); |
| 50 | void PrintValueBase64(const uint8_t* bytes, intptr_t length); |
| 51 | void PrintValue(const char* s); |
| 52 | void PrintValue(const char* s, intptr_t len); |
| 53 | void PrintValueNoEscape(const char* s); |
| 54 | void PrintfValue(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
| 55 | void VPrintfValue(const char* format, va_list args); |
| 56 | bool PrintValueStr(const String& s, intptr_t offset, intptr_t count); |
| 57 | |
| 58 | void PrintPropertyBool(const char* name, bool b); |
| 59 | void PrintProperty(const char* name, intptr_t i); |
| 60 | void PrintProperty64(const char* name, int64_t i); |
| 61 | void PrintProperty(const char* name, double d); |
| 62 | void PrintPropertyBase64(const char* name, |
| 63 | const uint8_t* bytes, |
| 64 | intptr_t length); |
| 65 | void PrintProperty(const char* name, const char* s); |
| 66 | bool PrintPropertyStr(const char* name, |
| 67 | const String& s, |
| 68 | intptr_t offset = 0, |
| 69 | intptr_t count = -1); |
| 70 | void PrintPropertyNoEscape(const char* name, const char* s); |
| 71 | void PrintfProperty(const char* name, const char* format, ...) |
| 72 | PRINTF_ATTRIBUTE(3, 4); |
| 73 | void VPrintfProperty(const char* name, const char* format, va_list args); |
| 74 | |
| 75 | void PrintPropertyName(const char* name); |
| 76 | |
| 77 | void PrintNewline(); |
| 78 | |
| 79 | void AddEscapedUTF8String(const char* s); |
| 80 | void AddEscapedUTF8String(const char* s, intptr_t len); |
| 81 | |
| 82 | private: |
| 83 | bool NeedComma(); |
| 84 | bool AddDartString(const String& s, intptr_t offset, intptr_t count); |
| 85 | |
| 86 | // Debug only fatal assertion. |
| 87 | static void EnsureIntegerIsRepresentableInJavaScript(int64_t i); |
| 88 | |
| 89 | intptr_t open_objects_; |
| 90 | TextBuffer buffer_; |
| 91 | }; |
| 92 | |
| 93 | } // namespace dart |
| 94 | |
| 95 | #endif // RUNTIME_VM_JSON_WRITER_H_ |
| 96 | |