nlib
JsonStreamGenerator.h
1 
2 /*--------------------------------------------------------------------------------*
3  Project: CrossRoad
4  Copyright (C)Nintendo All rights reserved.
5 
6  These coded instructions, statements, and computer programs contain proprietary
7  information of Nintendo and/or its licensed developers and are protected by
8  national and international copyright laws. They may not be disclosed to third
9  parties or copied or duplicated in any form, in whole or in part, without the
10  prior written consent of Nintendo.
11 
12  The content herein is highly confidential and should be handled accordingly.
13  *--------------------------------------------------------------------------------*/
14 
15 #pragma once
16 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMGENERATOR_H_
17 #define INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMGENERATOR_H_
18 
19 #include <utility>
20 #include "nn/nlib/Swap.h"
22 
23 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
24 #undef NLIB_VIS_PUBLIC
25 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
26 #endif
27 NLIB_NAMESPACE_BEGIN
28 class OutputStream;
29 namespace msgpack {
30 
32  bool msgpack;
35  bool strict;
37  : msgpack(false), pretty_print(false),
38  omit_utf8_checking(false), strict(false) {}
39 };
40 
42  public:
43  enum Error {
44  kOk = 0,
45  kErrorKeyRequired,
46  kErrorKeyForbidden,
47  kErrorEndArrayRequired,
48  kErrorEndArrayForbidden,
49  kErrorEndMapRequired,
50  kErrorEndMapForbidden,
51  kErrorStringUtf8,
52  kErrorStream,
53  kErrorOutOfMemory,
54  kErrorArrayOrMapRequired,
55  kErrorDocumentEnd,
56  kErrorBinaryNotAvailable,
57  kErrorExtNotAvailable,
58  kErrorInvalidParam,
59  ERROR_OK = kOk,
60  ERROR_KEY_REQUIRED = kErrorKeyRequired,
61  ERROR_KEY_FORBIDDEN = kErrorKeyForbidden,
62  ERROR_ENDARRAY_REQUIRED = kErrorEndArrayRequired,
63  ERROR_ENDARRAY_FORBIDDEN = kErrorEndArrayForbidden,
64  ERROR_ENDMAP_REQUIRED = kErrorEndMapRequired,
65  ERROR_ENDMAP_FORBIDDEN = kErrorEndMapForbidden,
66  ERROR_STRING_UTF8 = kErrorStringUtf8,
67  ERROR_STREAM = kErrorStream,
68  ERROR_ENOMEM = kErrorOutOfMemory,
69  ERROR_ARRAY_OR_MAP_REQUIRED = kErrorArrayOrMapRequired,
70  ERROR_DOCUMENT_END = kErrorDocumentEnd,
71  ERROR_BINARY_NOT_AVAILABLE = kErrorBinaryNotAvailable,
72  ERROR_EXT_NOT_AVAILABLE = kErrorExtNotAvailable,
73  ERROR_EINVAL = kErrorInvalidParam
74  };
75 
76  public:
77  static errno_t Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj,
78  const JsonStreamGeneratorSettings& settings)
80  static errno_t Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj)
83  return Generate(written, str, n, obj, settings);
84  }
85  template<size_t N>
86  static errno_t Generate(size_t* written, nlib_utf8_t (&str)[N], const MpObject& obj,
88  return Generate(written, str, N, obj, settings);
89  }
90  template<size_t N>
91  static errno_t Generate(size_t* written, nlib_utf8_t (&str)[N],
92  const MpObject& obj) NLIB_NOEXCEPT {
94  return Generate(written, str, N, obj, settings);
95  }
96 
97  static std::pair<errno_t, size_t>
98  Generate(nlib_utf8_t* str, size_t n, const MpObject& obj,
100  std::pair<errno_t, size_t> rval;
101  rval.first = Generate(&rval.second, str, n, obj, settings);
102  return rval;
103  }
104  template<size_t N>
105  static std::pair<errno_t, size_t>
106  Generate(nlib_utf8_t (&str)[N], const MpObject& obj,
107  const JsonStreamGeneratorSettings& settings) NLIB_NOEXCEPT {
108  return Generate(&str[0], N, obj, settings);
109  }
110  static std::pair<errno_t, size_t>
111  Generate(nlib_utf8_t* str, size_t n, const MpObject& obj) NLIB_NOEXCEPT {
113  return Generate(str, n, obj, settings);
114  }
115  template<size_t N>
116  static std::pair<errno_t, size_t>
117  Generate(nlib_utf8_t (&str)[N], const MpObject& obj) NLIB_NOEXCEPT {
119  return Generate(&str[0], N, obj, settings);
120  }
121 
122  NLIB_CEXPR JsonStreamGenerator() NLIB_NOEXCEPT : prv_(nullptr), error_(kOk) {}
124  NLIB_DEFMOVE_PIMPL(JsonStreamGenerator);
126  using std::swap;
127  swap(prv_, rhs.prv_);
128  swap(error_, rhs.error_);
129  }
130  void Reset() NLIB_NOEXCEPT;
131  errno_t Init(const JsonStreamGeneratorSettings& settings) NLIB_NOEXCEPT;
132  errno_t Init() NLIB_NOEXCEPT {
134  return Init(settings);
135  }
136  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT NLIB_NONNULL;
137  bool Flush() NLIB_NOEXCEPT;
138  errno_t Close() NLIB_NOEXCEPT;
139 
140  JsonStreamGenerator& StartArray(size_t count) NLIB_NOEXCEPT;
141  JsonStreamGenerator& StartMap(size_t count) NLIB_NOEXCEPT;
142  JsonStreamGenerator& Key(const nlib_utf8_t* key) NLIB_NOEXCEPT NLIB_NONNULL;
146  JsonStreamGenerator& Boolean(bool value) NLIB_NOEXCEPT;
147  JsonStreamGenerator& Int8(int8_t num) NLIB_NOEXCEPT;
148  JsonStreamGenerator& Uint8(uint8_t num) NLIB_NOEXCEPT;
149  JsonStreamGenerator& Int16(int16_t num) NLIB_NOEXCEPT;
150  JsonStreamGenerator& Uint16(uint16_t num) NLIB_NOEXCEPT;
151  JsonStreamGenerator& Int32(int32_t num) NLIB_NOEXCEPT;
152  JsonStreamGenerator& Uint32(uint32_t num) NLIB_NOEXCEPT;
153  JsonStreamGenerator& Int64(int64_t num) NLIB_NOEXCEPT;
154  JsonStreamGenerator& Uint64(uint64_t num) NLIB_NOEXCEPT;
155  JsonStreamGenerator& Float(float num) NLIB_NOEXCEPT;
156  JsonStreamGenerator& Double(double num) NLIB_NOEXCEPT;
157  JsonStreamGenerator& String(const nlib_utf8_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
158  JsonStreamGenerator& Binary(const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
159  JsonStreamGenerator& Ext(int8_t tp, const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
161  JsonStreamGenerator& Object(const MpObject& obj) NLIB_NOEXCEPT;
162 
163  // NOTE: nlib_utf8_t str[32]; ... String(str) calls this...
164  // template<class STDSTRING>
165  // JsonStreamGenerator& String(const STDSTRING& str) NLIB_NOEXCEPT {
166  // return String(str.c_str());
167  // }
168  JsonStreamGenerator& EmptyArray() NLIB_NOEXCEPT { return StartArray(0).EndArray(); }
169  JsonStreamGenerator& EmptyMap() NLIB_NOEXCEPT { return StartMap(0).EndMap(); }
170  JsonStreamGenerator& Int8Array(const int8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
171  JsonStreamGenerator& Uint8Array(const uint8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
172  JsonStreamGenerator& Int16Array(const int16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
173  JsonStreamGenerator& Uint16Array(const uint16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
174  JsonStreamGenerator& Int32Array(const int32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
175  JsonStreamGenerator& Uint32Array(const uint32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
176  JsonStreamGenerator& Int64Array(const int64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
177  JsonStreamGenerator& Uint64Array(const uint64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
178  JsonStreamGenerator& FloatArray(const float* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
179  JsonStreamGenerator& DoubleArray(const double* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
180  Error GetError() const NLIB_NOEXCEPT { return error_; }
181  NLIB_SAFE_BOOL(JsonStreamGenerator, error_ == kOk);
182 
183  private:
184  struct State;
185  NLIB_VIS_HIDDEN State* WriteStartSeq_(size_t count) NLIB_NOEXCEPT;
186  NLIB_VIS_HIDDEN bool JsonIndent_(size_t level) NLIB_NOEXCEPT;
187  NLIB_VIS_HIDDEN bool CheckAndStateChange_() NLIB_NOEXCEPT;
188  NLIB_VIS_HIDDEN bool WriteJsonString_(const nlib_utf8_t* str,
189  size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
190  NLIB_VIS_HIDDEN bool WriteMsgpackString_(const nlib_utf8_t* str,
191  size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
192  NLIB_VIS_HIDDEN void String_(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
193  NLIB_VIS_HIDDEN void Key_(const nlib_utf8_t* key, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
194 
195  struct JsonStreamGeneratorPrivate;
196  JsonStreamGeneratorPrivate* prv_;
197  Error error_;
199 };
200 
201 } // namespace msgpack
202 NLIB_NAMESPACE_END
203 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::JsonStreamGenerator)
204 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
205 #undef NLIB_VIS_PUBLIC
206 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
207 #endif
208 
209 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMGENERATOR_H_
errno_t Init() noexcept
Initializes the parser using the default settings.
static errno_t Generate(size_t *written, nlib_utf8_t(&str)[N], const MpObject &obj, const JsonStreamGeneratorSettings &settings) noexcept
Runs Generate(written, str, N, obj, settings).
bool strict
If true, generates an error if the first output is not an array or map.
static errno_t Generate(size_t *written, nlib_utf8_t(&str)[N], const MpObject &obj) noexcept
Runs Generate(written, str, N, obj, settings) with the default JsonStreamGeneratorSettings settings s...
JsonStreamGenerator & EmptyArray() noexcept
Outputs an empty array.
#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:179
#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:194
JsonStreamGenerator & EndArray() noexcept
Finishes the array.
Error GetError() const noexcept
Gets an error.
constexpr JsonStreamGenerator() noexcept
Instantiates the object with default parameters (default constructor).
int64_t nlib_time
The type expressing the time in increments of 100 ns from the zero starting point of 1970-01-01...
Definition: Platform.h:457
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:88
#define NLIB_DEPRECATED
Indicates that a function or something has been deprecated.
Definition: Config.h:109
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:89
bool omit_utf8_checking
If true, checking if the string is UTF-8 is skipped when outputting the string.
Object created when MessagePack or JSON is read.
Definition: MpObject.h:95
#define NLIB_NONNULL_2
Indicates that you cannot specify NULL for the second argument.
bool msgpack
If false, JsonStreamGenerator outputs JSON; if true, it outputs msgpack.
Object created when MessagePack, JSON, or CSV is read.
constexpr JsonStreamGeneratorSettings() noexcept
Instantiates the object with default parameters (default constructor). Sets each data member to the d...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Config.h:105
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:107
static errno_t Generate(size_t *written, nlib_utf8_t *str, size_t n, const MpObject &obj) noexcept
Runs Generate(written, str, n, obj, settings) with the default JsonStreamGeneratorSettings settings s...
bool pretty_print
If true, outputs formatted JSON when outputting JSON.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:245
Data structure used to store the JsonStreamGenerator settings parameters.
JsonStreamGenerator & EndMap() noexcept
Finishes the map.
JsonStreamGenerator & EmptyMap() noexcept
Outputs an empty map.
Class for outputting JSON or msgpack.
#define NLIB_NONNULL
Indicates that you cannot specify NULL for all arguments.
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:30
char nlib_utf8_t
Defines char with a typedef. Indicates that it is a UTF-8 string.
Definition: Platform.h:308
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37