NCBI C++ ToolKit
objostrjson.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef OBJOSTRJSON__HPP
2 #define OBJOSTRJSON__HPP
3 
4 /* $Id: objostrjson.hpp 95477 2021-11-18 14:56:01Z gouriano $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Andrei Gourianov
30 *
31 * File Description:
32 * Encode data object using JSON format
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objostr.hpp>
37 #include <stack>
38 
39 
40 /** @addtogroup ObjStreamSupport
41  *
42  * @{
43  */
44 
45 
47 
48 /////////////////////////////////////////////////////////////////////////////
49 ///
50 /// CObjectOStreamJson --
51 ///
52 /// Encode serial data object using JSON format
54 {
55 public:
56 
57  /// Constructor.
58  ///
59  /// @param out
60  /// Output stream
61  /// @param deleteOut
62  /// when TRUE, the output stream will be deleted automatically
63  /// when the writer is deleted
64  /// @deprecated
65  /// Use one with EOwnership enum instead
67 
68  /// Constructor.
69  ///
70  /// @param out
71  /// Output stream
72  /// @param deleteOut
73  /// When eTakeOwnership, the output stream will be deleted automatically
74  /// when the writer is deleted
76 
77  /// Destructor.
78  virtual ~CObjectOStreamJson(void);
79 
80  /// Set default encoding of 'string' objects
81  ///
82  /// @param enc
83  /// Encoding
84  void SetDefaultStringEncoding(EEncoding enc);
85 
86  /// Get default encoding of 'string' objects
87  ///
88  /// @return
89  /// Encoding
90  EEncoding GetDefaultStringEncoding(void) const;
91 
92  /// formatting of binary data ('OCTET STRING', 'hexBinary', 'base64Binary')
94  eDefault, ///< default
95  eArray_Bool, ///< array of 'true' and 'false'
96  eArray_01, ///< array of 1 and 0
97  eArray_Uint, ///< array of unsigned integers
98  eString_Hex, ///< HEX string
99  eString_01, ///< string of 0 and 1
100  eString_01B, ///< string of 0 and 1, plus 'B' at the end
101  eString_Base64 ///< Base64Binary string
102  };
103  /// Get formatting of binary data
104  ///
105  /// @return
106  /// Formatting type
107  EBinaryDataFormat GetBinaryDataFormat(void) const;
108 
109  /// Set formatting of binary data
110  ///
111  /// @param fmt
112  /// Formatting type
113  void SetBinaryDataFormat(EBinaryDataFormat fmt);
114 
115  /// Get current stream position as string.
116  /// Useful for diagnostic and information messages.
117  ///
118  /// @return
119  /// string
120  virtual string GetPosition(void) const override;
121 
122  /// Set JSONP mode
123  /// JSONP prefix will become "function_name("
124  /// JSONP suffix will become ");"
125  void SetJsonpMode(const string& function_name);
126 
127  /// Get JSONP padding (prefix and suffix)
128  ///
129  /// @param prefix
130  /// Receives JSONP prefix
131  /// @param suffix
132  /// Receives JSONP suffix
133  void GetJsonpPadding(string* prefix, string* suffix) const;
134 
135  /// Preserve key names
136  /// By default, hyphens in key names are changed into underscores
137  /// to be compatible with JavaScript variable names
138  ///
139  /// @param preserve
140  /// when TRUE, keep key names unchanged
141  void PreserveKeyNames(bool preserve=true) {
142  m_PreserveKeys = preserve;
143  }
144 
145  virtual void WriteFileHeader(TTypeInfo type) override;
146  virtual void EndOfWrite(void) override;
147 
148 protected:
149  virtual void WriteBool(bool data) override;
150  virtual void WriteChar(char data) override;
151  virtual void WriteInt4(Int4 data) override;
152  virtual void WriteUint4(Uint4 data) override;
153  virtual void WriteInt8(Int8 data) override;
154  virtual void WriteUint8(Uint8 data) override;
155  virtual void WriteFloat(float data) override;
156  virtual void WriteDouble(double data) override;
157  void WriteDouble2(double data, unsigned digits);
158  virtual void WriteCString(const char* str) override;
159  virtual void WriteString(const string& s,
160  EStringType type = eStringTypeVisible) override;
161  virtual void WriteStringStore(const string& s) override;
162  virtual void CopyString(CObjectIStream& in,
163  EStringType type = eStringTypeVisible) override;
164  virtual void CopyStringStore(CObjectIStream& in) override;
165 
166  virtual void WriteNullPointer(void) override;
167  virtual void WriteObjectReference(TObjectIndex index) override;
168  virtual void WriteOtherBegin(TTypeInfo typeInfo) override;
169  virtual void WriteOtherEnd(TTypeInfo typeInfo) override;
170  virtual void WriteOther(TConstObjectPtr object, TTypeInfo typeInfo) override;
171 
172  virtual void WriteNull(void) override;
173  virtual void WriteAnyContentObject(const CAnyContentObject& obj) override;
174  virtual void CopyAnyContentObject(CObjectIStream& in) override;
175 
176  virtual void WriteBitString(const CBitString& obj) override;
177  virtual void CopyBitString(CObjectIStream& in) override;
178 
179  virtual void WriteEnum(const CEnumeratedTypeValues& values,
180  TEnumValueType value) override;
181  virtual void CopyEnum(const CEnumeratedTypeValues& values,
182  CObjectIStream& in) override;
183 
184 #ifdef VIRTUAL_MID_LEVEL_IO
185  virtual void WriteClassMember(const CMemberId& memberId,
186  TTypeInfo memberType,
187  TConstObjectPtr memberPtr) override;
188  virtual bool WriteClassMember(const CMemberId& memberId,
189  const CDelayBuffer& buffer) override;
190  virtual void WriteClassMemberSpecialCase(
191  const CMemberId& memberId, TTypeInfo memberType,
192  TConstObjectPtr memberPtr, ESpecialCaseWrite how) override;
193 #endif
194 
195  // low level I/O
196  virtual void BeginNamedType(TTypeInfo namedTypeInfo) override;
197  virtual void EndNamedType(void) override;
198 
199  virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
200  virtual void EndContainer(void) override;
201  virtual void BeginContainerElement(TTypeInfo elementType) override;
202  virtual void EndContainerElement(void) override;
203 
204  virtual void BeginClass(const CClassTypeInfo* classInfo) override;
205  virtual void EndClass(void) override;
206  virtual void BeginClassMember(const CMemberId& id) override;
207 
208  virtual void EndClassMember(void) override;
209 
210  virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
211  virtual void EndChoice(void) override;
212  virtual void BeginChoiceVariant(const CChoiceTypeInfo* choiceType,
213  const CMemberId& id) override;
214  virtual void EndChoiceVariant(void) override;
215 
216  virtual void BeginBytes(const ByteBlock& block) override;
217  virtual void WriteBytes(const ByteBlock& block,
218  const char* bytes, size_t length) override;
219  virtual void EndBytes(const ByteBlock& block) override;
220 
221  virtual void WriteChars(const CharBlock& block,
222  const char* chars, size_t length) override;
223 
224  // Write current separator to the stream
225  virtual void WriteSeparator(void) override;
226 
227 private:
228  void WriteBase64Bytes(const char* bytes, size_t length);
229  void WriteBytes(const char* bytes, size_t length);
230  void WriteCustomBytes(const char* bytes, size_t length);
231 
232  void WriteMemberId(const CMemberId& id);
233  void WriteSkippedMember(void);
234  void WriteEscapedChar(char c, EEncoding enc_in);
235  void WriteEncodedChar(const char*& src, EStringType type);
236  void x_WriteString(const string& value,
238  void WriteKey(const string& key);
239  void BeginValue(void);
240  void WriteValue(const string& value,
242  void WriteKeywordValue(const string& value);
243  void StartBlock(void);
244  void EndBlock(void);
245  void NextElement(void);
246  void BeginArray(void);
247  void EndArray(void);
248  void NameSeparator(void);
249 
259  size_t m_WrapAt;
260 };
261 
262 
263 /* @} */
264 
266 
267 #endif
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CDelayBuffer.
Definition: delaybuf.hpp:58
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStreamJson –.
Definition: objostrjson.hpp:54
CObjectOStream –.
Definition: objostr.hpp:83
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Include a standard set of the NCBI C++ Toolkit most basic headers.
std::ofstream out("events_result.xml")
main entry point for tests
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1209
int TEnumValueType
Definition: serialdef.hpp:232
const void * TConstObjectPtr
Definition: serialdef.hpp:59
EStringType
String type.
Definition: serialdef.hpp:185
@ eStringTypeVisible
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:186
EBinaryDataFormat
formatting of binary data ('OCTET STRING', 'hexBinary', 'base64Binary')
Definition: objostrjson.hpp:93
virtual void EndClassMember(void)
Definition: objostr.cpp:888
MLIOVIR void WriteClassMemberSpecialCase(const CMemberId &memberId, TTypeInfo memberType, TConstObjectPtr memberPtr, ESpecialCaseWrite how)
Definition: objostr.cpp:580
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
void WriteClassMember(const CConstObjectInfoMI &member)
Definition: objostr.cpp:571
virtual void EndBytes(const ByteBlock &block)
Definition: objostr.cpp:1124
virtual void WriteFileHeader(TTypeInfo type)
Definition: objostr.cpp:680
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objostr.cpp:1029
virtual void WriteUint8(Uint8 data)=0
virtual void BeginBytes(const ByteBlock &block)
Definition: objostr.cpp:1120
void PreserveKeyNames(bool preserve=true)
Preserve key names By default, hyphens in key names are changed into underscores to be compatible wit...
virtual void CopyString(CObjectIStream &in, EStringType type=eStringTypeVisible)=0
virtual void WriteOther(TConstObjectPtr object, TTypeInfo typeInfo)
Definition: objostr.cpp:786
virtual void WriteInt8(Int8 data)=0
virtual void EndClass(void)
Definition: objostr.cpp:884
virtual void WriteSeparator(void)
Definition: objostr.cpp:1181
virtual void WriteString(const string &str, EStringType type=eStringTypeVisible)=0
virtual void WriteBitString(const CBitString &obj)=0
virtual void WriteNullPointer(void)=0
virtual void EndContainerElement(void)
Definition: objostr.cpp:806
virtual void WriteChar(char data)=0
virtual void CopyAnyContentObject(CObjectIStream &in)=0
EEncoding m_StringEncoding
virtual void BeginNamedType(TTypeInfo namedTypeInfo)
Definition: objostr.cpp:734
virtual void CopyBitString(CObjectIStream &in)=0
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
virtual void WriteStringStore(const string &data)=0
virtual void WriteOtherEnd(TTypeInfo typeInfo)
Definition: objostr.cpp:794
virtual void WriteInt4(Int4 data)=0
virtual void WriteUint4(Uint4 data)=0
virtual void WriteCString(const char *str)=0
virtual void WriteObjectReference(TObjectIndex index)=0
virtual void BeginChoiceVariant(const CChoiceTypeInfo *choiceType, const CMemberId &id)=0
virtual string GetPosition(void) const override
Get current stream position as string.
Definition: objostr.cpp:510
virtual void WriteChars(const CharBlock &block, const char *chars, size_t length)=0
virtual void EndOfWrite(void)
Definition: objostr.cpp:559
EBinaryDataFormat m_BinaryFormat
virtual void CopyEnum(const CEnumeratedTypeValues &values, CObjectIStream &in)=0
virtual void WriteBytes(const ByteBlock &block, const char *bytes, size_t length)=0
virtual void CopyStringStore(CObjectIStream &in)=0
virtual void WriteFloat(float data)
Definition: objostr.cpp:721
virtual void EndChoice(void)
Definition: objostr.cpp:1032
virtual void WriteDouble(double data)=0
virtual void WriteNull(void)=0
virtual void WriteAnyContentObject(const CAnyContentObject &obj)=0
virtual void EndNamedType(void)
Definition: objostr.cpp:738
virtual void BeginContainerElement(TTypeInfo elementType)
Definition: objostr.cpp:802
virtual void BeginClassMember(const CMemberId &id)=0
virtual void WriteOtherBegin(TTypeInfo typeInfo)=0
virtual void WriteBool(bool data)=0
virtual void EndContainer(void)
Definition: objostr.cpp:798
virtual void EndChoiceVariant(void)
Definition: objostr.cpp:1035
virtual void WriteEnum(const CEnumeratedTypeValues &values, TEnumValueType value)=0
@ eString_01B
string of 0 and 1, plus 'B' at the end
@ eArray_Uint
array of unsigned integers
Definition: objostrjson.hpp:97
@ eArray_Bool
array of 'true' and 'false'
Definition: objostrjson.hpp:95
@ eArray_01
array of 1 and 0
Definition: objostrjson.hpp:96
@ eString_Hex
HEX string.
Definition: objostrjson.hpp:98
@ eString_01
string of 0 and 1
Definition: objostrjson.hpp:99
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
EEncoding
Definition: ncbistr.hpp:199
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
const struct ncbi::grid::netcache::search::fields::KEY key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
static const BitmapCharRec *const chars[]
Definition: ncbi_10x20.c:1829
std::istream & in(std::istream &in_, double &x_)
static const char * suffix[]
Definition: pcregrep.c:408
static const char * prefix[]
Definition: pcregrep.c:405
static pcre_uint8 * buffer
Definition: pcretest.c:1051
Definition: type.c:6
Modified on Mon Jun 24 05:21:06 2024 by modify_doxy.py rev. 669887