b7608cc5a4
R=iposva@google.com Review URL: https://codereview.chromium.org//18168003 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@24697 260f80e4-7a28-3924-810f-c04153c831b5
136 lines
2.5 KiB
C++
136 lines
2.5 KiB
C++
// 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
|
|
|