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

Go to the SVN repository for this file.

1 #ifndef OBJISTRJSON__HPP
2 #define OBJISTRJSON__HPP
3 
4 /* $Id: objistrjson.hpp 97030 2022-06-10 12:57:23Z ludwigf $
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 * Decode data object using JSON format
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objistr.hpp>
37 //#include <stack>
38 
39 
40 /** @addtogroup ObjStreamSupport
41  *
42  * @{
43  */
44 
45 
47 
48 /////////////////////////////////////////////////////////////////////////////
49 ///
50 /// CObjectIStreamJson --
51 ///
52 /// Decode serial data object using JSON format
54 {
55 public:
56  CObjectIStreamJson(void);
57  ~CObjectIStreamJson(void);
58 
59  /// Constructor.
60  ///
61  /// @param in
62  /// input stream
63  /// @param deleteIn
64  /// When eTakeOwnership, the input stream will be deleted automatically
65  /// when the reader is deleted
67 
68  /// Check if there is still some meaningful data that can be read;
69  /// this function will skip white spaces and comments
70  ///
71  /// @return
72  /// TRUE if there is no more data
73  virtual bool EndOfData(void) override;
74 
75  /// Get current stream location as tuple (positiontype:string, size_t).
76  /// Useful for customized diagnostic and information messages.
77  /// The meaning of positiontype depends on the implementation and coincides
78  /// with the prefix of the string returned by GetPosition().
79  ///
80  virtual void Location(string&, size_t&) const override;
81 
82  /// Set default encoding of 'string' objects
83  /// If data encoding is different, string will be converted to
84  /// this encoding
85  ///
86  /// @param enc
87  /// Encoding
88  void SetDefaultStringEncoding(EEncoding enc);
89 
90  /// Get default encoding of 'string' objects
91  ///
92  /// @return
93  /// Encoding
94  EEncoding GetDefaultStringEncoding(void) const;
95 
96  /// formatting of binary data ('OCTET STRING', 'hexBinary', 'base64Binary')
98  eDefault, ///< default
99  eArray_Bool, ///< array of 'true' and 'false'
100  eArray_01, ///< array of 1 and 0
101  eArray_Uint, ///< array of unsigned integers
102  eString_Hex, ///< HEX string
103  eString_01, ///< string of 0 and 1
104  eString_01B, ///< string of 0 and 1, plus 'B' at the end
105  eString_Base64 ///< Base64Binary string
106  };
107  /// Get formatting of binary data
108  ///
109  /// @return
110  /// Formatting type
111  EBinaryDataFormat GetBinaryDataFormat(void) const;
112 
113  /// Set formatting of binary data
114  ///
115  /// @param fmt
116  /// Formatting type
117  void SetBinaryDataFormat(EBinaryDataFormat fmt);
118 
119  virtual string ReadFileHeader(void) override;
120 
121 protected:
122 
123  virtual void EndOfRead(void) override;
124 
125  virtual bool ReadBool(void) override;
126  virtual void SkipBool(void) override;
127 
128  virtual char ReadChar(void) override;
129  virtual void SkipChar(void) override;
130 
131  virtual Int8 ReadInt8(void) override;
132  virtual Uint8 ReadUint8(void) override;
133 
134  virtual void SkipSNumber(void) override;
135  virtual void SkipUNumber(void) override;
136 
137  virtual double ReadDouble(void) override;
138 
139  virtual void SkipFNumber(void) override;
140 
141  virtual void ReadString(string& s,
142  EStringType type = eStringTypeVisible) override;
143  virtual void SkipString(EStringType type = eStringTypeVisible) override;
144 
145  virtual void ReadNull(void) override;
146  virtual void SkipNull(void) override;
147 
148  virtual void ReadAnyContentObject(CAnyContentObject& obj) override;
149  void SkipAnyContent(void);
150  virtual void SkipAnyContentObject(void) override;
151 
152  virtual void ReadBitString(CBitString& obj) override;
153  virtual void SkipBitString(void) override;
154 
155  virtual void SkipByteBlock(void) override;
156 
157  virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues& values) override;
158 
159 #ifdef VIRTUAL_MID_LEVEL_IO
160  virtual void ReadClassSequential(const CClassTypeInfo* classType,
161  TObjectPtr classPtr) override;
162  virtual void SkipClassSequential(const CClassTypeInfo* classType) override;
163 #endif
164 
165  // container
166  virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
167  virtual void EndContainer(void) override;
168  virtual bool BeginContainerElement(TTypeInfo elementType) override;
169  virtual void EndContainerElement(void) override;
170 
171  // class
172  virtual void BeginClass(const CClassTypeInfo* classInfo) override;
173  virtual void EndClass(void) override;
174 
175  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType) override;
176  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType,
177  TMemberIndex pos) override;
178  virtual void EndClassMember(void) override;
179  virtual void UndoClassMember(void) override;
180 
181  // choice
182  virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
183  virtual void EndChoice(void) override;
184  virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo* choiceType) override;
185  virtual void EndChoiceVariant(void) override;
186 
187  // byte block
188  virtual void BeginBytes(ByteBlock& block) override;
189  int GetHexChar(void);
190  int GetBase64Char(void);
191  virtual size_t ReadBytes(ByteBlock& block, char* buffer, size_t count) override;
192  virtual void EndBytes(const ByteBlock& block) override;
193 
194  // char block
195  virtual void BeginChars(CharBlock& block) override;
196  virtual size_t ReadChars(CharBlock& block, char* buffer, size_t count) override;
197  virtual void EndChars(const CharBlock& block) override;
198 
199  virtual EPointerType ReadPointerType(void) override;
200  virtual TObjectIndex ReadObjectPointer(void) override;
201  virtual string ReadOtherPointer(void) override;
202 
203  virtual void ResetState(void) override;
204 
205 private:
206 
207  char GetChar(void);
208  char PeekChar(void);
209  char GetChar(bool skipWhiteSpace);
210  char PeekChar(bool skipWhiteSpace);
211 
212  void SkipEndOfLine(char c);
213  char SkipWhiteSpace(void);
214  char SkipWhiteSpaceAndGetChar(void);
215 
216  bool GetChar(char c, bool skipWhiteSpace = false);
217  void Expect(char c, bool skipWhiteSpace = false);
218  void UnexpectedMember(const CTempString& id, const CItemsInfo& items);
219  template<typename Type>
220  Type x_UseMemberDefault(void);
221 
222  int ReadEscapedChar(bool* encoded=0);
223  char ReadEncodedChar(EStringType type, bool& encoded);
224  TUnicodeSymbol ReadUtf8Char(char c);
225  string x_ReadString(EStringType type);
226  void x_ReadData(string& data, EStringType type = eStringTypeUTF8);
227  bool x_ReadDataAndCheck(string& data, EStringType type = eStringTypeUTF8);
228  void x_SkipData(void);
229  string ReadKey(void);
231 
232  void StartBlock(char expect);
233  void EndBlock(char expect);
234  bool NextElement(void);
235 
236  TMemberIndex FindDeep(const CItemsInfo& items, const CTempString& name, bool& deep) const;
237  size_t ReadCustomBytes(ByteBlock& block, char* buffer, size_t count);
238  size_t ReadBase64Bytes(ByteBlock& block, char* buffer, size_t count);
239  size_t ReadHexBytes(ByteBlock& block, char* buffer, size_t count);
240 
245  char m_Closing;
247  string m_LastTag;
251  CStringUTF8::const_iterator m_Utf8Pos;
252 };
253 
254 /* @} */
255 
257 
258 #endif
void ReadValue(CStreamBuffer &b)
Definition: test.cpp:302
char SkipWhiteSpace(CStreamBuffer &b)
Definition: test.cpp:78
void SkipEndOfLine(CStreamBuffer &b, char lastChar)
Definition: test.cpp:32
int GetHexChar(CStreamBuffer &b)
Definition: test.cpp:235
char SkipWhiteSpaceAndGetChar(CStreamBuffer &b)
Definition: test.cpp:110
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CObjectIStreamJson –.
Definition: objistrjson.hpp:54
CObjectIStream –.
Definition: objistr.hpp:93
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
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.
static const char * expect
Definition: tables.c:54
char data[12]
Definition: iconv.c:80
void * TObjectPtr
Definition: serialdef.hpp:55
int TEnumValueType
Definition: serialdef.hpp:232
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
EStringType
String type.
Definition: serialdef.hpp:185
@ eStringTypeUTF8
UTF8-encoded string.
Definition: serialdef.hpp:187
@ eStringTypeVisible
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:186
virtual void ReadString(string &s, EStringType type=eStringTypeVisible)=0
virtual EPointerType ReadPointerType(void)=0
virtual bool ReadBool(void)=0
virtual void EndClassMember(void)
Definition: objistr.cpp:1372
virtual void Location(string &, size_t &) const
Get current stream location as tuple (positiontype:string, size_t).
Definition: objistr.cpp:815
MLIOVIR void ReadClassSequential(const CClassTypeInfo *classType, TObjectPtr classPtr)
Definition: objistr.cpp:1398
virtual void EndContainerElement(void)
Definition: objistr.cpp:1314
virtual void ReadNull(void)=0
virtual void UndoClassMember(void)
Definition: objistr.hpp:996
virtual void EndContainer(void)=0
virtual void EndBytes(const ByteBlock &block)
Definition: objistr.cpp:1671
CStringUTF8 m_Utf8Buf
virtual void SkipByteBlock(void)=0
virtual bool EndOfData(void)
Check if there is still some meaningful data that can be read; in text streams this function will ski...
Definition: objistr.cpp:588
virtual TMemberIndex BeginClassMember(const CClassTypeInfo *classType)=0
virtual size_t ReadChars(CharBlock &block, char *buffer, size_t count)=0
virtual string ReadOtherPointer(void)=0
virtual void SkipString(EStringType type=eStringTypeVisible)=0
virtual void SkipFNumber(void)=0
virtual void BeginBytes(ByteBlock &block)=0
virtual double ReadDouble(void)=0
virtual string ReadFileHeader(void)
Read file header.
Definition: objistr.cpp:1121
virtual void ResetState(void) override
Definition: objistr.cpp:486
CStringUTF8::const_iterator m_Utf8Pos
virtual void SkipBool(void)=0
virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo *choiceType)=0
virtual void SkipSNumber(void)=0
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
virtual void SkipUNumber(void)=0
EEncoding m_StringEncoding
virtual void SkipNull(void)=0
virtual void BeginChars(CharBlock &block)=0
EBinaryDataFormat m_BinaryFormat
virtual char ReadChar(void)=0
virtual void ReadAnyContentObject(CAnyContentObject &obj)=0
virtual size_t ReadBytes(ByteBlock &block, char *buffer, size_t count)=0
virtual void ReadBitString(CBitString &obj)=0
virtual void SkipBitString(void)=0
virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues &values)=0
virtual bool BeginContainerElement(TTypeInfo elementType)=0
virtual Int8 ReadInt8(void)=0
virtual TObjectIndex ReadObjectPointer(void)=0
MLIOVIR void SkipClassSequential(const CClassTypeInfo *classType)
Definition: objistr.cpp:1451
virtual void SkipAnyContentObject(void)=0
virtual Uint8 ReadUint8(void)=0
EBinaryDataFormat
formatting of binary data ('OCTET STRING', 'hexBinary', 'base64Binary')
Definition: objistrjson.hpp:97
virtual void EndClass(void)
Definition: objistr.cpp:1368
virtual void EndChoiceVariant(void)
Definition: objistr.cpp:1478
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
virtual void EndChars(const CharBlock &block)
Definition: objistr.cpp:1675
virtual void EndChoice(void)
Definition: objistr.cpp:1475
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1472
virtual void EndOfRead(void)
Definition: objistr.cpp:903
virtual void SkipChar(void)=0
@ eArray_Uint
array of unsigned integers
@ eArray_01
array of 1 and 0
@ eArray_Bool
array of 'true' and 'false'
Definition: objistrjson.hpp:99
@ eString_01
string of 0 and 1
@ eString_01B
string of 0 and 1, plus 'B' at the end
@ eString_Hex
HEX string.
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
std::string CStringUTF8
Definition: ncbistl.hpp:254
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
EEncoding
Definition: ncbistr.hpp:199
char32_t TUnicodeSymbol
Unicode character.
Definition: ncbistr.hpp:141
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
std::istream & in(std::istream &in_, double &x_)
#define count
static uint8_t * buffer
Definition: pcre2test.c:1016
Definition: type.c:6
#define Type
Modified on Fri Sep 20 14:57:27 2024 by modify_doxy.py rev. 669887