1 #ifndef OBJISTRJSON__HPP
2 #define OBJISTRJSON__HPP
3 
4 /*  $Id: objistrjson.hpp 552042 2017-11-28 17:25:31Z 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 *   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 
46 BEGIN_NCBI_SCOPE
47 
48 /////////////////////////////////////////////////////////////////////////////
49 ///
50 /// CObjectIStreamJson --
51 ///
52 /// Decode serial data object using JSON format
53 class NCBI_XSERIAL_EXPORT CObjectIStreamJson : public CObjectIStream
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
66     CObjectIStreamJson(CNcbiIstream& in, EOwnership deleteIn);
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 position as string.
76     /// Useful for diagnostic and information messages.
77     ///
78     /// @return
79     ///   string
80     virtual string GetPosition(void) 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')
97     enum EBinaryDataFormat {
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);
230     string ReadValue(EStringType type = eStringTypeVisible);
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 
241     bool m_FileHeader;
242     bool m_BlockStart;
243     bool m_ExpectValue;
244     bool m_GotNameless;
245     char m_Closing;
246     EEncoding m_StringEncoding;
247     string m_LastTag;
248     string m_RejectedTag;
249     EBinaryDataFormat m_BinaryFormat;
250     CStringUTF8 m_Utf8Buf;
251     CStringUTF8::const_iterator m_Utf8Pos;
252 };
253 
254 /* @} */
255 
256 END_NCBI_SCOPE
257 
258 #endif
259