/Users/deen/code/yugabyte-db/src/yb/rocksdb/third-party/fbson/FbsonUtil.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2011-present, Facebook, Inc. |
3 | | * All rights reserved. |
4 | | * |
5 | | * This source code is licensed under the BSD-style license found in the |
6 | | * LICENSE file in the root directory of this source tree. An additional grant |
7 | | * of patent rights can be found in the PATENTS file in the same directory. |
8 | | * |
9 | | * The following only applies to changes made to this file as part of YugaByte development. |
10 | | * |
11 | | * Portions Copyright (c) YugaByte, Inc. |
12 | | * |
13 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
14 | | * in compliance with the License. You may obtain a copy of the License at |
15 | | * |
16 | | * http://www.apache.org/licenses/LICENSE-2.0 |
17 | | * |
18 | | * Unless required by applicable law or agreed to in writing, software distributed under the License |
19 | | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
20 | | * or implied. See the License for the specific language governing permissions and limitations |
21 | | * under the License. |
22 | | * |
23 | | * |
24 | | */ |
25 | | |
26 | | /* |
27 | | * This header file defines miscellaneous utility classes. |
28 | | * |
29 | | * @author Tian Xia <tianx@fb.com> |
30 | | */ |
31 | | |
32 | | #ifndef FBSON_FBSONUTIL_H |
33 | | #define FBSON_FBSONUTIL_H |
34 | | |
35 | | #include <sstream> |
36 | | #include "FbsonDocument.h" |
37 | | |
38 | | namespace fbson { |
39 | | |
40 | | #define OUT_BUF_SIZE 1024 |
41 | | |
42 | | /* |
43 | | * FbsonToJson converts an FbsonValue object to a JSON string. |
44 | | */ |
45 | | class FbsonToJson { |
46 | | public: |
47 | 0 | FbsonToJson() : os_(buffer_, OUT_BUF_SIZE) {} |
48 | | |
49 | | // get json string |
50 | 0 | const char* json(const FbsonValue* pval) { |
51 | 0 | os_.clear(); |
52 | 0 | os_.seekp(0); |
53 | |
|
54 | 0 | if (pval) { |
55 | 0 | intern_json(pval); |
56 | 0 | } |
57 | |
|
58 | 0 | os_.put(0); |
59 | 0 | return os_.getBuffer(); |
60 | 0 | } |
61 | | |
62 | | private: |
63 | | // recursively convert FbsonValue |
64 | 0 | void intern_json(const FbsonValue* val) { |
65 | 0 | switch (val->type()) { |
66 | 0 | case FbsonType::T_Null: { |
67 | 0 | os_.write("null", 4); |
68 | 0 | break; |
69 | 0 | } |
70 | 0 | case FbsonType::T_True: { |
71 | 0 | os_.write("true", 4); |
72 | 0 | break; |
73 | 0 | } |
74 | 0 | case FbsonType::T_False: { |
75 | 0 | os_.write("false", 5); |
76 | 0 | break; |
77 | 0 | } |
78 | 0 | case FbsonType::T_Int8: { |
79 | 0 | os_.write(((Int8Val*)val)->val()); |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | case FbsonType::T_Int16: { |
83 | 0 | os_.write(((Int16Val*)val)->val()); |
84 | 0 | break; |
85 | 0 | } |
86 | 0 | case FbsonType::T_Int32: { |
87 | 0 | os_.write(((Int32Val*)val)->val()); |
88 | 0 | break; |
89 | 0 | } |
90 | 0 | case FbsonType::T_Int64: { |
91 | 0 | os_.write(((Int64Val*)val)->val()); |
92 | 0 | break; |
93 | 0 | } |
94 | 0 | case FbsonType::T_Double: { |
95 | 0 | os_.write(((DoubleVal*)val)->val()); |
96 | 0 | break; |
97 | 0 | } |
98 | 0 | case FbsonType::T_String: { |
99 | 0 | os_.put('"'); |
100 | 0 | os_.write(((StringVal*)val)->getBlob(), ((StringVal*)val)->getBlobLen()); |
101 | 0 | os_.put('"'); |
102 | 0 | break; |
103 | 0 | } |
104 | 0 | case FbsonType::T_Binary: { |
105 | 0 | os_.write("\"<BINARY>", 9); |
106 | 0 | os_.write(((BinaryVal*)val)->getBlob(), ((BinaryVal*)val)->getBlobLen()); |
107 | 0 | os_.write("<BINARY>\"", 9); |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | case FbsonType::T_Object: { |
111 | 0 | object_to_json((ObjectVal*)val); |
112 | 0 | break; |
113 | 0 | } |
114 | 0 | case FbsonType::T_Array: { |
115 | 0 | array_to_json((ArrayVal*)val); |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | default: |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | } |
122 | | |
123 | | // convert object |
124 | 0 | void object_to_json(const ObjectVal* val) { |
125 | 0 | os_.put('{'); |
126 | |
|
127 | 0 | auto iter = val->begin(); |
128 | 0 | auto iter_fence = val->end(); |
129 | |
|
130 | 0 | while (iter < iter_fence) { |
131 | | // write key |
132 | 0 | if (iter->klen()) { |
133 | 0 | os_.put('"'); |
134 | 0 | os_.write(iter->getKeyStr(), iter->klen()); |
135 | 0 | os_.put('"'); |
136 | 0 | } else { |
137 | 0 | os_.write(iter->getKeyId()); |
138 | 0 | } |
139 | 0 | os_.put(':'); |
140 | | |
141 | | // convert value |
142 | 0 | intern_json(iter->value()); |
143 | |
|
144 | 0 | ++iter; |
145 | 0 | if (iter != iter_fence) { |
146 | 0 | os_.put(','); |
147 | 0 | } |
148 | 0 | } |
149 | |
|
150 | 0 | assert(iter == iter_fence); |
151 | | |
152 | 0 | os_.put('}'); |
153 | 0 | } |
154 | | |
155 | | // convert array to json |
156 | 0 | void array_to_json(const ArrayVal* val) { |
157 | 0 | os_.put('['); |
158 | |
|
159 | 0 | auto iter = val->begin(); |
160 | 0 | auto iter_fence = val->end(); |
161 | |
|
162 | 0 | while (iter != iter_fence) { |
163 | | // convert value |
164 | 0 | intern_json((const FbsonValue*)iter); |
165 | 0 | ++iter; |
166 | 0 | if (iter != iter_fence) { |
167 | 0 | os_.put(','); |
168 | 0 | } |
169 | 0 | } |
170 | |
|
171 | 0 | assert(iter == iter_fence); |
172 | | |
173 | 0 | os_.put(']'); |
174 | 0 | } |
175 | | |
176 | | private: |
177 | | FbsonOutStream os_; |
178 | | char buffer_[OUT_BUF_SIZE]; |
179 | | }; |
180 | | |
181 | | } // namespace fbson |
182 | | |
183 | | #endif // FBSON_FBSONUTIL_H |