]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/document/json_document_builder.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
10 #include "rocksdb/utilities/json_document.h"
11 #include "third-party/fbson/FbsonWriter.h"
14 JSONDocumentBuilder::JSONDocumentBuilder()
15 : writer_(new fbson::FbsonWriter()) {
18 JSONDocumentBuilder::JSONDocumentBuilder(fbson::FbsonOutStream
* out
)
19 : writer_(new fbson::FbsonWriter(*out
)) {
22 void JSONDocumentBuilder::Reset() {
26 bool JSONDocumentBuilder::WriteStartArray() {
27 return writer_
->writeStartArray();
30 bool JSONDocumentBuilder::WriteEndArray() {
31 return writer_
->writeEndArray();
34 bool JSONDocumentBuilder::WriteStartObject() {
35 return writer_
->writeStartObject();
38 bool JSONDocumentBuilder::WriteEndObject() {
39 return writer_
->writeEndObject();
42 bool JSONDocumentBuilder::WriteKeyValue(const std::string
& key
,
43 const JSONDocument
& value
) {
44 assert(key
.size() <= std::numeric_limits
<uint8_t>::max());
45 size_t bytesWritten
= writer_
->writeKey(key
.c_str(),
46 static_cast<uint8_t>(key
.size()));
47 if (bytesWritten
== 0) {
50 return WriteJSONDocument(value
);
53 bool JSONDocumentBuilder::WriteJSONDocument(const JSONDocument
& value
) {
54 switch (value
.type()) {
55 case JSONDocument::kNull
:
56 return writer_
->writeNull() != 0;
57 case JSONDocument::kInt64
:
58 return writer_
->writeInt64(value
.GetInt64());
59 case JSONDocument::kDouble
:
60 return writer_
->writeDouble(value
.GetDouble());
61 case JSONDocument::kBool
:
62 return writer_
->writeBool(value
.GetBool());
63 case JSONDocument::kString
:
65 bool res
= writer_
->writeStartString();
69 const std::string
& str
= value
.GetString();
70 res
= writer_
->writeString(str
.c_str(),
71 static_cast<uint32_t>(str
.size()));
75 return writer_
->writeEndString();
77 case JSONDocument::kArray
:
79 bool res
= WriteStartArray();
83 for (size_t i
= 0; i
< value
.Count(); ++i
) {
84 res
= WriteJSONDocument(value
[i
]);
89 return WriteEndArray();
91 case JSONDocument::kObject
:
93 bool res
= WriteStartObject();
97 for (auto keyValue
: value
.Items()) {
98 WriteKeyValue(keyValue
.first
, keyValue
.second
);
100 return WriteEndObject();
108 JSONDocument
JSONDocumentBuilder::GetJSONDocument() {
109 fbson::FbsonValue
* value
=
110 fbson::FbsonDocument::createValue(writer_
->getOutput()->getBuffer(),
111 static_cast<uint32_t>(writer_
->getOutput()->getSize()));
112 return JSONDocument(value
, true);
115 JSONDocumentBuilder::~JSONDocumentBuilder() {
118 } // namespace rocksdb
120 #endif // ROCKSDB_LITE