| OLD | NEW |
| 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/json_stream.h" | 7 #include "vm/json_stream.h" |
| 7 | 8 |
| 8 | 9 |
| 9 namespace dart { | 10 namespace dart { |
| 10 | 11 |
| 11 JSONStream::JSONStream(TextBuffer* buffer) { | 12 JSONStream::JSONStream(TextBuffer* buffer) { |
| 12 ASSERT(buffer != NULL); | 13 ASSERT(buffer != NULL); |
| 13 buffer_ = buffer; | 14 buffer_ = buffer; |
| 14 open_objects_ = 0; | 15 open_objects_ = 0; |
| 15 } | 16 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 buffer_->Printf("%f", d); | 77 buffer_->Printf("%f", d); |
| 77 } | 78 } |
| 78 | 79 |
| 79 | 80 |
| 80 void JSONStream::PrintValue(const char* s) { | 81 void JSONStream::PrintValue(const char* s) { |
| 81 PrintCommaIfNeeded(); | 82 PrintCommaIfNeeded(); |
| 82 buffer_->Printf("\"%s\"", s); | 83 buffer_->Printf("\"%s\"", s); |
| 83 } | 84 } |
| 84 | 85 |
| 85 | 86 |
| 87 void JSONStream::PrintValue(const Object& o, bool ref) { |
| 88 PrintCommaIfNeeded(); |
| 89 o.PrintToJSONStream(this, ref); |
| 90 } |
| 91 |
| 92 |
| 86 void JSONStream::PrintPropertyBool(const char* name, bool b) { | 93 void JSONStream::PrintPropertyBool(const char* name, bool b) { |
| 87 PrintPropertyName(name); | 94 PrintPropertyName(name); |
| 88 PrintValueBool(b); | 95 PrintValueBool(b); |
| 89 } | 96 } |
| 90 | 97 |
| 91 | 98 |
| 92 void JSONStream::PrintProperty(const char* name, intptr_t i) { | 99 void JSONStream::PrintProperty(const char* name, intptr_t i) { |
| 93 PrintPropertyName(name); | 100 PrintPropertyName(name); |
| 94 PrintValue(i); | 101 PrintValue(i); |
| 95 } | 102 } |
| 96 | 103 |
| 97 | 104 |
| 98 void JSONStream::PrintProperty(const char* name, double d) { | 105 void JSONStream::PrintProperty(const char* name, double d) { |
| 99 PrintPropertyName(name); | 106 PrintPropertyName(name); |
| 100 PrintValue(d); | 107 PrintValue(d); |
| 101 } | 108 } |
| 102 | 109 |
| 103 | 110 |
| 104 void JSONStream::PrintProperty(const char* name, const char* s) { | 111 void JSONStream::PrintProperty(const char* name, const char* s) { |
| 105 PrintPropertyName(name); | 112 PrintPropertyName(name); |
| 106 PrintValue(s); | 113 PrintValue(s); |
| 107 } | 114 } |
| 108 | 115 |
| 109 | 116 |
| 117 void JSONStream::PrintProperty(const char* name, const Object& o, bool ref) { |
| 118 PrintPropertyName(name); |
| 119 PrintValue(o, ref); |
| 120 } |
| 121 |
| 122 |
| 110 void JSONStream::PrintPropertyName(const char* name) { | 123 void JSONStream::PrintPropertyName(const char* name) { |
| 111 ASSERT(name != NULL); | 124 ASSERT(name != NULL); |
| 112 PrintCommaIfNeeded(); | 125 PrintCommaIfNeeded(); |
| 113 buffer_->Printf("\"%s\":", name); | 126 buffer_->Printf("\"%s\":", name); |
| 114 } | 127 } |
| 115 | 128 |
| 116 | 129 |
| 117 void JSONStream::PrintCommaIfNeeded() { | 130 void JSONStream::PrintCommaIfNeeded() { |
| 118 if (NeedComma()) { | 131 if (NeedComma()) { |
| 119 buffer_->AddChar(','); | 132 buffer_->AddChar(','); |
| 120 } | 133 } |
| 121 } | 134 } |
| 122 | 135 |
| 123 | 136 |
| 124 bool JSONStream::NeedComma() { | 137 bool JSONStream::NeedComma() { |
| 125 const char* buffer = buffer_->buf(); | 138 const char* buffer = buffer_->buf(); |
| 126 intptr_t length = buffer_->length(); | 139 intptr_t length = buffer_->length(); |
| 127 if (length == 0) { | 140 if (length == 0) { |
| 128 return false; | 141 return false; |
| 129 } | 142 } |
| 130 char ch = buffer[length-1]; | 143 char ch = buffer[length-1]; |
| 131 return ch != '[' && ch != '{' && ch != ':' && ch != ','; | 144 return ch != '[' && ch != '{' && ch != ':' && ch != ','; |
| 132 } | 145 } |
| 133 | 146 |
| 134 } // namespace dart | 147 } // namespace dart |
| 135 | 148 |
| OLD | NEW |