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

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
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..59a8524f85fddd2d0f7dc264b9f6df301f80ef42
--- /dev/null
+++ b/runtime/vm/json_stream.cc
@@ -0,0 +1,126 @@
+// 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;
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
+}
+
+
+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
+}
+
+
+void JSONStream::Clear() {
+ buffer_->Clear();
+ open_objects_ = 0;
+}
+
Ivan Posva 2013/07/01 23:18:14 2 lines.
Cutch 2013/07/01 23:33:55 Done.
+void JSONStream::OpenObject() {
+ open_objects_++;
+ buffer_->AddChar('{');
+}
+
+
+void JSONStream::CloseObject() {
+ ASSERT(open_objects_ > 0);
+ open_objects_--;
+ buffer_->AddChar('}');
+}
+
+
+void JSONStream::OpenArray() {
+ 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");
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
+}
+
+
+void JSONStream::PrintValue(int i) {
+ PrintCommaIfNeeded();
+ buffer_->Printf("%d", 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, int 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 != ':';
+}
+
+} // namespace dart
+

Powered by Google App Engine
This is Rietveld 408576698