MistServer  2.5.3-Pro-19-gf5e75b1 ( Generic_64)
json.h
Go to the documentation of this file.
1 
3 #pragma once
4 #include <string>
5 #include <deque>
6 #include <map>
7 #include <istream>
8 #include <vector>
9 #include "socket.h"
10 
12 namespace JSON {
13 
15  enum ValueType {
17  };
18 
19 
21  class Value {
22  friend class Iter;
23  friend class ConstIter;
24  private:
26  long long int intVal;
27  std::string strVal;
28  std::deque<Value*> arrVal;
29  std::map<std::string, Value*> objVal;
30  public:
31  //constructors/destructors
32  Value();
33  ~Value();
34  Value(const Value & rhs);
35  Value(std::istream & fromstream);
36  Value(const std::string & val);
37  Value(const char * val);
38  Value(long long int val);
39  Value(bool val);
40  //comparison operators
41  bool operator==(const Value & rhs) const;
42  bool operator!=(const Value & rhs) const;
43  //assignment operators
44  Value & operator=(const Value & rhs);
45  Value & operator=(const std::string & rhs);
46  Value & operator=(const char * rhs);
47  Value & operator=(const long long int & rhs);
48  Value & operator=(const int & rhs);
49  Value & operator=(const unsigned int & rhs);
50  Value & operator=(const bool & rhs);
51  //converts to basic types
52  operator long long int() const;
53  operator std::string() const;
54  operator bool() const;
55  const std::string asString() const;
56  const long long int asInt() const;
57  const bool asBool() const;
58  const std::string & asStringRef() const;
59  const char * c_str() const;
60  //array operator for maps and arrays
61  Value & operator[](const std::string i);
62  Value & operator[](const char * i);
63  Value & operator[](unsigned int i);
64  const Value & operator[](const std::string i) const;
65  const Value & operator[](const char * i) const;
66  const Value & operator[](unsigned int i) const;
67  //handy functions and others
68  std::string toPacked() const;
69  void sendTo(Socket::Connection & socket) const;
70  unsigned int packedSize() const;
71  void netPrepare();
72  std::string & toNetPacked();
73  std::string toString() const;
74  std::string toPrettyString(int indentation = 0) const;
75  void append(const Value & rhs);
76  void prepend(const Value & rhs);
77  void shrink(unsigned int size);
78  void removeMember(const std::string & name);
79  bool isMember(const std::string & name) const;
80  bool isInt() const;
81  bool isString() const;
82  bool isBool() const;
83  bool isObject() const;
84  bool isArray() const;
85  bool isNull() const;
86  unsigned int size() const;
87  void null();
88  };
89 
90  Value fromDTMI2(std::string & data);
91  Value fromDTMI2(const unsigned char * data, unsigned int len, unsigned int & i);
92  Value fromDTMI(std::string & data);
93  Value fromDTMI(const unsigned char * data, unsigned int len, unsigned int & i);
94  Value fromString(std::string json);
95  Value fromFile(std::string filename);
96  void fromDTMI2(std::string & data, Value & ret);
97  void fromDTMI2(const unsigned char * data, unsigned int len, unsigned int & i, Value & ret);
98  void fromDTMI(std::string & data, Value & ret);
99  void fromDTMI(const unsigned char * data, unsigned int len, unsigned int & i, Value & ret);
100 
101  class Iter {
102  public:
103  Iter(Value & root);
104  Value & operator*() const;
105  Value* operator->() const;
106  operator bool() const;
107  Iter & operator++();
108  const std::string & key() const;
109  unsigned int num() const;
110  private:
112  Value * r;
113  unsigned int i;
114  std::deque<Value*>::iterator aIt;
115  std::map<std::string, Value*>::iterator oIt;
116  };
117  class ConstIter {
118  public:
119  ConstIter(const Value & root);
120  const Value & operator*() const;
121  const Value* operator->() const;
122  operator bool() const;
123  ConstIter & operator++();
124  const std::string & key() const;
125  unsigned int num() const;
126  private:
128  const Value * r;
129  unsigned int i;
130  std::deque<Value*>::const_iterator aIt;
131  std::map<std::string, Value*>::const_iterator oIt;
132  };
133  #define jsonForEach(val, i) for(JSON::Iter i(val); i; ++i)
134  #define jsonForEachConst(val, i) for(JSON::ConstIter i(val); i; ++i)
135 
136  template <typename T>
137  std::string encodeVector(T begin, T end) {
138  std::string result;
139  for (T it = begin; it != end; it++) {
140  long long int tmp = (*it);
141  while (tmp >= 0xFFFF) {
142  result += (char)0xFF;
143  result += (char)0xFF;
144  tmp -= 0xFFFF;
145  }
146  result += (char)(tmp / 256);
147  result += (char)(tmp % 256);
148  }
149  return result;
150  }
151 
152  template <typename T>
153  void decodeVector(std::string input, T & result) {
154  result.clear();
155  unsigned int tmp = 0;
156  for (int i = 0; i < input.size(); i += 2) {
157  unsigned int curLen = (input[i] << 8) + input[i + 1];
158  tmp += curLen;
159  if (curLen != 0xFFFF) {
160  result.push_back(tmp);
161  tmp = 0;
162  }
163  }
164  }
165 
166  template <typename T>
167  std::string encodeVector4(T begin, T end) {
168  std::string result;
169  for (T it = begin; it != end; it++) {
170  long long int tmp = (*it);
171  while (tmp >= 0xFFFFFFFF) {
172  result += (char)0xFF;
173  result += (char)0xFF;
174  result += (char)0xFF;
175  result += (char)0xFF;
176  tmp -= 0xFFFFFFFF;
177  }
178  result += (char)((tmp & 0xFF000000) >> 24);
179  result += (char)((tmp & 0x00FF0000) >> 16);
180  result += (char)((tmp & 0x0000FF00) >> 8);
181  result += (char)((tmp & 0x000000FF));
182  }
183  return result;
184  }
185 
186  template <typename T>
187  void decodeVector4(std::string input, T & result) {
188  result.clear();
189  unsigned int tmp = 0;
190  for (int i = 0; i < input.size(); i += 4) {
191  unsigned int curLen = (input[i] << 24) + (input[i + 1] << 16) + (input[i + 2] << 8) + (input[i + 3]);
192  tmp += curLen;
193  if (curLen != 0xFFFFFFFF) {
194  result.push_back(tmp);
195  tmp = 0;
196  }
197  }
198  }
199 }
bool isMember(const std::string &name) const
For object JSON::Value objects, returns true if the given name is a member.
Definition: json.cpp:1115
std::string strVal
Definition: json.h:27
Definition: json.h:101
bool isObject() const
Returns true if this object is an object.
Definition: json.cpp:1135
void null()
Completely clears the contents of this value, changing its type to NULL in the process.
Definition: json.cpp:473
std::string toPacked() const
Packs to a std::string for transfer over the network.
Definition: json.cpp:692
unsigned int size() const
Returns the total of the objects and array size combined.
Definition: json.cpp:1150
bool isString() const
Returns true if this object is a string.
Definition: json.cpp:1125
ValueType myType
Definition: json.h:111
const bool asBool() const
Explicit conversion to bool.
Definition: json.cpp:600
const std::string & asStringRef() const
Explicit conversion to std::string reference.
Definition: json.cpp:608
Definition: json.h:16
unsigned int packedSize() const
Returns the packed size of this Value.
Definition: json.cpp:830
void decodeVector(std::string input, T &result)
Definition: json.h:153
std::string & toNetPacked()
Packs any object-type JSON::Value to a std::string for transfer over the network, including proper DT...
Definition: json.cpp:932
std::string toString() const
Converts this JSON::Value to valid JSON notation and returns it.
Definition: json.cpp:948
Value fromString(std::string json)
Converts a std::string to a JSON::Value.
Definition: json.cpp:1155
A JSON::Value is either a string or an integer, but may also be an object, array or null...
Definition: json.h:21
bool isNull() const
Returns true if this object is null.
Definition: json.cpp:1145
std::deque< Value * >::iterator aIt
Definition: json.h:114
friend class ConstIter
Definition: json.h:23
ValueType myType
Definition: json.h:127
void sendTo(Socket::Connection &socket) const
Packs and transfers over the network.
Definition: json.cpp:745
Definition: json.h:16
void decodeVector4(std::string input, T &result)
Definition: json.h:187
bool operator!=(const Value &rhs) const
Compares a JSON::Value to another for equality.
Definition: json.cpp:467
std::map< std::string, Value * >::iterator oIt
Definition: json.h:115
Definition: json.h:16
bool isBool() const
Returns true if this object is a bool.
Definition: json.cpp:1130
Value * r
Definition: json.h:112
const char * c_str() const
Explicit conversion to c-string.
Definition: json.cpp:620
const std::string asString() const
Explicit conversion to std::string.
Definition: json.cpp:592
std::map< std::string, Value * > objVal
Definition: json.h:29
long long int intVal
Definition: json.h:26
std::deque< Value * > arrVal
Definition: json.h:28
std::string toPrettyString(int indentation=0) const
Converts this JSON::Value to valid JSON notation and returns it.
Definition: json.cpp:1006
Value()
Sets this JSON::Value to null;.
Definition: json.cpp:277
std::deque< Value * >::const_iterator aIt
Definition: json.h:130
void removeMember(const std::string &name)
For object JSON::Value objects, removes the member with the given name, if it exists.
Definition: json.cpp:1106
Definition: json.h:117
Value fromFile(std::string filename)
Converts a file to a JSON::Value.
Definition: json.cpp:1161
Value & operator[](const std::string i)
Retrieves or sets the JSON::Value at this position in the object.
Definition: json.cpp:629
const long long int asInt() const
Explicit conversion to long long int.
Definition: json.cpp:596
ValueType
Lists all types of JSON::Value.
Definition: json.h:15
Definition: json.h:16
Value fromDTMI2(std::string &data)
Definition: json.cpp:1280
bool operator==(const Value &rhs) const
Compares a JSON::Value to another for equality.
Definition: json.cpp:431
unsigned int i
Definition: json.h:129
void netPrepare()
Pre-packs any object-type JSON::Value to a std::string for transfer over the network, including proper DTMI header.
Definition: json.cpp:861
bool isInt() const
Returns true if this object is an integer.
Definition: json.cpp:1120
Definition: json.h:16
void append(const Value &rhs)
Appends the given value to the end of this JSON::Value array.
Definition: json.cpp:1069
std::string encodeVector4(T begin, T end)
Definition: json.h:167
~Value()
Sets this JSON::Value to null.
Definition: json.cpp:282
const Value * r
Definition: json.h:128
ValueType myType
Definition: json.h:25
Value fromDTMI(std::string &data)
Parses a std::string to a valid JSON::Value.
Definition: json.cpp:1269
std::string encodeVector(T begin, T end)
Definition: json.h:137
This class is for easy communicating through sockets, either TCP or Unix.
Definition: socket.h:47
void shrink(unsigned int size)
For array and object JSON::Value objects, reduces them so they contain at most size elements...
Definition: json.cpp:1093
JSON-related classes and functions.
Definition: json.h:12
std::map< std::string, Value * >::const_iterator oIt
Definition: json.h:131
Definition: json.h:16
friend class Iter
Definition: json.h:22
unsigned int i
Definition: json.h:113
Value & operator=(const Value &rhs)
Sets this JSON::Value to be equal to the given JSON::Value.
Definition: json.cpp:481
void prepend(const Value &rhs)
Prepends the given value to the beginning of this JSON::Value array.
Definition: json.cpp:1079
bool isArray() const
Returns true if this object is an array.
Definition: json.cpp:1140