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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/vm/json_stream.h ('k') | runtime/vm/json_test.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/json_stream.cc
diff --git a/runtime/vm/json_stream.cc b/runtime/vm/json_stream.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1749cff98c68412a0e161ab3f72f7674ff6dc3a6
--- /dev/null
+++ b/runtime/vm/json_stream.cc
@@ -0,0 +1,135 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+#include "platform/assert.h"
+#include "vm/json_stream.h"
+
+
+namespace dart {
+
+JSONStream::JSONStream(TextBuffer* buffer) {
+ ASSERT(buffer != NULL);
+ buffer_ = buffer;
+ open_objects_ = 0;
+}
+
+
+JSONStream::~JSONStream() {
+}
+
+
+void JSONStream::Clear() {
+ buffer_->Clear();
+ open_objects_ = 0;
+}
+
+
+void JSONStream::OpenObject(const char* property_name) {
+ PrintCommaIfNeeded();
+ open_objects_++;
+ if (property_name != NULL) {
+ PrintPropertyName(property_name);
+ }
+ buffer_->AddChar('{');
+}
+
+
+void JSONStream::CloseObject() {
+ ASSERT(open_objects_ > 0);
+ open_objects_--;
+ buffer_->AddChar('}');
+}
+
+
+void JSONStream::OpenArray(const char* property_name) {
+ PrintCommaIfNeeded();
+ if (property_name != NULL) {
+ PrintPropertyName(property_name);
+ }
+ open_objects_++;
+ buffer_->AddChar('[');
+}
+
+
+void JSONStream::CloseArray() {
+ ASSERT(open_objects_ > 0);
+ open_objects_--;
+ buffer_->AddChar(']');
+}
+
+
+void JSONStream::PrintValueBool(bool b) {
+ PrintCommaIfNeeded();
+ buffer_->Printf("%s", b ? "true" : "false");
+}
+
+
+void JSONStream::PrintValue(intptr_t i) {
+ PrintCommaIfNeeded();
+ buffer_->Printf("%"Pd"", i);
+}
+
+
+void JSONStream::PrintValue(double d) {
+ PrintCommaIfNeeded();
+ buffer_->Printf("%f", d);
+}
+
+
+void JSONStream::PrintValue(const char* s) {
+ PrintCommaIfNeeded();
+ buffer_->Printf("\"%s\"", s);
+}
+
+
+void JSONStream::PrintPropertyBool(const char* name, bool b) {
+ PrintPropertyName(name);
+ PrintValueBool(b);
+}
+
+
+void JSONStream::PrintProperty(const char* name, intptr_t i) {
+ PrintPropertyName(name);
+ PrintValue(i);
+}
+
+
+void JSONStream::PrintProperty(const char* name, double d) {
+ PrintPropertyName(name);
+ PrintValue(d);
+}
+
+
+void JSONStream::PrintProperty(const char* name, const char* s) {
+ PrintPropertyName(name);
+ PrintValue(s);
+}
+
+
+void JSONStream::PrintPropertyName(const char* name) {
+ ASSERT(name != NULL);
+ PrintCommaIfNeeded();
+ buffer_->Printf("\"%s\":", name);
+}
+
+
+void JSONStream::PrintCommaIfNeeded() {
+ if (NeedComma()) {
+ buffer_->AddChar(',');
+ }
+}
+
+
+bool JSONStream::NeedComma() {
+ const char* buffer = buffer_->buf();
+ intptr_t length = buffer_->length();
+ if (length == 0) {
+ return false;
+ }
+ char ch = buffer[length-1];
+ return ch != '[' && ch != '{' && ch != ':' && ch != ',';
+}
+
+} // namespace dart
+
« 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