libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
JsonEncoder.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef JSONENCODER_H_
32 #define JSONENCODER_H_
33 
34 #include "../Exception.h"
35 #include "../Variable.h"
36 
37 #include <list>
38 #include <atomic>
39 #if __GNUC__ > 4
40 #include <codecvt>
41 #endif
42 
43 namespace BaseLib {
44 
45 class SharedObjects;
46 
47 namespace Rpc {
48 
49 class JsonEncoder {
50  public:
51  JsonEncoder() = default;
52  explicit JsonEncoder(BaseLib::SharedObjects *dummy) {}
53  virtual ~JsonEncoder() = default;
54 
55  static void encode(const std::shared_ptr<Variable> &variable, std::string &json);
56  static void encode(const std::shared_ptr<Variable> &variable, std::vector<char> &json);
57  static std::string encode(const std::shared_ptr<Variable> &variable);
58  static std::vector<char> encodeBinary(const std::shared_ptr<Variable> &variable);
59  void encodeRequest(std::string &methodName, std::shared_ptr<std::list<std::shared_ptr<Variable>>> &parameters, std::vector<char> &encodedData);
60  std::vector<char> encodeRequest(const std::string &methodName, const std::shared_ptr<Variable> &parameters);
61  static void encodeResponse(const std::shared_ptr<Variable> &variable, int32_t id, std::vector<char> &json);
62  static void encodeMQTTResponse(const std::string &methodName, const std::shared_ptr<Variable> &variable, int32_t id, std::vector<char> &json);
63 
64  static std::string encodeString(const std::string &s);
65  private:
66  std::atomic<int32_t> _requestId{1};
67 
68  static void encodeValue(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
69  static void encodeValue(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
70  static void encodeArray(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
71  static void encodeArray(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
72  static void encodeStruct(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
73  static void encodeStruct(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
74  static void encodeBoolean(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
75  static void encodeBoolean(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
76  static void encodeInteger(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
77  static void encodeInteger(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
78  static void encodeInteger64(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
79  static void encodeInteger64(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
80  static void encodeFloat(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
81  static void encodeFloat(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
82  static void encodeString(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
83  static void encodeString(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
84  static void encodeVoid(const std::shared_ptr<Variable> &variable, std::ostringstream &s);
85  static void encodeVoid(const std::shared_ptr<Variable> &variable, std::vector<char> &s);
86 };
87 }
88 }
89 
90 #endif
This is the base library main class.
Definition: BaseLib.h:95
static void encodeMQTTResponse(const std::string &methodName, const std::shared_ptr< Variable > &variable, int32_t id, std::vector< char > &json)
Definition: JsonEncoder.cpp:82
Definition: BaseLib.cpp:34
virtual ~JsonEncoder()=default
static std::vector< char > encodeBinary(const std::shared_ptr< Variable > &variable)
Definition: JsonEncoder.cpp:143
static void encode(const std::shared_ptr< Variable > &variable, std::string &json)
Definition: JsonEncoder.cpp:95
static void encodeResponse(const std::shared_ptr< Variable > &variable, int32_t id, std::vector< char > &json)
Definition: JsonEncoder.cpp:69
static std::string encodeString(const std::string &s)
Definition: JsonEncoder.cpp:676
Definition: JsonEncoder.h:49
JsonEncoder(BaseLib::SharedObjects *dummy)
Definition: JsonEncoder.h:52
void encodeRequest(std::string &methodName, std::shared_ptr< std::list< std::shared_ptr< Variable >>> &parameters, std::vector< char > &encodedData)
Definition: JsonEncoder.cpp:39