Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: runtime/vm/json_stream.cc

Issue 23875015: - Base JSON stream printing on stack objects. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/json_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "platform/assert.h" 5 #include "platform/assert.h"
6 #include "vm/object.h" 6 #include "vm/object.h"
7 #include "vm/json_stream.h" 7 #include "vm/json_stream.h"
8 8
9 9
10 namespace dart { 10 namespace dart {
11 11
12 JSONStream::JSONStream(TextBuffer* buffer) { 12 JSONStream::JSONStream(intptr_t buf_size) : buffer_(buf_size) {
13 ASSERT(buffer != NULL);
14 buffer_ = buffer;
15 open_objects_ = 0; 13 open_objects_ = 0;
16 arguments_ = NULL; 14 arguments_ = NULL;
17 num_arguments_ = 0; 15 num_arguments_ = 0;
18 option_keys_ = NULL; 16 option_keys_ = NULL;
19 option_values_ = NULL; 17 option_values_ = NULL;
20 num_options_ = 0; 18 num_options_ = 0;
21 } 19 }
22 20
23 21
24 JSONStream::~JSONStream() { 22 JSONStream::~JSONStream() {
25 } 23 }
26 24
27 25
28 void JSONStream::Clear() { 26 void JSONStream::Clear() {
29 buffer_->Clear(); 27 buffer_.Clear();
30 open_objects_ = 0; 28 open_objects_ = 0;
31 } 29 }
32 30
33 31
34 void JSONStream::OpenObject(const char* property_name) { 32 void JSONStream::OpenObject(const char* property_name) {
35 PrintCommaIfNeeded(); 33 PrintCommaIfNeeded();
36 open_objects_++; 34 open_objects_++;
37 if (property_name != NULL) { 35 if (property_name != NULL) {
38 PrintPropertyName(property_name); 36 PrintPropertyName(property_name);
39 } 37 }
40 buffer_->AddChar('{'); 38 buffer_.AddChar('{');
41 } 39 }
42 40
43 41
44 void JSONStream::CloseObject() { 42 void JSONStream::CloseObject() {
45 ASSERT(open_objects_ > 0); 43 ASSERT(open_objects_ > 0);
46 open_objects_--; 44 open_objects_--;
47 buffer_->AddChar('}'); 45 buffer_.AddChar('}');
48 } 46 }
49 47
50 48
51 void JSONStream::OpenArray(const char* property_name) { 49 void JSONStream::OpenArray(const char* property_name) {
52 PrintCommaIfNeeded(); 50 PrintCommaIfNeeded();
53 if (property_name != NULL) { 51 if (property_name != NULL) {
54 PrintPropertyName(property_name); 52 PrintPropertyName(property_name);
55 } 53 }
56 open_objects_++; 54 open_objects_++;
57 buffer_->AddChar('['); 55 buffer_.AddChar('[');
58 } 56 }
59 57
60 58
61 void JSONStream::CloseArray() { 59 void JSONStream::CloseArray() {
62 ASSERT(open_objects_ > 0); 60 ASSERT(open_objects_ > 0);
63 open_objects_--; 61 open_objects_--;
64 buffer_->AddChar(']'); 62 buffer_.AddChar(']');
65 } 63 }
66 64
67 65
68 void JSONStream::PrintValueBool(bool b) { 66 void JSONStream::PrintValueBool(bool b) {
69 PrintCommaIfNeeded(); 67 PrintCommaIfNeeded();
70 buffer_->Printf("%s", b ? "true" : "false"); 68 buffer_.Printf("%s", b ? "true" : "false");
71 } 69 }
72 70
73 71
74 void JSONStream::PrintValue(intptr_t i) { 72 void JSONStream::PrintValue(intptr_t i) {
75 PrintCommaIfNeeded(); 73 PrintCommaIfNeeded();
76 buffer_->Printf("%" Pd "", i); 74 buffer_.Printf("%" Pd "", i);
77 } 75 }
78 76
79 77
80 void JSONStream::PrintValue(double d) { 78 void JSONStream::PrintValue(double d) {
81 PrintCommaIfNeeded(); 79 PrintCommaIfNeeded();
82 buffer_->Printf("%f", d); 80 buffer_.Printf("%f", d);
83 } 81 }
84 82
85 83
86 void JSONStream::PrintValue(const char* s) { 84 void JSONStream::PrintValue(const char* s) {
87 PrintCommaIfNeeded(); 85 PrintCommaIfNeeded();
88 buffer_->AddChar('"'); 86 buffer_.AddChar('"');
89 buffer_->AddEscapedString(s); 87 buffer_.AddEscapedString(s);
90 buffer_->AddChar('"'); 88 buffer_.AddChar('"');
91 } 89 }
92 90
93 91
94 void JSONStream::PrintfValue(const char* format, ...) { 92 void JSONStream::PrintfValue(const char* format, ...) {
95 PrintCommaIfNeeded(); 93 PrintCommaIfNeeded();
96 94
97 va_list args; 95 va_list args;
98 va_start(args, format); 96 va_start(args, format);
99 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 97 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
100 va_end(args); 98 va_end(args);
101 char* p = reinterpret_cast<char*>(malloc(len+1)); 99 char* p = reinterpret_cast<char*>(malloc(len+1));
102 va_start(args, format); 100 va_start(args, format);
103 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); 101 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
104 va_end(args); 102 va_end(args);
105 ASSERT(len == len2); 103 ASSERT(len == len2);
106 buffer_->AddChar('"'); 104 buffer_.AddChar('"');
107 buffer_->AddEscapedString(p); 105 buffer_.AddEscapedString(p);
108 buffer_->AddChar('"'); 106 buffer_.AddChar('"');
109 free(p); 107 free(p);
110 } 108 }
111 109
112 110
113 void JSONStream::PrintValue(const Object& o, bool ref) { 111 void JSONStream::PrintValue(const Object& o, bool ref) {
114 PrintCommaIfNeeded(); 112 PrintCommaIfNeeded();
115 o.PrintToJSONStream(this, ref); 113 o.PrintToJSONStream(this, ref);
116 } 114 }
117 115
118 116
(...skipping 25 matching lines...) Expand all
144 PrintPropertyName(name); 142 PrintPropertyName(name);
145 va_list args; 143 va_list args;
146 va_start(args, format); 144 va_start(args, format);
147 intptr_t len = OS::VSNPrint(NULL, 0, format, args); 145 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
148 va_end(args); 146 va_end(args);
149 char* p = reinterpret_cast<char*>(malloc(len+1)); 147 char* p = reinterpret_cast<char*>(malloc(len+1));
150 va_start(args, format); 148 va_start(args, format);
151 intptr_t len2 = OS::VSNPrint(p, len+1, format, args); 149 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
152 va_end(args); 150 va_end(args);
153 ASSERT(len == len2); 151 ASSERT(len == len2);
154 buffer_->AddChar('"'); 152 buffer_.AddChar('"');
155 buffer_->AddEscapedString(p); 153 buffer_.AddEscapedString(p);
156 buffer_->AddChar('"'); 154 buffer_.AddChar('"');
157 free(p); 155 free(p);
158 } 156 }
159 157
160 158
161 void JSONStream::SetArguments(const char** arguments, intptr_t num_arguments) { 159 void JSONStream::SetArguments(const char** arguments, intptr_t num_arguments) {
162 arguments_ = arguments; 160 arguments_ = arguments;
163 num_arguments_ = num_arguments; 161 num_arguments_ = num_arguments;
164 } 162 }
165 163
166 164
167 void JSONStream::SetOptions(const char** option_keys, 165 void JSONStream::SetOptions(const char** option_keys,
168 const char** option_values, 166 const char** option_values,
169 intptr_t num_options) { 167 intptr_t num_options) {
170 option_keys_ = option_keys; 168 option_keys_ = option_keys;
171 option_values_ = option_values; 169 option_values_ = option_values;
172 num_options_ = num_options; 170 num_options_ = num_options;
173 } 171 }
174 172
175 173
176 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { 174 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) {
177 PrintPropertyName(name); 175 PrintPropertyName(name);
178 PrintValue(o, ref); 176 PrintValue(o, ref);
179 } 177 }
180 178
181 179
182 void JSONStream::PrintPropertyName(const char* name) { 180 void JSONStream::PrintPropertyName(const char* name) {
183 ASSERT(name != NULL); 181 ASSERT(name != NULL);
184 PrintCommaIfNeeded(); 182 PrintCommaIfNeeded();
185 buffer_->AddChar('"'); 183 buffer_.AddChar('"');
186 buffer_->AddEscapedString(name); 184 buffer_.AddEscapedString(name);
187 buffer_->AddChar('"'); 185 buffer_.AddChar('"');
188 buffer_->AddChar(':'); 186 buffer_.AddChar(':');
189 } 187 }
190 188
191 189
192 void JSONStream::PrintCommaIfNeeded() { 190 void JSONStream::PrintCommaIfNeeded() {
193 if (NeedComma()) { 191 if (NeedComma()) {
194 buffer_->AddChar(','); 192 buffer_.AddChar(',');
195 } 193 }
196 } 194 }
197 195
198 196
199 bool JSONStream::NeedComma() { 197 bool JSONStream::NeedComma() {
200 const char* buffer = buffer_->buf(); 198 const char* buffer = buffer_.buf();
201 intptr_t length = buffer_->length(); 199 intptr_t length = buffer_.length();
202 if (length == 0) { 200 if (length == 0) {
203 return false; 201 return false;
204 } 202 }
205 char ch = buffer[length-1]; 203 char ch = buffer[length-1];
206 return ch != '[' && ch != '{' && ch != ':' && ch != ','; 204 return (ch != '[') && (ch != '{') && (ch != ':') && (ch != ',');
205 }
206
207
208 JSONObject::JSONObject(const JSONArray& arr) : stream_(arr.stream_) {
209 stream_->OpenObject();
210 }
211
212
213 void JSONObject::AddPropertyF(const char* name,
214 const char* format, ...) const {
215 stream_->PrintPropertyName(name);
216 va_list args;
217 va_start(args, format);
218 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
219 va_end(args);
220 char* p = reinterpret_cast<char*>(malloc(len+1));
221 va_start(args, format);
222 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
223 va_end(args);
224 ASSERT(len == len2);
225 stream_->buffer_.AddChar('"');
226 stream_->buffer_.AddEscapedString(p);
227 stream_->buffer_.AddChar('"');
228 free(p);
229 }
230
231
232 void JSONArray::AddValueF(const char* format, ...) const {
233 stream_->PrintCommaIfNeeded();
234 va_list args;
235 va_start(args, format);
236 intptr_t len = OS::VSNPrint(NULL, 0, format, args);
237 va_end(args);
238 char* p = reinterpret_cast<char*>(malloc(len+1));
239 va_start(args, format);
240 intptr_t len2 = OS::VSNPrint(p, len+1, format, args);
241 va_end(args);
242 ASSERT(len == len2);
243 stream_->buffer_.AddChar('"');
244 stream_->buffer_.AddEscapedString(p);
245 stream_->buffer_.AddChar('"');
246 free(p);
207 } 247 }
208 248
209 } // namespace dart 249 } // namespace dart
210 250
OLDNEW
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/json_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698