EpcTools
An event based multi-threaded C++ development framework.
ejsonbuilder.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2020 T-Mobile
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #ifndef __ejsonbuilder_h_included
18 #define __ejsonbuilder_h_included
19 
20 #include "estring.h"
21 #include "eerror.h"
22 
23 #include <memory>
24 
31 {
32 public:
33 
36  EJsonBuilder();
37 
39  ~EJsonBuilder();
40 
42  enum class ContainerType
43  {
45  Array,
47  Object
48  };
49 
52  template<typename T>
53  class StackValue
54  {
55  public:
62  StackValue(EJsonBuilder &builder, const T &value, const EString &name = "");
63 
64  private:
65  EJsonBuilder &m_builder;
66  EString m_name;
67  };
68 
72 
75  template<ContainerType T>
77  {
78  public:
83  StackContainer(EJsonBuilder &builder, const EString &name = "");
84 
87  ~StackContainer();
88 
89  private:
90  EJsonBuilder &m_builder;
91  EString m_name;
92  };
93 
96 
100  Void push(ContainerType type);
101 
104  Void push(const EString &value);
105 
108  Void push(const UInt value);
109 
117  Void pop(const EString &name = "");
118 
123  cpStr toString(Bool pretty = false);
124 
125 private:
126  class Impl;
127  Impl &impl();
128  std::unique_ptr<Impl> m_impl;
129 };
130 
132 DECLARE_ERROR(EJsonBuilder_UnrecognizedType);
133 DECLARE_ERROR(EJsonBuilder_EmptyStack);
134 DECLARE_ERROR(EJsonBuilder_NonEmptyStack);
135 DECLARE_ERROR(EJsonBuilder_EmptyName);
136 DECLARE_ERROR(EJsonBuilder_NonEmptyName);
138 
139 #endif // #ifndef __ejsonbuilder_h_included
cpStr toString(Bool pretty=false)
Returns a string representation of the json objects.
Definition: ejsonbuilder.cpp:209
Encapsulates and extends a std::string object.
A helper class which pushes/pops items on the builder&#39;s object stack based on its lifetime...
Definition: ejsonbuilder.h:53
ContainerType
Enumeration for JSON containers types.
Definition: ejsonbuilder.h:42
Void pop(const EString &name="")
Pops the top object off the stack.
Definition: ejsonbuilder.cpp:204
Void push(ContainerType type)
Pushes a container type object onto the stack.
Definition: ejsonbuilder.cpp:189
EJsonBuilder()
Constructor. Initializes the document object used to contain all appended JSON objects.
Definition: ejsonbuilder.cpp:150
A helper class which pushes/pops items on the builder&#39;s object stack based on its lifetime...
Definition: ejsonbuilder.h:76
an object type with name/values {}
~EJsonBuilder()
Destructor.
Defines base class for exceptions and declaration helper macros.
A class used to build JSON strings. It maintains a stack of JSON objects which allows you to build a ...
Definition: ejsonbuilder.h:30
#define DECLARE_ERROR(__e__)
Declares exception class derived from EError with no constructor parameters.
Definition: eerror.h:53
String class.
Definition: estring.h:31