OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "platform/assert.h" | |
6 #include "vm/json_stream.h" | |
7 | |
8 | |
9 namespace dart { | |
10 | |
11 JSONStream::JSONStream(TextBuffer* buffer) { | |
12 ASSERT(buffer != NULL); | |
13 buffer_ = buffer; | |
14 open_objects_ = 0; | |
Ivan Posva
2013/07/01 23:18:14
What is open_object_ used for?
Cutch
2013/07/01 23:33:55
Sanity check for calls to OpenObject / CloseObject
| |
15 } | |
16 | |
17 | |
18 JSONStream::~JSONStream() { | |
Ivan Posva
2013/07/01 23:18:14
Who is charged with freeing the TextBuffer?
Cutch
2013/07/01 23:33:55
TextBuffer is a ValueObject so it is cleaned up wh
| |
19 } | |
20 | |
21 | |
22 void JSONStream::Clear() { | |
23 buffer_->Clear(); | |
24 open_objects_ = 0; | |
25 } | |
26 | |
Ivan Posva
2013/07/01 23:18:14
2 lines.
Cutch
2013/07/01 23:33:55
Done.
| |
27 void JSONStream::OpenObject() { | |
28 open_objects_++; | |
29 buffer_->AddChar('{'); | |
30 } | |
31 | |
32 | |
33 void JSONStream::CloseObject() { | |
34 ASSERT(open_objects_ > 0); | |
35 open_objects_--; | |
36 buffer_->AddChar('}'); | |
37 } | |
38 | |
39 | |
40 void JSONStream::OpenArray() { | |
41 open_objects_++; | |
42 buffer_->AddChar('['); | |
43 } | |
44 | |
45 | |
46 void JSONStream::CloseArray() { | |
47 ASSERT(open_objects_ > 0); | |
48 open_objects_--; | |
49 buffer_->AddChar(']'); | |
50 } | |
51 | |
52 | |
53 void JSONStream::PrintValueBool(bool b) { | |
54 PrintCommaIfNeeded(); | |
55 buffer_->Printf("%s", b ? "true" : "false"); | |
Ivan Posva
2013/07/01 23:18:14
How about adding a private PrintBool(bool b) and c
Cutch
2013/07/01 23:33:55
Not sure what this has to do with PrintCommaIfNeed
| |
56 } | |
57 | |
58 | |
59 void JSONStream::PrintValue(int i) { | |
60 PrintCommaIfNeeded(); | |
61 buffer_->Printf("%d", i); | |
62 } | |
63 | |
64 | |
65 void JSONStream::PrintValue(double d) { | |
66 PrintCommaIfNeeded(); | |
67 buffer_->Printf("%f", d); | |
68 } | |
69 | |
70 | |
71 void JSONStream::PrintValue(const char* s) { | |
72 PrintCommaIfNeeded(); | |
73 buffer_->Printf("\"%s\"", s); | |
74 } | |
75 | |
76 | |
77 void JSONStream::PrintPropertyBool(const char* name, bool b) { | |
78 PrintPropertyName(name); | |
79 PrintValueBool(b); | |
80 } | |
81 | |
82 | |
83 void JSONStream::PrintProperty(const char* name, int i) { | |
84 PrintPropertyName(name); | |
85 PrintValue(i); | |
86 } | |
87 | |
88 | |
89 void JSONStream::PrintProperty(const char* name, double d) { | |
90 PrintPropertyName(name); | |
91 PrintValue(d); | |
92 } | |
93 | |
94 | |
95 void JSONStream::PrintProperty(const char* name, const char* s) { | |
96 PrintPropertyName(name); | |
97 PrintValue(s); | |
98 } | |
99 | |
100 | |
101 void JSONStream::PrintPropertyName(const char* name) { | |
102 ASSERT(name != NULL); | |
103 PrintCommaIfNeeded(); | |
104 buffer_->Printf("\"%s\":", name); | |
105 } | |
106 | |
107 | |
108 void JSONStream::PrintCommaIfNeeded() { | |
109 if (NeedComma()) { | |
110 buffer_->AddChar(','); | |
111 } | |
112 } | |
113 | |
114 | |
115 bool JSONStream::NeedComma() { | |
116 const char* buffer = buffer_->buf(); | |
117 intptr_t length = buffer_->length(); | |
118 if (length == 0) { | |
119 return false; | |
120 } | |
121 char ch = buffer[length-1]; | |
122 return ch != '[' && ch != '{' && ch != ':'; | |
123 } | |
124 | |
125 } // namespace dart | |
126 | |
OLD | NEW |