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