nlib
JsonWriter.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_MSGPACK_JSONWRITER_H_
4 #define INCLUDE_NN_NLIB_MSGPACK_JSONWRITER_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
9 #undef NLIB_VIS_PUBLIC
10 #define NLIB_VIS_PUBLIC NLIB_WINEXPORT
11 #endif
12 
13 NLIB_NAMESPACE_BEGIN
14 class OutputStream;
15 namespace msgpack {
16 class MpObject;
17 
19  public:
20  enum Option {
21  OPTION_DEFAULT = 0x00000000,
22  OPTION_RELAXED = 0x00000001
23  };
24  JsonWriter() NLIB_NOEXCEPT : m_ErrorValue(0), m_Stream(NULL) {}
25  ~JsonWriter() NLIB_NOEXCEPT { this->Close(); }
26  bool Init(OutputStream* stream) NLIB_NOEXCEPT NLIB_NONNULL;
27  bool Flush() NLIB_NOEXCEPT;
28  bool Close() NLIB_NOEXCEPT;
29  errno_t GetErrorValue() const NLIB_NOEXCEPT { return m_ErrorValue; }
30 
31  public:
32  bool Write(const MpObject& obj, uint32_t option = 0) NLIB_NOEXCEPT;
33  OutputStream* GetStream() NLIB_NOEXCEPT { return m_Stream; }
34 
35  static bool Write(char* str, size_t n, const MpObject& obj,
36  uint32_t option = 0) NLIB_NOEXCEPT NLIB_NONNULL;
37  template <size_t n>
38  static bool Write(char (&str)[n], const MpObject& obj, uint32_t option = 0) NLIB_NOEXCEPT {
39  return Write(&str[0], n, obj, option);
40  }
41  NLIB_SAFE_BOOL(JsonWriter, GetErrorValue() == 0);
42 
43  private:
44  NLIB_VIS_HIDDEN bool Write_(const MpObject& obj) NLIB_NOEXCEPT;
45  void SetError(errno_t e) NLIB_NOEXCEPT {
46  if (m_ErrorValue == 0) m_ErrorValue = e;
47  }
48  NLIB_VIS_HIDDEN bool WriteString(const char* p, size_t n) NLIB_NOEXCEPT NLIB_NONNULL;
49 
50  private:
51  errno_t m_ErrorValue;
52  OutputStream* m_Stream;
54 };
55 
56 } // namespace msgpack
57 NLIB_NAMESPACE_END
58 
59 #if defined(_MSC_VER) && defined(nx_msgpack_EXPORTS)
60 #undef NLIB_VIS_PUBLIC
61 #define NLIB_VIS_PUBLIC NLIB_WINIMPORT
62 #endif
63 
64 #endif // INCLUDE_NN_NLIB_MSGPACK_JSONWRITER_H_
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
Definition: Platform.h:2151
#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
#define NLIB_VIS_HIDDEN
Symbols for functions and classes are not made available outside of the library.
Definition: Platform_unix.h:50
~JsonWriter() noexcept
Destructor.
Definition: JsonWriter.h:25
Object created when MessagePack or JSON is read.
Definition: MpObject.h:83
bool Write(BinaryWriter *w, T x)
You can write user-defined class objects by specializing this function template.
Definition: BinaryWriter.h:121
JsonWriter() noexcept
Instantiates the object with default parameters (default constructor).
Definition: JsonWriter.h:24
JSON generator. Converts an MpObject to JSON and writes it to the stream.
Definition: JsonWriter.h:18
A file that contains the configuration information for each development environment.
#define NLIB_VIS_PUBLIC
Symbols for functions and classes are made available outside of the library.
Definition: Platform_unix.h:51
Option
Defines the option values that can be passed to Write.
Definition: JsonWriter.h:20
The base class for output streams. This class cannot be instantiated.
Definition: OutputStream.h:17
int errno_t
Indicates with an int-type typedef that a POSIX error value is returned as the return value...
Definition: NMalloc.h:24