PLCnext API Documentation 25.0.2.69
JsonReader.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Core/PimplPtr.hxx"
9#include "Arp/System/Commons/Json/JsonSerializationContext.hpp"
10#include "Arp/System/Commons/Exceptions/JsonException.hpp"
11#include "Arp/System/Commons/Exceptions/Exceptions.h"
12#include "Arp/System/Core/IEnumerator.hxx"
13
14namespace Arp { namespace System { namespace Commons { namespace Json
15{
16
18
20class JsonValue;
21class ARP_CXX_SYMBOL_EXPORT JsonReader
22{
23public: // Impl forward declaration
24 class Impl;
25
26public: // construction/destruction/assignment
28 JsonReader(const JsonReader& arg) = delete;
29 JsonReader(JsonReader&& arg)noexcept;
30 JsonReader& operator=(const JsonReader& arg) = delete;
33
34public: // static operations
35 static JsonReader Create(const String& filename);
36 static JsonReader CreateFromText(const String& text);
37 static bool TryCreate(const String& filename, JsonReader& reader);
38 static bool TryCreateFromText(const String& text, JsonReader& reader);
39
40public: // setter/getter operations
41 const String& GetDocumentFileName();
42
43 bool HasRootObject(void);
44 bool HasRootSimpleArray(void);
45 bool HasRootComplexArray(void);
46
47public: // operations
48
49 // go straight into the object (and complex array)
50 void ReadRootObject(void);
51 bool TryReadRootObject(void);
52 void ReadStartObject(const char* objectName);
53 bool TryReadStartObject(const char* objectName);
54 void ReadEndObject(void);
55 bool TryReadEndObject(void);
56
57 // go into the object (and complex array) using a loop
58 bool TryReadStartAny(String& name);
59 void ReadEndAny(void);
60 bool TryReadEndAny(void);
61
62 // loop complex array items
63 void ReadStartComplexArrayItem(void);
64 bool TryReadStartComplexArrayItem(void);
65 void ReadEndComplexArrayItem(void);
66 bool TryReadEndComplexArrayItem(void);
67
68 bool TryReadValue(const char* valueName, bool& result);
69 bool TryReadValue(const char* valueName, String& result);
70 bool TryReadValue(const char* valueName, uint8& result);
71 bool TryReadValue(const char* valueName, int8& result);
72 bool TryReadValue(const char* valueName, uint16& result);
73 bool TryReadValue(const char* valueName, int16& result);
74 bool TryReadValue(const char* valueName, uint32& result);
75 bool TryReadValue(const char* valueName, int32& result);
76 bool TryReadValue(const char* valueName, uint64& result);
77 bool TryReadValue(const char* valueName, int64& result);
78 bool TryReadValue(const char* valueName, float32& result);
79 bool TryReadValue(const char* valueName, float64& result);
80
81 void ReadValue(const char* valueName, char* pBuffer, size_t bufferSize);
82 bool TryReadValue(const char* valueName, char* pBuffer, size_t bufferSize);
83
84 bool TryReadSimpleArray(const char* arrayName, std::vector<bool>& result);
85 bool TryReadSimpleArray(const char* arrayName, std::vector<String>& result);
86 bool TryReadSimpleArray(const char* arrayName, std::vector<uint8>& result);
87 bool TryReadSimpleArray(const char* arrayName, std::vector<int8>& result);
88 bool TryReadSimpleArray(const char* arrayName, std::vector<uint16>& result);
89 bool TryReadSimpleArray(const char* arrayName, std::vector<int16>& result);
90 bool TryReadSimpleArray(const char* arrayName, std::vector<uint32>& result);
91 bool TryReadSimpleArray(const char* arrayName, std::vector<int32>& result);
92 bool TryReadSimpleArray(const char* arrayName, std::vector<uint64>& result);
93 bool TryReadSimpleArray(const char* arrayName, std::vector<int64>& result);
94 bool TryReadSimpleArray(const char* arrayName, std::vector<float32>& result);
95 bool TryReadSimpleArray(const char* arrayName, std::vector<float64>& result);
96 bool TryReadSimpleArray(const char* arrayName, std::vector<Arp::byte>& result);
97
98 JsonValue GetRootValue(void);
99 bool TryGetRootValue(JsonValue& jsonValueResult);
100 JsonValue GetValue(const char* valueName);
101 bool TryGetValue(const char* valueName, JsonValue& jsonValueResult);
102
103 IEnumerator<const JsonValue&>::Ptr GetValueEnumerator(void);
104
105 template<class T>
106 bool TryReadStartComplexArrayItem(T& arrayObjectResult, JsonSerializationContext& context);
107
108 template<class T>
109 T ReadValue(const char* valueName);
110
111 template<class T>
112 std::vector<T> ReadSimpleArray(const char* arrayName);
113
114 template<typename... Args>
115 JsonException CreateException(const char* message, const Args& ... args) const;
116 JsonException CreateException(const String& message) const;
117
118public: // Impl operations
119 Impl& GetImpl(void);
120 const Impl& GetImpl(void)const;
121
122private: // Impl usings
123 using Pimpl = PimplPtr<Impl>;
124
125private: // Impl fields
126 Pimpl pimpl;
127};
128
130// inline methods of class JsonReader
131
136template<class T>
138{
140 {
141 arrayObjectResult.ReadJson(*this, context);
142
144
145 return true;
146 }
147 // else
148 return false;
149}
150
155template<class T>
156inline T JsonReader::ReadValue(const char* valueName)
157{
158 T value;
159 if (!this->TryReadValue(valueName, value))
160 {
161 throw JsonException("Error reading value, type mismatch, valueName: {0} type: {1}", valueName, typeid(T).name());
162 }
163 // else
164 return value;
165}
166
171template<class T>
172inline std::vector<T> JsonReader::ReadSimpleArray(const char* arrayName)
173{
174 std::vector<T> value;
175 if (!this->TryReadSimpleArray(arrayName, value))
176 {
177 throw JsonException("Error reading simple array, type mismatch, arrayName: {0} type: {1}", arrayName, typeid(T).name());
178 }
179 // else
180 return value;
181}
182
183template<typename... Args>
184inline JsonException JsonReader::CreateException(const char* message, const Args& ... args) const
185{
186 return this->CreateException(String::Format(message, args...));
187}
188
189}}}} // end of namespace Arp::System::Commons::Json
std::shared_ptr< IEnumerator > Ptr
The smart pointer type of this interface.
Definition: IEnumerator.hxx:52
Adapter class to implement PImpl idiom.
Definition: PimplPtr.hxx:15
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
static String Format(const String &format, const Args &... args)
Formats the format string using the .NET/Python syntax with the given variadic arguments.
Definition: String.inl:18
This exception is used for Json parsing errors.
Definition: JsonException.hpp:15
Definition: JsonReader.hpp:22
JsonReader(void)
Default constructor.
bool TryReadValue(const char *valueName, bool &result)
Reads a bool value
Definition: JsonReader.cpp:189
bool TryReadSimpleArray(const char *arrayName, std::vector< bool > &result)
Reads a simple array of bool
Definition: JsonReader.cpp:317
JsonReader(JsonReader &&arg) noexcept
Default move constructor.
bool TryReadEndComplexArrayItem(void)
End of reading a complex array item
Definition: JsonReader.cpp:178
bool TryReadStartComplexArrayItem(void)
Reads a complex array item
Definition: JsonReader.cpp:164
std::vector< T > ReadSimpleArray(const char *arrayName)
Reads a simple array
Definition: JsonReader.hpp:172
JsonReader & operator=(JsonReader &&arg) noexcept
Default move-assignment operator.
void ReadValue(const char *valueName, char *pBuffer, size_t bufferSize)
Reads a char* value
Definition: JsonReader.cpp:298
~JsonReader(void)
Default destructor.
Json context used during Json reading and writing to hold the context information
Definition: JsonSerializationContext.hpp:20
std::uint32_t uint32
The Arp unsigned integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:33
float float32
The Arp floating point numbers type of 4 byte size.
Definition: PrimitiveTypes.hpp:41
std::int16_t int16
The Arp integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:31
std::uint8_t uint8
The Arp unsigned integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:25
std::uint16_t uint16
The Arp unsigned integer type of 2 byte size.
Definition: PrimitiveTypes.hpp:29
std::int64_t int64
The Arp integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:39
std::int32_t int32
The Arp integer type of 4 byte size.
Definition: PrimitiveTypes.hpp:35
std::int8_t int8
The Arp integer type of 1 byte size.
Definition: PrimitiveTypes.hpp:27
std::uint64_t uint64
The Arp unsigned integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:37
double float64
The Arp floating point numbers type of 8 byte size.
Definition: PrimitiveTypes.hpp:43
@ Create
Creates a new file. If the file already exists, it is overwritten.
@ ReadValue
Reading values of objects.
Root namespace for the PLCnext API