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 <stack>
25 #include <sstream>
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 { eEmptyObject, eObjectKey, eObject, eEmptyArray, eArray, eFinal };
36  /*
37  * stack empty:
38  * writer is empty
39  * valid calls: startObject, startArray, writeRawValue
40  * eFinal:
41  * writer contains single value or complete object/array
42  * valid calls: none
43  * eEmptyObject:
44  * after startObject()
45  * valid calls: writeKey(...)
46  * eObjectKey:
47  * after writeKey(...)
48  * valid calls: startObject, startArray, writeRawValue
49  * eObject:
50  * inside object, after complete value
51  * valid calls: writeKey(...)
52  * eEmptyArray:
53  * after startArray()
54  * valid calls: startObject, startArray, writeRawValue
55  * eArray:
56  * inside array, after complete value
57  * valid calls: startObject, startArray, writeRawValue
58  *
59  *
60  * stack examples:
61  * <empty> => empty stack
62  * <value> => eFinal
63  * [ => eEmptyArray
64  * [5 => eArray
65  * [[ => eEmptyArray, eEmptyArray
66  * { => eEmptyObject
67  * {"a": => eObjectKey
68  * {"a":5 => eObject
69  * {"a":[ => eObjectKey, eEmptyArray
70  * */
71 
72 
73  public:
74  JsonWriter(int indenting = 0, const std::string& indentChars = " ", bool jsStyleKeys = false);
75  void startObject();
76  void endObject();
77  void writeKey(const std::string& key);
78  void startArray();
79  void endArray();
80  void writeRawValue(const std::string& value);
81  std::string toString();
82  bool isId(const std::string& str);
83 
84  static std::string EscapeQuote(const std::string& str);
85  static std::string Escape(const std::string& str);
86  static inline bool IsControlChar(char c)
87  {
88  return c > 0 && c <= 0x1F;
89  }
90 
91  private:
92  std::stringstream ss;
93  std::stack<StackType> stack;
94  int indenting;
95  int nesting;
96  std::string indentChars;
97  bool jsStyleKeys;
98  void writeNewLine();
99  void beginValue();
100  void beginKey();
101  void endKey();
102  void endValue();
103  void endArrayOrObject();
104  };
105 }
106 
armarx::JsonWriter::writeRawValue
void writeRawValue(const std::string &value)
Definition: JsonWriter.cpp:104
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
armarx::JsonWriter::writeKey
void writeKey(const std::string &key)
Definition: JsonWriter.cpp:63
armarx::JsonWriter::startArray
void startArray()
Definition: JsonWriter.cpp:77
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::JsonWriterPtr
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition: JsonWriter.h:30
armarx::JsonWriter::endObject
void endObject()
Definition: JsonWriter.cpp:44
armarx::JsonWriter::toString
std::string toString()
Definition: JsonWriter.cpp:111
armarx::JsonWriter::IsControlChar
static bool IsControlChar(char c)
Definition: JsonWriter.h:86
armarx::JsonWriter::isId
bool isId(const std::string &str)
Definition: JsonWriter.cpp:116
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::JsonWriter::Escape
static std::string Escape(const std::string &str)
Definition: JsonWriter.cpp:127
armarx::JsonWriter::EscapeQuote
static std::string EscapeQuote(const std::string &str)
Definition: JsonWriter.cpp:122
armarx::JsonWriter::startObject
void startObject()
Definition: JsonWriter.cpp:35
armarx::JsonWriter
Definition: JsonWriter.h:32
armarx::JsonWriter::JsonWriter
JsonWriter(int indenting=0, const std::string &indentChars=" ", bool jsStyleKeys=false)
Definition: JsonWriter.cpp:30
armarx::JsonWriter::endArray
void endArray()
Definition: JsonWriter.cpp:85
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28