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

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

Issue 18168003: JSONStream and tests (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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
(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;
15 }
16
17
18 JSONStream::~JSONStream() {
19 }
20
21
22 void JSONStream::Clear() {
23 buffer_->Clear();
24 open_objects_ = 0;
25 }
26
27
28 void JSONStream::OpenObject(const char* property_name) {
29 PrintCommaIfNeeded();
30 open_objects_++;
31 if (property_name != NULL) {
32 PrintPropertyName(property_name);
33 }
34 buffer_->AddChar('{');
35 }
36
37
38 void JSONStream::CloseObject() {
39 ASSERT(open_objects_ > 0);
40 open_objects_--;
41 buffer_->AddChar('}');
42 }
43
44
45 void JSONStream::OpenArray(const char* property_name) {
46 PrintCommaIfNeeded();
47 if (property_name != NULL) {
48 PrintPropertyName(property_name);
49 }
50 open_objects_++;
51 buffer_->AddChar('[');
52 }
53
54
55 void JSONStream::CloseArray() {
56 ASSERT(open_objects_ > 0);
57 open_objects_--;
58 buffer_->AddChar(']');
59 }
60
61
62 void JSONStream::PrintValueBool(bool b) {
63 PrintCommaIfNeeded();
64 buffer_->Printf("%s", b ? "true" : "false");
65 }
66
67
68 void JSONStream::PrintValue(intptr_t i) {
69 PrintCommaIfNeeded();
70 buffer_->Printf("%"Pd"", i);
71 }
72
73
74 void JSONStream::PrintValue(double d) {
75 PrintCommaIfNeeded();
76 buffer_->Printf("%f", d);
77 }
78
79
80 void JSONStream::PrintValue(const char* s) {
81 PrintCommaIfNeeded();
82 buffer_->Printf("\"%s\"", s);
83 }
84
85
86 void JSONStream::PrintPropertyBool(const char* name, bool b) {
87 PrintPropertyName(name);
88 PrintValueBool(b);
89 }
90
91
92 void JSONStream::PrintProperty(const char* name, intptr_t i) {
93 PrintPropertyName(name);
94 PrintValue(i);
95 }
96
97
98 void JSONStream::PrintProperty(const char* name, double d) {
99 PrintPropertyName(name);
100 PrintValue(d);
101 }
102
103
104 void JSONStream::PrintProperty(const char* name, const char* s) {
105 PrintPropertyName(name);
106 PrintValue(s);
107 }
108
109
110 void JSONStream::PrintPropertyName(const char* name) {
111 ASSERT(name != NULL);
112 PrintCommaIfNeeded();
113 buffer_->Printf("\"%s\":", name);
114 }
115
116
117 void JSONStream::PrintCommaIfNeeded() {
118 if (NeedComma()) {
119 buffer_->AddChar(',');
120 }
121 }
122
123
124 bool JSONStream::NeedComma() {
125 const char* buffer = buffer_->buf();
126 intptr_t length = buffer_->length();
127 if (length == 0) {
128 return false;
129 }
130 char ch = buffer[length-1];
131 return ch != '[' && ch != '{' && ch != ':' && ch != ',';
132 }
133
134 } // namespace dart
135
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