]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/src/json/json_elements.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / src / json / json_elements.cc
CommitLineData
11fdf7f2
TL
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2015 Cloudius Systems
20 */
21
22#include <seastar/json/json_elements.hh>
23#include <string.h>
24#include <string>
25#include <vector>
26#include <sstream>
27
28namespace seastar {
29
30using namespace std;
31
32namespace json {
33
34
35class json_stream_builder;
36/**
37 * The json builder is a helper class
38 * To help create a json object
39 *
40 */
41class json_builder {
42public:
43 json_builder()
44 : first(true) {
45 result << OPEN;
46 }
47
48 /**
49 * add a name value to an object
50 * @param name the name of the element
51 * @param str the value already formated
52 */
53 void add(const string& name, const string& str) {
54 if (first) {
55 first = false;
56 } else {
57 result << ", ";
58 }
59 result << '"' << name << "\": " << str;
60 }
61
62 /**
63 * add a json element to the an object
64 * @param element
65 */
66 void add(json_base_element* element) {
67 if (element == nullptr || element->_set == false) {
68 return;
69 }
70 add(element->_name, element->to_string());
71 }
72
73 /**
74 * Get the string representation of the object
75 * @return a string of accumulative object
76 */
77 string as_json() {
78 result << CLOSE;
79 return result.str();
80 }
81
82private:
83 static const string OPEN;
84 static const string CLOSE;
85 friend class json_stream_builder;
86 stringstream result;
87 bool first;
88
89};
90
91/**
92 * The json builder is a helper class
93 * To help create a json object
94 *
95 */
96class json_stream_builder {
97public:
98 json_stream_builder(output_stream<char>& s)
99 : first(true), open(false), _s(s) {
100 }
101
102 /**
103 * add a name value to an object
104 * @param name the name of the element
105 * @param str the value already formated
106 */
107 future<> add(const string& name, const json_base_element& element) {
108 if (!open) {
109 open = true;
110 return _s.write(json_builder::OPEN).then([this, &name, &element] {
111 return add(name, element);
112 });
113 }
9f95a23c 114 return _s.write(((first)? '"' + name : ",\"" + name) + "\":").then([this, &element] {
11fdf7f2
TL
115 first = false;
116 return element.write(_s);
117 });
118 }
119
120 /**
121 * add a json element to the an object
122 * @param element
123 */
124 future<> add(json_base_element* element) {
125 if (element == nullptr || element->_set == false) {
126 return make_ready_future<>();
127 }
128 return add(element->_name, *element);
129 }
130
131 /**
132 * Get the string representation of the object
133 * @return a string of accumulative object
134 */
135 future<> done() {
136 return _s.write(json_builder::CLOSE);
137 }
138
139private:
140
141 bool first;
142 bool open;
143 output_stream<char>& _s;
144};
145
146const string json_builder::OPEN("{");
147const string json_builder::CLOSE("}");
148
149void json_base::add(json_base_element* element, string name, bool mandatory) {
150 element->_mandatory = mandatory;
151 element->_name = name;
152 _elements.push_back(element);
153}
154
155string json_base::to_json() const {
156 json_builder res;
157 for (auto i : _elements) {
158 res.add(i);
159 }
160 return res.as_json();
161}
162
163future<> json_base::write(output_stream<char>& s) const {
164 return do_with(json_stream_builder(s), [this] (json_stream_builder& builder) {
165 return do_for_each(_elements, [&builder] (auto m) {
166 return builder.add(m);
167 }).then([&builder] {
168 return builder.done();
169 });
170 });
171}
172
173bool json_base::is_verify() const {
174 for (auto i : _elements) {
175 if (!i->is_verify()) {
176 return false;
177 }
178 }
179 return true;
180}
181
182}
183
184}