nlib
JsonStreamGenerator.h
Go to the documentation of this file.
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  pretty_print(false),
38  omit_utf8_checking(false),
39  strict(false) {}
40 };
41 
43  public:
44  enum Error {
45  kOk = 0,
46  kErrorKeyRequired,
47  kErrorKeyForbidden,
48  kErrorEndArrayRequired,
49  kErrorEndArrayForbidden,
50  kErrorEndMapRequired,
51  kErrorEndMapForbidden,
52  kErrorStringUtf8,
53  kErrorStream,
54  kErrorOutOfMemory,
55  kErrorArrayOrMapRequired,
56  kErrorDocumentEnd,
57  kErrorBinaryNotAvailable,
58  kErrorExtNotAvailable,
59  kErrorInvalidParam
60  };
61 
62  public:
63  static errno_t
64  Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj,
66  static errno_t
67  Generate(size_t* written, nlib_utf8_t* str, size_t n, const MpObject& obj) NLIB_NOEXCEPT {
69  return Generate(written, str, n, obj, settings);
70  }
71  template<size_t N>
72  static errno_t Generate(size_t* written, nlib_utf8_t (&str)[N], const MpObject& obj,
74  return Generate(written, str, N, obj, settings);
75  }
76  template<size_t N>
77  static errno_t
78  Generate(size_t* written, nlib_utf8_t (&str)[N], const MpObject& obj) NLIB_NOEXCEPT {
80  return Generate(written, str, N, obj, settings);
81  }
82 
83  static std::pair<errno_t, size_t>
84  Generate(nlib_utf8_t* str, size_t n, const MpObject& obj,
86  std::pair<errno_t, size_t> rval;
87  rval.first = Generate(&rval.second, str, n, obj, settings);
88  return rval;
89  }
90  template<size_t N>
91  static std::pair<errno_t, size_t>
92  Generate(nlib_utf8_t (&str)[N], const MpObject& obj,
94  return Generate(&str[0], N, obj, settings);
95  }
96  static std::pair<errno_t, size_t>
97  Generate(nlib_utf8_t* str, size_t n, const MpObject& obj) NLIB_NOEXCEPT {
99  return Generate(str, n, obj, settings);
100  }
101  template<size_t N>
102  static std::pair<errno_t, size_t>
103  Generate(nlib_utf8_t (&str)[N], const MpObject& obj) NLIB_NOEXCEPT {
105  return Generate(&str[0], N, obj, settings);
106  }
107 
108  NLIB_CEXPR JsonStreamGenerator() NLIB_NOEXCEPT : prv_(nullptr), error_(kOk) {}
110  NLIB_DEFMOVE_PIMPL(JsonStreamGenerator);
111  void Reset() NLIB_NOEXCEPT;
112  errno_t Init(const JsonStreamGeneratorSettings& settings) NLIB_NOEXCEPT;
115  return Init(settings);
116  }
118  bool Flush() NLIB_NOEXCEPT;
119  errno_t Close() NLIB_NOEXCEPT;
120 
121  JsonStreamGenerator& StartArray(size_t count) NLIB_NOEXCEPT;
122  JsonStreamGenerator& StartMap(size_t count) NLIB_NOEXCEPT;
127  JsonStreamGenerator& Boolean(bool value) NLIB_NOEXCEPT;
128  JsonStreamGenerator& Int8(int8_t num) NLIB_NOEXCEPT;
129  JsonStreamGenerator& Uint8(uint8_t num) NLIB_NOEXCEPT;
130  JsonStreamGenerator& Int16(int16_t num) NLIB_NOEXCEPT;
131  JsonStreamGenerator& Uint16(uint16_t num) NLIB_NOEXCEPT;
132  JsonStreamGenerator& Int32(int32_t num) NLIB_NOEXCEPT;
133  JsonStreamGenerator& Uint32(uint32_t num) NLIB_NOEXCEPT;
134  JsonStreamGenerator& Int64(int64_t num) NLIB_NOEXCEPT;
135  JsonStreamGenerator& Uint64(uint64_t num) NLIB_NOEXCEPT;
136  JsonStreamGenerator& Float(float num) NLIB_NOEXCEPT;
137  JsonStreamGenerator& Double(double num) NLIB_NOEXCEPT;
139  JsonStreamGenerator& Binary(const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
140  JsonStreamGenerator& Ext(int8_t tp, const void* bin, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
142  JsonStreamGenerator& Object(const MpObject& obj) NLIB_NOEXCEPT;
143 
144  // NOTE: nlib_utf8_t str[32]; ... String(str) calls this...
145  // template<class STDSTRING>
146  // JsonStreamGenerator& String(const STDSTRING& str) NLIB_NOEXCEPT {
147  // return String(str.c_str());
148  // }
149  JsonStreamGenerator& EmptyArray() NLIB_NOEXCEPT { return StartArray(0).EndArray(); }
150  JsonStreamGenerator& EmptyMap() NLIB_NOEXCEPT { return StartMap(0).EndMap(); }
151  JsonStreamGenerator& Int8Array(const int8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
152  JsonStreamGenerator& Uint8Array(const uint8_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
153  JsonStreamGenerator& Int16Array(const int16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
154  JsonStreamGenerator& Uint16Array(const uint16_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
155  JsonStreamGenerator& Int32Array(const int32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
156  JsonStreamGenerator& Uint32Array(const uint32_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
157  JsonStreamGenerator& Int64Array(const int64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
158  JsonStreamGenerator& Uint64Array(const uint64_t* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
159  JsonStreamGenerator& FloatArray(const float* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
160  JsonStreamGenerator& DoubleArray(const double* p, size_t count) NLIB_NOEXCEPT NLIB_NONNULL;
161  Error GetError() const NLIB_NOEXCEPT { return error_; }
162  NLIB_SAFE_BOOL(JsonStreamGenerator, error_ == kOk);
163 
164  private:
165  struct State;
166  NLIB_VIS_HIDDEN State* WriteStartSeq_(size_t count) NLIB_NOEXCEPT;
167  NLIB_VIS_HIDDEN bool JsonIndent_(size_t level) NLIB_NOEXCEPT;
168  NLIB_VIS_HIDDEN bool CheckAndStateChange_() NLIB_NOEXCEPT;
169  NLIB_VIS_HIDDEN bool
170  WriteJsonString_(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
171  NLIB_VIS_HIDDEN bool
172  WriteMsgpackString_(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
173  NLIB_VIS_HIDDEN void String_(const nlib_utf8_t* str, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
174  NLIB_VIS_HIDDEN void Key_(const nlib_utf8_t* key, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
175 
176  struct JsonStreamGeneratorPrivate;
177  JsonStreamGeneratorPrivate* prv_;
178  Error error_;
180 };
181 
182 } // namespace msgpack
183 NLIB_NAMESPACE_END
184 NLIB_DEFINE_STD_SWAP(::nlib_ns::msgpack::JsonStreamGenerator)
185 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
186 #undef NLIB_VIS_PUBLIC
187 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
188 #endif
189 
190 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONSTREAMGENERATOR_H_
static errno_t Generate(size_t *written, nlib_utf8_t(&str)[N], const MpObject &obj, const JsonStreamGeneratorSettings &settings) noexcept
A template overload of the above function.
bool strict
If true, generates an error if the first output is not an array or map. The default value is false...
static errno_t Generate(size_t *written, nlib_utf8_t(&str)[N], const MpObject &obj) noexcept
A template overload of the above function.
#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:183
#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:199
static std::pair< errno_t, size_t > Generate(nlib_utf8_t *str, size_t n, const MpObject &obj) noexcept
A parameter omitted version of the above function which passes settings as the default value...
Error GetError() const noexcept
Gets an error.
constexpr JsonStreamGenerator() noexcept
Instantiates the object with default parameters (default constructor).
static std::pair< errno_t, size_t > Generate(nlib_utf8_t(&str)[N], const MpObject &obj) noexcept
A template overload of the above function.
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:452
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:86
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:87
bool omit_utf8_checking
If true, checking if the string is UTF-8 is skipped when outputting the string. The default value is ...
Object created when MessagePack or JSON is read.
Definition: MpObject.h:96
static std::pair< errno_t, size_t > Generate(nlib_utf8_t *str, size_t n, const MpObject &obj, const JsonStreamGeneratorSettings &settings) noexcept
Outputs the content of obj in JSON or msgpack.
#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. The default value is false...
Object created when MessagePack, JSON, or CSV is read.
static std::pair< errno_t, size_t > Generate(nlib_utf8_t(&str)[N], const MpObject &obj, const JsonStreamGeneratorSettings &settings) noexcept
A template overload of the above function.
Implements stream-related classes usually commonly used, various containers, and other gadget classes...
Definition: Base64.h:25
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:109
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
Definition: Config.h:111
static errno_t Generate(size_t *written, nlib_utf8_t *str, size_t n, const MpObject &obj) noexcept
A parameter omitted version of the above function which passes settings as the default value...
bool pretty_print
If true, outputs formatted JSON when outputting JSON. The default value is false. ...
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:250
Data structure used to store the JsonStreamGenerator settings parameters.
JsonStreamGenerator & EmptyMap() noexcept
Outputs an empty map.
The 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:303
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:37