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
27namespace armarx
28{
29 class JsonWriter;
30 using JsonWriterPtr = std::shared_ptr<JsonWriter>;
31
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
constexpr T c
std::string str(const T &t)
bool isId(const std::string &str)
void writeKey(const std::string &key)
static std::string Escape(const std::string &str)
static bool IsControlChar(char c)
Definition JsonWriter.h:99
static std::string EscapeQuote(const std::string &str)
std::string toString()
void writeRawValue(const std::string &value)
JsonWriter(int indenting=0, const std::string &indentChars=" ", bool jsStyleKeys=false)
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition JsonWriter.h:30