JsonWriter.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
17 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18 * GNU General Public License
19 */
20 
21 #pragma once
22 
23 #include <memory>
24 #include <sstream>
25 #include <stack>
26 
27 namespace armarx
28 {
29  class JsonWriter;
30  using JsonWriterPtr = std::shared_ptr<JsonWriter>;
31 
32  class JsonWriter
33  {
34  private:
35  enum StackType
36  {
37  eEmptyObject,
38  eObjectKey,
39  eObject,
40  eEmptyArray,
41  eArray,
42  eFinal
43  };
44 
45  /*
46  * stack empty:
47  * writer is empty
48  * valid calls: startObject, startArray, writeRawValue
49  * eFinal:
50  * writer contains single value or complete object/array
51  * valid calls: none
52  * eEmptyObject:
53  * after startObject()
54  * valid calls: writeKey(...)
55  * eObjectKey:
56  * after writeKey(...)
57  * valid calls: startObject, startArray, writeRawValue
58  * eObject:
59  * inside object, after complete value
60  * valid calls: writeKey(...)
61  * eEmptyArray:
62  * after startArray()
63  * valid calls: startObject, startArray, writeRawValue
64  * eArray:
65  * inside array, after complete value
66  * valid calls: startObject, startArray, writeRawValue
67  *
68  *
69  * stack examples:
70  * <empty> => empty stack
71  * <value> => eFinal
72  * [ => eEmptyArray
73  * [5 => eArray
74  * [[ => eEmptyArray, eEmptyArray
75  * { => eEmptyObject
76  * {"a": => eObjectKey
77  * {"a":5 => eObject
78  * {"a":[ => eObjectKey, eEmptyArray
79  * */
80 
81 
82  public:
83  JsonWriter(int indenting = 0,
84  const std::string& indentChars = " ",
85  bool jsStyleKeys = false);
86  void startObject();
87  void endObject();
88  void writeKey(const std::string& key);
89  void startArray();
90  void endArray();
91  void writeRawValue(const std::string& value);
92  std::string toString();
93  bool isId(const std::string& str);
94 
95  static std::string EscapeQuote(const std::string& str);
96  static std::string Escape(const std::string& str);
97 
98  static inline bool
100  {
101  return c > 0 && c <= 0x1F;
102  }
103 
104  private:
105  std::stringstream ss;
106  std::stack<StackType> stack;
107  int indenting;
108  int nesting;
109  std::string indentChars;
110  bool jsStyleKeys;
111  void writeNewLine();
112  void beginValue();
113  void beginKey();
114  void endKey();
115  void endValue();
116  void endArrayOrObject();
117  };
118 } // namespace armarx
armarx::JsonWriter::writeRawValue
void writeRawValue(const std::string &value)
Definition: JsonWriter.cpp:111
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::JsonWriter::writeKey
void writeKey(const std::string &key)
Definition: JsonWriter.cpp:67
armarx::JsonWriter::startArray
void startArray()
Definition: JsonWriter.cpp:82
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::JsonWriterPtr
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition: JsonWriter.h:30
armarx::JsonWriter::endObject
void endObject()
Definition: JsonWriter.cpp:47
armarx::JsonWriter::toString
std::string toString()
Definition: JsonWriter.cpp:119
armarx::JsonWriter::IsControlChar
static bool IsControlChar(char c)
Definition: JsonWriter.h:99
armarx::JsonWriter::isId
bool isId(const std::string &str)
Definition: JsonWriter.cpp:125
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::JsonWriter::Escape
static std::string Escape(const std::string &str)
Definition: JsonWriter.cpp:141
armarx::JsonWriter::EscapeQuote
static std::string EscapeQuote(const std::string &str)
Definition: JsonWriter.cpp:135
armarx::JsonWriter::startObject
void startObject()
Definition: JsonWriter.cpp:37
armarx::JsonWriter
Definition: JsonWriter.h:32
armarx::JsonWriter::JsonWriter
JsonWriter(int indenting=0, const std::string &indentChars=" ", bool jsStyleKeys=false)
Definition: JsonWriter.cpp:31
armarx::JsonWriter::endArray
void endArray()
Definition: JsonWriter.cpp:91
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27