nlib
JsonReader.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONREADER_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONREADER_H_
5 
6 #include "nn/nlib/UniquePtr.h"
7 #include "nn/nlib/TextReader.h"
8 
9 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
10 #undef NLIB_VIS_PUBLIC
11 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
12 #endif
13 
14 NLIB_NAMESPACE_BEGIN
15 class InputStream;
16 namespace msgpack {
17 class MpObject;
18 
20  size_t maxArraySize;
21  size_t maxMapSize;
22  size_t maxStrLen;
23  size_t maxDepth;
24 
25  public:
26  NLIB_CEXPR JsonReaderSettings(size_t max_array_size_, size_t max_map_size_,
27  size_t max_str_len_, size_t max_depth_) NLIB_NOEXCEPT
28  : maxArraySize(max_array_size_),
29  maxMapSize(max_map_size_),
30  maxStrLen(max_str_len_),
31  maxDepth(max_depth_) {}
33  maxMapSize(256),
34  maxStrLen(2047),
35  maxDepth(7) {}
36 };
37 
39  public:
40  enum Option {
41  OPTION_DEFAULT = 0x00000000,
42  OPTION_RELAXED = 0x00000001
43  };
46  NLIB_VIS_PUBLIC bool Init(InputStream* stream,
47  const JsonReaderSettings& settings) NLIB_NOEXCEPT NLIB_NONNULL;
48  bool Init(InputStream* stream) NLIB_NOEXCEPT NLIB_NONNULL {
50  return this->Init(stream, settings);
51  }
52  bool Close() NLIB_NOEXCEPT { return true; }
53  errno_t GetErrorValue() const NLIB_NOEXCEPT { return m_ErrorValue; }
54  InputStream* GetStream() NLIB_NOEXCEPT { return m_Reader.GetStream(); }
55  int GetLine() const NLIB_NOEXCEPT { return m_Reader.GetLine(); }
56  int GetColumn() const NLIB_NOEXCEPT { return m_Reader.GetColumn(); }
57 
58  public:
59  NLIB_VIS_PUBLIC static errno_t ReadEx(MpObject* obj, const void* jsontext, uint32_t option = 0,
61  static bool Read(MpObject* obj, const char* jsontext,
62  uint32_t option = 0) NLIB_NOEXCEPT NLIB_NONNULL {
63  // This can be used if jsontext is NULL terminated.
64  return ReadEx(obj, jsontext, option, RSIZE_MAX) == 0;
65  }
66  NLIB_VIS_PUBLIC bool Read(MpObject* obj, uint32_t option = 0) NLIB_NOEXCEPT NLIB_NONNULL;
67  NLIB_SAFE_BOOL(JsonReader, GetErrorValue() == 0);
68 
69  private:
70  void SetError(errno_t e) NLIB_NOEXCEPT {
71  if (m_ErrorValue == 0) m_ErrorValue = e;
72  }
73  NLIB_VIS_HIDDEN bool GetValue(MpObject* obj) NLIB_NOEXCEPT;
74  NLIB_VIS_HIDDEN bool GetString(MpObject* obj) NLIB_NOEXCEPT;
75  NLIB_VIS_HIDDEN bool GetNumber(MpObject* obj) NLIB_NOEXCEPT;
76 
77  private:
78  errno_t m_ErrorValue;
79  size_t m_MaxArraySize;
80  size_t m_MaxMapSize;
81  size_t m_MaxStrLen;
82  size_t m_MaxDepth;
83  UniquePtr<char[]> m_StrBuf;
84  size_t m_Depth;
85  TextReader m_Reader;
87 };
88 
89 } // namespace msgpack
90 NLIB_NAMESPACE_END
91 
92 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
93 #undef NLIB_VIS_PUBLIC
94 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
95 #endif
96 
97 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONREADER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
int GetLine() const noexcept
Gets the line number (near where the error occurred).
Definition: JsonReader.h:55
Option
Defines the option value that may be passed to Read.
Definition: JsonReader.h:40
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
Definition: Platform_unix.h:66
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:126
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
Definition: Config.h:141
bool Close() noexcept
Closes JsonReader.
Definition: JsonReader.h:52
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
Data structure used to store the JsonReader settings parameters.
Definition: JsonReader.h:19
constexpr JsonReaderSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets each data member to the d...
Definition: JsonReader.h:32
size_t maxArraySize
Specifies the maximum size of the JSON array being read. The default is 8192.
Definition: JsonReader.h:20
constexpr JsonReaderSettings(size_t max_array_size_, size_t max_map_size_, size_t max_str_len_, size_t max_depth_) noexcept
Instantiates the object. Sets the individual data members.
Definition: JsonReader.h:26
Defines that class that is corresponding to std::unique_ptr.
Object created when MessagePack or JSON is read.
Definition: MpObject.h:83
#define RSIZE_MAX
Defines a value somewhat smaller than the maximum value of size_t.
Definition: Platform.h:295
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
size_t maxMapSize
Specifies the maximum size of the JSON associative array being read. The default is 256...
Definition: JsonReader.h:21
Defines the class for reading text from streams.
int GetColumn() const noexcept
Gets the digit number (near where the error occurred).
Definition: JsonReader.h:56
JSON parser. Reads and parses the JSON string from the stream.
Definition: JsonReader.h:38
The class for reading text from streams.
Definition: TextReader.h:20
The base class for input streams. This class cannot be instantiated.
Definition: InputStream.h:15
size_t maxStrLen
Specifies the maximum size (in bytes) of the string being read. The default is 2047.
Definition: JsonReader.h:22
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
bool Read(BinaryReader *r, T *x)
You can read to user-defined class objects by specializing this function template.
Definition: BinaryReader.h:158
size_t maxDepth
Specifies the maximum depth of JSON (recursive structure). The default is 7.
Definition: JsonReader.h:23
InputStream * GetStream() noexcept
Gets the base stream specified in Init.
Definition: JsonReader.h:54
errno_t GetErrorValue() const noexcept
Gets the error that occurred.
Definition: JsonReader.h:53
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24