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 "nn/nlib/Swap.h"
21 
22 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
23 #undef NLIB_VIS_PUBLIC
24 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
25 #endif
26 NLIB_NAMESPACE_BEGIN
27 class OutputStream;
28 namespace msgpack {
29 
31  bool msgpack;
34  bool strict;
36  : msgpack(false), pretty_print(false),
37  omit_utf8_checking(false), strict(false) {}
38 };
39 
41  public:
42  enum Error {
43  kOk = 0,
44  kErrorKeyRequired,
45  kErrorKeyForbidden,
46  kErrorEndArrayRequired,
47  kErrorEndArrayForbidden,
48  kErrorEndMapRequired,
49  kErrorEndMapForbidden,
50  kErrorStringUtf8,
51  kErrorStream,
52  kErrorOutOfMemory,
53  kErrorArrayOrMapRequired,
54  kErrorDocumentEnd,
55  kErrorBinaryNotAvailable,
56  kErrorExtNotAvailable,
57  kErrorInvalidParam,
58  ERROR_OK = kOk,
59  ERROR_KEY_REQUIRED = kErrorKeyRequired,
60  ERROR_KEY_FORBIDDEN = kErrorKeyForbidden,
61  ERROR_ENDARRAY_REQUIRED = kErrorEndArrayRequired,
62  ERROR_ENDARRAY_FORBIDDEN = kErrorEndArrayForbidden,
63  ERROR_ENDMAP_REQUIRED = kErrorEndMapRequired,
64  ERROR_ENDMAP_FORBIDDEN = kErrorEndMapForbidden,
65  ERROR_STRING_UTF8 = kErrorStringUtf8,
66  ERROR_STREAM = kErrorStream,
67  ERROR_ENOMEM = kErrorOutOfMemory,
68  ERROR_ARRAY_OR_MAP_REQUIRED = kErrorArrayOrMapRequired,
69  ERROR_DOCUMENT_END = kErrorDocumentEnd,
70  ERROR_BINARY_NOT_AVAILABLE = kErrorBinaryNotAvailable,
71  ERROR_EXT_NOT_AVAILABLE = kErrorExtNotAvailable,
72  ERROR_EINVAL = kErrorInvalidParam
73  };
74 
75  public:
76  static errno_t Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj,
77  const JsonStreamGeneratorSettings& settings)
79  static errno_t Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj)
82  return Generate(written, str, n, obj, settings);
83  }
84  template<size_t N>
85  static errno_t Generate(size_t* written, nlib_utf8_t (&str)[N], const MpObject& obj,
87  return Generate(written, str, N, obj, settings);
88  }
89  template<size_t N>
90  static errno_t Generate(size_t* written, nlib_utf8_t (&str)[N],
91  const MpObject& obj) NLIB_NOEXCEPT {
93  return Generate(written, str, N, obj, settings);
94  }
95 
96  JsonStreamGenerator() NLIB_NOEXCEPT : prv_(NULL), error_(kOk) {}
98  NLIB_MOVE_MEMBER_HELPER_1(JsonStreamGenerator, prv_);
100  errno_t Init() NLIB_NOEXCEPT {
102  return Init(settings);
103  }
104  errno_t Open(OutputStream* stream) NLIB_NOEXCEPT NLIB_NONNULL;
105  bool Flush() NLIB_NOEXCEPT;
106  errno_t Close() NLIB_NOEXCEPT;
107 
108  JsonStreamGenerator& StartArray(size_t count) NLIB_NOEXCEPT;
109  JsonStreamGenerator& StartMap(size_t count) NLIB_NOEXCEPT;
110  JsonStreamGenerator& Key(const nlib_utf8_t* key) NLIB_NOEXCEPT NLIB_NONNULL;
114  JsonStreamGenerator& Boolean(bool value) NLIB_NOEXCEPT;
115  JsonStreamGenerator& Int8(int8_t num) NLIB_NOEXCEPT;
116  JsonStreamGenerator& Uint8(uint8_t num) NLIB_NOEXCEPT;
117  JsonStreamGenerator& Int16(int16_t num) NLIB_NOEXCEPT;
118  JsonStreamGenerator& Uint16(uint16_t num) NLIB_NOEXCEPT;
119  JsonStreamGenerator& Int32(int32_t num) NLIB_NOEXCEPT;
120  JsonStreamGenerator& Uint32(uint32_t num) NLIB_NOEXCEPT;
121  JsonStreamGenerator& Int64(int64_t num) NLIB_NOEXCEPT;
122  JsonStreamGenerator& Uint64(uint64_t num) NLIB_NOEXCEPT;
123  JsonStreamGenerator& Float(float num) NLIB_NOEXCEPT;
124  JsonStreamGenerator& Double(double num) NLIB_NOEXCEPT;
125  JsonStreamGenerator& String(const nlib_utf8_t* str) NLIB_NOEXCEPT NLIB_NONNULL;
126  JsonStreamGenerator& Binary(const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
127  JsonStreamGenerator& Ext(int8_t tp, const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
128  JsonStreamGenerator& Object(const MpObject& obj) NLIB_NOEXCEPT;
129 
130  template<class STDSTRING>
131  JsonStreamGenerator& String(const STDSTRING& str) NLIB_NOEXCEPT {
132  return String(str.c_str());
133  }
134  JsonStreamGenerator& EmptyArray() NLIB_NOEXCEPT { return StartArray(0).EndArray(); }
135  JsonStreamGenerator& EmptyMap() NLIB_NOEXCEPT { return StartMap(0).EndMap(); }
136  JsonStreamGenerator& Int8Array(const int8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
137  JsonStreamGenerator& Uint8Array(const uint8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
138  JsonStreamGenerator& Int16Array(const int16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
139  JsonStreamGenerator& Uint16Array(const uint16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
140  JsonStreamGenerator& Int32Array(const int32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
141  JsonStreamGenerator& Uint32Array(const uint32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
142  JsonStreamGenerator& Int64Array(const int64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
143  JsonStreamGenerator& Uint64Array(const uint64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
144  JsonStreamGenerator& FloatArray(const float* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
145  JsonStreamGenerator& DoubleArray(const double* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
146  Error GetError() const NLIB_NOEXCEPT { return error_; }
147  NLIB_SAFE_BOOL(JsonStreamGenerator, error_ == kOk);
148  void swap(JsonStreamGenerator& rhs) NLIB_NOEXCEPT {
149  using std::swap;
150  swap(prv_, rhs.prv_);
151  swap(error_, rhs.error_);
152  }
153 
154  private:
155  struct State;
156  NLIB_VIS_HIDDEN State* WriteStartSeq_(size_t count) NLIB_NOEXCEPT;
157  NLIB_VIS_HIDDEN bool JsonIndent_(size_t level) NLIB_NOEXCEPT;
158  NLIB_VIS_HIDDEN bool CheckAndStateChange_() NLIB_NOEXCEPT;
159  NLIB_VIS_HIDDEN bool WriteJsonString_(const nlib_utf8_t* str,
160  size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
161  NLIB_VIS_HIDDEN bool WriteMsgpackString_(const nlib_utf8_t* str,
162  size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
163  NLIB_VIS_HIDDEN void String_(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
164  NLIB_VIS_HIDDEN void Key_(const nlib_utf8_t* key, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
165 
166  struct JsonStreamGeneratorPrivate;
167  JsonStreamGeneratorPrivate* prv_;
168  Error error_;
170 };
171 
172 } // namespace msgpack
173 NLIB_NAMESPACE_END
174 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::JsonStreamGenerator)
175 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
176 #undef NLIB_VIS_PUBLIC
177 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
178 #endif
179 
180 #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:163
#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:178
JsonStreamGenerator & EndArray() noexcept
Finishes the array.
Error GetError() const noexcept
Gets an error.
JsonStreamGenerator() noexcept
Instantiates the object with default parameters (default constructor).
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:88
#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.
JsonStreamGenerator & String(const STDSTRING &str) noexcept
Outputs a string.
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:99
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:93
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:229
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:300
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37