BDE 4.39.x Production Release
Loading...
Searching...
No Matches
baljsn.h
Go to the documentation of this file.
1/// @file baljsn.h
2///
3///
4/// @defgroup baljsn Package baljsn
5/// @brief Basic Application Library JSoN (baljsn)
6/// @addtogroup bal
7/// @{
8/// @addtogroup baljsn
9/// @{
10/// * <a href="#baljsn-purpose"> Purpose</a>
11/// * <a href="#baljsn-mnemonic"> Mnemonic </a>
12/// * <a href="#baljsn-description"> Description </a>
13/// * <a href="#baljsn-hierarchical-synopsis"> Hierarchical Synopsis </a>
14/// * <a href="#baljsn-component-synopsis"> Component Synopsis </a>
15/// * <a href="#baljsn-encoding-format"> Encoding Format </a>
16/// * <a href="#baljsn-validateinputisutf8-option"> validateInputIsUtf8 Option </a>
17///
18/// # Purpose {#baljsn-purpose}
19/// Provide components for encoding/decoding in the JSON format.
20///
21/// # Mnemonic {#baljsn-mnemonic}
22/// Basic Application Library JSoN (baljsn)
23///
24/// # Description {#baljsn-description}
25/// The `baljsn` package provides facilities for encoding and
26/// decoding value-semantic objects in the JSON encoding format. Currently, the
27/// encoder and decoder provided in this package work with types that support the
28/// `bdeat` framework (see the `bdlat` package for details), which is a
29/// compile-time interface for manipulating struct-like and union-like objects.
30/// Typical usage is illustrated in the @ref baljsn_encoder and @ref baljsn_decoder
31/// components documentation.
32///
33/// There are 2 sets of the encoding/decoding member functions:
34///
35/// 1. `encode` & `decode`, and
36/// 2. `encodeAny` & `decodeAny` (referred as "any-functions" below).
37///
38/// The first set uses templates and compile-time metaprogramming to generate the
39/// encoding/decoding C++ code. The second set uses the runtime dispatch
40/// approach - base classes and virtual functions - to provide the same
41/// functionality. The result does not depend on the functions you use - their
42/// functionality is absolutely identical. The difference is not in **what**
43/// they do, but in **how** they do it.
44///
45/// As usual, the compile-time approach can be faster, but generates a lot of
46/// code. And since this code is in the headers, this causes big load on the
47/// compiler and linker - the generated code is in each translation unit and in
48/// the object file generated from it. As a result, slow compilation, huge
49/// memory and CPU consumption during the compilation and linking, and a larger
50/// resulting executable file size.
51///
52/// All the above mentioned shortcomings (except the final runtime performance)
53/// can be mitigated by using the "any-functions", at the cost of a small runtime
54/// slowdown. All the encoding/decoding code is pre-compiled and located in one
55/// place - the BDE library (archive) files. But how big is the execution
56/// slowdown? Our benchmarks show that the slowdown is less than 10%.
57///
58/// ## Hierarchical Synopsis {#baljsn-hierarchical-synopsis}
59///
60/// The 'baljsn' package currently has 20 components having 8 levels of physical
61/// dependency. The list below shows the hierarchical ordering of the components.
62/// The order of components within each level is not architecturally significant,
63/// just alphabetical.
64/// @code
65/// 8. baljsn_jsonconverter
66///
67/// 7. baljsn_decoder
68///
69/// 6. baljsn_encoder
70///
71/// 5. baljsn_datumutil
72/// baljsn_encodeimplutil
73/// baljsn_jsonformatter
74/// baljsn_jsonparserutil
75///
76/// 4. baljsn_formatter
77/// baljsn_parserutil
78/// baljsn_simpleformatter
79///
80/// 3. baljsn_printutil
81///
82/// 2. baljsn_datumdecoderoptions
83/// baljsn_datumencoderoptions
84/// baljsn_decoderoptionsutil
85/// baljsn_encoderoptions
86/// baljsn_jsontokenizer
87///
88/// 1. baljsn_decoderoptions
89/// baljsn_encoder_testtypes !PRIVATE!
90/// baljsn_encodingstyle
91/// baljsn_tokenizer
92/// @endcode
93///
94/// ## Component Synopsis {#baljsn-component-synopsis}
95///
96/// @ref baljsn_datumdecoderoptions :
97/// Provide options for decoding JSON into a `Datum` object.
98///
99/// @ref baljsn_datumencoderoptions :
100/// Provide an attribute class for specifying Datum<->JSON options.
101///
102/// @ref baljsn_datumutil :
103/// Provide utilities converting between `bdld::Datum` and JSON data.
104///
105/// @ref baljsn_decoder :
106/// Provide a JSON decoder for `bdeat` compatible types.
107///
108/// @ref baljsn_decoderoptions :
109/// Provide an attribute class for specifying JSON decoding options.
110///
111/// @ref baljsn_decoderoptionsutil :
112/// Provide a utility for configuring `baljsn::DecoderOptions`.
113///
114/// @ref baljsn_encodeimplutil :
115/// Provide a utility to encode `bdlat`-compatible types as JSON.
116///
117/// @ref baljsn_encoder :
118/// Provide a JSON encoder for `bdlat`-compatible types.
119///
120/// @ref baljsn_encoder_testtypes : !PRIVATE!
121/// Provide value-semantic attribute classes
122///
123/// @ref baljsn_encoderoptions :
124/// Provide an attribute class for specifying JSON encoding options.
125///
126/// @ref baljsn_encodingstyle :
127/// Provide value-semantic attribute classes.
128///
129/// @ref baljsn_formatter :
130/// Provide a formatter for encoding data in the JSON format.
131///
132/// @ref baljsn_jsonconverter :
133/// Provide conversions between JSON and `bdlat`-compatible types.
134///
135/// @ref baljsn_jsonformatter :
136/// Provide a formatter for converting `bdlat` object to `Json` analog.
137///
138/// @ref baljsn_jsonparserutil :
139/// Provide a utility to get simple types from `bdl::json` objects
140///
141/// @ref baljsn_jsontokenizer :
142/// Provide a tokenizer for viewing parts of a `bdljsn::Json` object.
143///
144/// @ref baljsn_parserutil :
145/// Provide a utility for decoding JSON data into simple types.
146///
147/// @ref baljsn_printutil :
148/// Provide a utility for encoding simple types in the JSON format.
149///
150/// @ref baljsn_simpleformatter :
151/// Provide a simple formatter for encoding data in the JSON format.
152///
153/// @ref baljsn_tokenizer :
154/// Provide a tokenizer for extracting JSON data from a `streambuf`.
155///
156/// ## Encoding Format {#baljsn-encoding-format}
157///
158///
159///The following table provides a mapping between an element's 'bdem' elem type
160///(as specified in @ref bdem_elemtype ), its XSD type, its C++ type, its
161///corresponding JSON type, and the encoding used.
162///
163/// @code
164///BDEM type XSD type C++ Type JSON type Format
165///--------- -------- -------- --------- ------
166///BDEM_VOID N/A N/A null Encoding error
167///
168///BDEM_BOOL boolean bool true/false <BOOLEAN>
169///
170///BDEM_CHAR byte char number/string <NUMBER>
171///
172///BDEM_SHORT short short number/string <NUMBER>
173///
174/// unsignedByte unsigned char number/string <NUMBER>
175///
176///BDEM_INT int int number/string <NUMBER>
177///
178/// unsignedShort unsigned short number/string <NUMBER>
179///
180///BDEM_INT64 integer Int64 number/string <NUMBER>
181///
182/// long Int64 number/string <NUMBER>
183///
184/// unsignedInt unsigned int number/string <NUMBER>
185///
186/// unsignedLong unsigned Uint64 number/string <NUMBER>
187///
188///BDEM_FLOAT float float number/string <DOUBLE>
189///
190///BDEM_DOUBLE decimal double number/string <DOUBLE>
191///
192/// double double number/string <DOUBLE>
193///
194///BDEM_STRING string bsl::string string <STRING>
195///
196///BDEM_DATETIME dateTime bdlt::Datetime string "<DATETIME>"
197///
198///BDEM_DATETIMETZ dateTime bdlt::DatetimeTz string "<DATETIMETZ>"
199///
200///BDEM_DATE date bdlt::Date string "<DATE>"
201///
202///BDEM_DATETZ date bdlt::DateTz string "<DATETZ>"
203///
204///BDEM_TIME time bdlt::Time string "<TIME>"
205///
206///BDEM_TIMETZ time bdlt::TimeTz string "<TIMETZ>"
207///
208///BDEM_CHAR_ARRAY base64Binary vector<char> string "<BASE64STR>"
209///
210/// hexBinary vector<char> string "<BASE64STR>"
211///
212///BDEM_TYPE_ARRAY maxOccurs > 1 vector<TYPE> array <SIMPLE_ARRAY>
213///
214///BDEM_LIST sequence bcem_Aggregate object <SEQUENCE>
215///
216///BDEM_TABLE maxOccurs > 1 bcem_Aggregate array of objs <SEQ_ARRAY>
217///
218///BDEM_CHOICE choice bcem_Aggregate object <CHOICE>
219///
220///BDEM_CHOICE_ARRAY maxOccurs > 1 bcem_Aggregate array of objs <CHOICE_ARRAY>
221///
222///BDEM_INT enumeration C++ enumeration string/number "<NUMBER>"
223///
224///BDEM_STRING enumeration C++ enumeration string <STRING>
225///
226///BDEM_* minOccurs = 0 NullableValue null <NULL_VALUE>
227/// @endcode
228///* The exact syntax of the format is specified below.
229///
230///* BDEM_TYPE_ARRAY refers to all the types supported by bdem (such as
231///BDEM_INT_ARRAY, BDEM_STRING_ARRAY etc) including BDEM_CHAR_ARRAY. A
232///'vector<char>' that is not specified with the 'xs:base64Binary' or
233///'xs:hexBinary' is treated similarly to vector of any scalar type. Vectors of
234///nullable scalar types (specified via the 'xs:nillable' attribute) are encoded
235///similar to their vector of non-nullable scalar types except that their
236///elements could also be specified as 'null'.
237///
238///* Integral types are always encoded in the <NUMBER> format and int-valued
239///enumerations are always encoded in the "<NUMBER>" format, but during decoding
240///both of these formats are supported for both of these categories of types.
241///This flexibility applies to all integral types, including ones that do not
242///support an enumeration constraint.
243///
244///The format grammar specified below uses the Extended BNF notation (except that
245///',' is not used for concatenation to enhance readability). A quick reference
246///of EBNF is provided below (refer here for more details):
247///
248/// @code
249///| is used to select between alternate options
250///[ ... ] (square brackets) are used to specify an optional item
251///{ ... } (curly brackets) are used to specify an item that can be repeated zero
252/// or more times
253///( ... ) (parenthesis) are used to group elements
254///
255///<VOID> Results in an encoding error
256///
257///<BOOLEAN> 'true' | 'false';
258///
259///<STRING> Same as the spec for 'string' on http://www.json.org;
260///
261///<NUMBER> Same as the spec for 'number' on http://www.json.org;
262///
263///<DOUBLE_STRING> "NaN" | "+INF" | "-INF";
264///
265///<DOUBLE> <NUMBER> | <DOUBLE_STRING>; (1)
266///
267///<DATE> <YEAR>'-'<MONTH>'-'<DAY>; (2)
268///
269///<DATETZ> <DATE><TZ>;
270///
271///<TIME> <HOUR>':'<MINUTES>':'<SECONDS>[<MILLISEC>]; (2)
272///
273///<TIMETZ> <TIME><TZ>;
274///
275///<DATETIME> <DATE>'T'<TIME>; (2)
276///
277///<DATETIMETZ> <DATETIME><TZ>;
278///
279///<BASE64STR> Strings encoded in base 64 encoding;
280///
281///<SEQUENCE> '{' '}' | '{' <MEMBER> {',' <MEMBER> } '}';
282///
283///<CHOICE> '{' <MEMBER> '}';
284///
285///<SIMPLE_ARRAY> '[' ']' | '[' <VALUE> {',' <VALUE> } ']';
286///
287///<SEQ_ARRAY> '[' ']' | '[' <SEQUENCE> {',' <SEQUENCE> } ']';
288///
289///<CHOICE_ARRAY> '[' ']' | '[' <CHOICE> {',' <CHOICE> } ']';
290///
291///<NULL> 'null';
292///
293///<NULL_VALUE> <NULL> | ''; (3)
294///
295///<NAME> <STRING>; (4)
296///
297///<SIMPLE> <NUMBER> | <STRING> | '"' <DATE> '"' | '"' <DATETZ> '"'
298/// | '"' <TIME> '"' | '"' <TIMETZ> '"'
299/// | '"' <DATETIME> '"' | '"' <DATETIMETZ> '"'
300/// | '"' <BASE64STR> '"';
301///
302///<VALUE> <NULL> | <SIMPLE> | <SEQUENCE> | <CHOICE> | <SIMPLE_ARRAY>
303/// | <COMPLEX_ARRAY>;
304///
305///<MEMBER> <NAME>':'<VALUE>;
306///
307///<SIGN> '+' | '-';
308///
309///<POSITIVE_DIGIT> '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
310///
311///<DIGIT> '0' | <POSITIVE_DIGIT>;
312///
313///<YEAR> <DIGIT><DIGIT><DIGIT><POSITIVE_DIGIT>;
314///
315///<MONTH> '0' <POSITIVE_DIGIT> | '1' ( '0' | '1' | '2' );
316///
317///<DAY> ( '0' | '1' | '2' ) <POSITIVE_DIGIT> | '3' ( '0' | '1' );
318///
319///<HOUR> ( '0' | '1' ) <DIGIT> | '2' ( '0' | '1' | '2' | '3');
320///
321///<MINUTES> ( '0' | '1' | '2' | '3' | '4' | '5' ) <DIGIT>;
322///
323///<SECONDS> ( '0' | '1' | '2' | '3' | '4' | '5' ) <DIGIT>;
324///
325///<MILLISEC> '.' <DIGIT> { <DIGIT> };
326///
327///<TZ> <SIGN><HOUR>':'<MINUTES> | 'Z' | 'z';
328/// @endcode
329///
330///(1) Double types (float, decimal, and double) are encoded in the number format
331///by default with the values NaN, +INF and -INF resulting in an encoding error.
332///These values can be printed as strings by setting the
333///'encodeInfAndNaNAsStrings' encoder option to 'true'.
334///
335///(2) In practice only the timezone-enabled components are used by
336///bcem_Aggregate and generated types. The supported format is a subset of the
337///ISO 8601 standard. For further details refer to the @ref bdepu_iso8601
338///component.
339///
340///(3) Null values are not encoded on the wire by default. The
341///'encodeNullElements' options can be set to 'true' to ensure that null values
342///are encoded.
343///
344///(4) The name of an element corresponds to that element's name in the provided
345///xsd.
346///
347/// ## validateInputIsUtf8 Option {#baljsn-validateinputisutf8-option}
348///
349///The 'baljsn::DecoderOption' parameter of the 'decode' function has a
350///configuration option named 'validateInputIsUtf8'. If this option is 'true',
351///the 'decode' function will succeed only if the encoding of the JSON data is
352///UTF-8, which the JSON specification requires. If the option is 'false',
353///'decode' will not validate that the encoding of the JSON data is UTF-8, and
354///may succeed even if the data does not satisfy the UTF-8 validity requirement
355///of the JSON specification. This option primarily affects the acceptance of
356///string literals, which are the parts of JSON documents that may have
357///rational justification for having non-UTF-8, and therefore invalid, content.
358///
359///Ideally, users *should* set 'validateInputIsUtf8' to 'true'. However, some
360///legacy applications currently might be trafficking in JSON that contains
361///non-UTF-8 with no adverse effects to their clients. Consequently, this
362///option is 'false' by default to maintain backward compatibility.
363///
364/// @}
365/** @} */