OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_JSON_STREAM_H_ | 5 #ifndef VM_JSON_STREAM_H_ |
6 #define VM_JSON_STREAM_H_ | 6 #define VM_JSON_STREAM_H_ |
7 | 7 |
8 #include "platform/json.h" | 8 #include "platform/json.h" |
| 9 #include "vm/allocation.h" |
9 | 10 |
10 namespace dart { | 11 namespace dart { |
11 | 12 |
12 class Object; | 13 class Object; |
| 14 class JSONObject; |
| 15 class JSONArray; |
13 | 16 |
14 class JSONStream : ValueObject { | 17 class JSONStream : ValueObject { |
15 public: | 18 public: |
16 explicit JSONStream(TextBuffer* buffer); | 19 explicit JSONStream(intptr_t buf_size = 256); |
17 ~JSONStream(); | 20 ~JSONStream(); |
18 | 21 |
| 22 const char* ToCString() { return buffer_.buf(); } |
| 23 |
| 24 void SetArguments(const char** arguments, intptr_t num_arguments); |
| 25 void SetOptions(const char** option_keys, const char** option_values, |
| 26 intptr_t num_options); |
| 27 |
| 28 intptr_t num_arguments() const { return num_arguments_; } |
| 29 const char* GetArgument(intptr_t i) const { |
| 30 return arguments_[i]; |
| 31 } |
| 32 intptr_t num_options() const { return num_options_; } |
| 33 const char* GetOptionKey(intptr_t i) const { |
| 34 return option_keys_[i]; |
| 35 } |
| 36 const char* GetOptionValue(intptr_t i) const { |
| 37 return option_values_[i]; |
| 38 } |
| 39 |
| 40 private: |
19 void Clear(); | 41 void Clear(); |
20 | 42 |
21 void OpenObject(const char* property_name = NULL); | 43 void OpenObject(const char* property_name = NULL); |
22 void CloseObject(); | 44 void CloseObject(); |
23 | 45 |
24 void OpenArray(const char* property_name = NULL); | 46 void OpenArray(const char* property_name = NULL); |
25 void CloseArray(); | 47 void CloseArray(); |
26 | 48 |
27 void PrintValueBool(bool b); | 49 void PrintValueBool(bool b); |
28 void PrintValue(intptr_t i); | 50 void PrintValue(intptr_t i); |
29 void PrintValue(double d); | 51 void PrintValue(double d); |
30 void PrintValue(const char* s); | 52 void PrintValue(const char* s); |
31 void PrintfValue(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); | 53 void PrintfValue(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
32 void PrintValue(const Object& o, bool ref = true); | 54 void PrintValue(const Object& o, bool ref = true); |
33 | 55 |
34 void PrintPropertyBool(const char* name, bool b); | 56 void PrintPropertyBool(const char* name, bool b); |
35 void PrintProperty(const char* name, intptr_t i); | 57 void PrintProperty(const char* name, intptr_t i); |
36 void PrintProperty(const char* name, double d); | 58 void PrintProperty(const char* name, double d); |
37 void PrintProperty(const char* name, const char* s); | 59 void PrintProperty(const char* name, const char* s); |
38 void PrintfProperty(const char* name, const char* format, ...) | 60 void PrintfProperty(const char* name, const char* format, ...) |
39 PRINTF_ATTRIBUTE(3, 4); | 61 PRINTF_ATTRIBUTE(3, 4); |
40 void PrintProperty(const char* name, const Object& o, bool ref = true); | 62 void PrintProperty(const char* name, const Object& o, bool ref = true); |
41 | 63 |
42 void SetArguments(const char** arguments, intptr_t num_arguments); | |
43 void SetOptions(const char** option_keys, const char** option_values, | |
44 intptr_t num_options); | |
45 | |
46 intptr_t num_arguments() const { return num_arguments_; } | |
47 const char* GetArgument(intptr_t i) const { | |
48 return arguments_[i]; | |
49 } | |
50 intptr_t num_options() const { return num_options_; } | |
51 const char* GetOptionKey(intptr_t i) const { | |
52 return option_keys_[i]; | |
53 } | |
54 const char* GetOptionValue(intptr_t i) const { | |
55 return option_values_[i]; | |
56 } | |
57 | |
58 private: | |
59 void PrintPropertyName(const char* name); | 64 void PrintPropertyName(const char* name); |
60 void PrintCommaIfNeeded(); | 65 void PrintCommaIfNeeded(); |
61 bool NeedComma(); | 66 bool NeedComma(); |
| 67 |
| 68 intptr_t nesting_level() const { return open_objects_; } |
| 69 |
62 intptr_t open_objects_; | 70 intptr_t open_objects_; |
63 TextBuffer* buffer_; | 71 TextBuffer buffer_; |
64 const char** arguments_; | 72 const char** arguments_; |
65 intptr_t num_arguments_; | 73 intptr_t num_arguments_; |
66 const char** option_keys_; | 74 const char** option_keys_; |
67 const char** option_values_; | 75 const char** option_values_; |
68 intptr_t num_options_; | 76 intptr_t num_options_; |
| 77 |
| 78 friend class JSONObject; |
| 79 friend class JSONArray; |
| 80 }; |
| 81 |
| 82 |
| 83 class JSONObject : public ValueObject { |
| 84 public: |
| 85 explicit JSONObject(JSONStream* stream) : stream_(stream) { |
| 86 stream_->OpenObject(); |
| 87 } |
| 88 JSONObject(const JSONObject& obj, const char* name) : stream_(obj.stream_) { |
| 89 stream_->OpenObject(name); |
| 90 } |
| 91 explicit JSONObject(const JSONArray& arr); |
| 92 |
| 93 ~JSONObject() { |
| 94 stream_->CloseObject(); |
| 95 } |
| 96 |
| 97 void AddProperty(const char* name, bool b) const { |
| 98 stream_->PrintPropertyBool(name, b); |
| 99 } |
| 100 void AddProperty(const char* name, intptr_t i) const { |
| 101 stream_->PrintProperty(name, i); |
| 102 } |
| 103 void AddProperty(const char* name, double d) const { |
| 104 stream_->PrintProperty(name, d); |
| 105 } |
| 106 void AddProperty(const char* name, const Object& obj, bool ref = true) const { |
| 107 stream_->PrintProperty(name, obj, ref); |
| 108 } |
| 109 void AddPropertyF(const char* name, const char* format, ...) const |
| 110 PRINTF_ATTRIBUTE(3, 4); |
| 111 |
| 112 private: |
| 113 JSONStream* stream_; |
| 114 |
| 115 friend class JSONArray; |
| 116 }; |
| 117 |
| 118 |
| 119 class JSONArray : public ValueObject { |
| 120 public: |
| 121 explicit JSONArray(JSONStream* stream) : stream_(stream) { |
| 122 stream_->OpenArray(); |
| 123 } |
| 124 JSONArray(const JSONObject& obj, const char* name) : stream_(obj.stream_) { |
| 125 stream_->OpenArray(name); |
| 126 } |
| 127 JSONArray(const JSONArray& arr) : stream_(arr.stream_) { |
| 128 stream_->OpenArray(); |
| 129 } |
| 130 ~JSONArray() { |
| 131 stream_->CloseArray(); |
| 132 } |
| 133 |
| 134 void AddValue(bool b) const { stream_->PrintValueBool(b); } |
| 135 void AddValue(intptr_t i) const { stream_->PrintValue(i); } |
| 136 void AddValue(double d) const { stream_->PrintValue(d); } |
| 137 void AddValue(const char* s) const { stream_->PrintValue(s); } |
| 138 void AddValue(const Object& obj, bool ref = true) const { |
| 139 stream_->PrintValue(obj, ref); |
| 140 } |
| 141 void AddValueF(const char* format, ...) const PRINTF_ATTRIBUTE(2, 3); |
| 142 |
| 143 private: |
| 144 JSONStream* stream_; |
| 145 |
| 146 friend class JSONObject; |
69 }; | 147 }; |
70 | 148 |
71 } // namespace dart | 149 } // namespace dart |
72 | 150 |
73 #endif // VM_JSON_STREAM_H_ | 151 #endif // VM_JSON_STREAM_H_ |
OLD | NEW |