BDE 4.14.0 Production release
Loading...
Searching...
No Matches
baljsn_tokenizer.h
Go to the documentation of this file.
1/// @file baljsn_tokenizer.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// baljsn_tokenizer.h -*-C++-*-
8#ifndef INCLUDED_BALJSN_TOKENIZER
9#define INCLUDED_BALJSN_TOKENIZER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup baljsn_tokenizer baljsn_tokenizer
15/// @brief Provide a tokenizer for extracting JSON data from a `streambuf`.
16/// @addtogroup bal
17/// @{
18/// @addtogroup baljsn
19/// @{
20/// @addtogroup baljsn_tokenizer
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#baljsn_tokenizer-purpose"> Purpose</a>
25/// * <a href="#baljsn_tokenizer-classes"> Classes </a>
26/// * <a href="#baljsn_tokenizer-description"> Description </a>
27/// * <a href="#baljsn_tokenizer-usage"> Usage </a>
28/// * <a href="#baljsn_tokenizer-example-1-extracting-json-data-into-an-object"> Example 1: Extracting JSON Data into an Object </a>
29///
30/// # Purpose {#baljsn_tokenizer-purpose}
31/// Provide a tokenizer for extracting JSON data from a `streambuf`.
32///
33/// # Classes {#baljsn_tokenizer-classes}
34///
35/// - baljsn::Tokenizer: tokenizer for parsing JSON data from a `streambuf`
36///
37/// @see baljsn_decoder, baljsn_parserutil
38///
39/// # Description {#baljsn_tokenizer-description}
40/// This component provides a class, `baljsn::Tokenizer`, that
41/// traverses data stored in a `bsl::streambuf` one node at a time and provides
42/// clients access to the data associated with that node, including its type and
43/// data value. Client code can use the `reset` function to associate a
44/// `bsl::streambuf` containing JSON data with a tokenizer object and then call
45/// the `advanceToNextToken` function to extract individual data values.
46///
47/// This `class` was created to be used by other components in the `baljsn`
48/// package and in most cases clients should use the @ref baljsn_decoder component
49/// instead of using this `class`.
50///
51/// On malformed JSON, tokenization may fail before the end of input is reached,
52/// but not all such errors are detected. In particular, callers should check
53/// that closing brackets and braces match opening ones.
54///
55/// ## Usage {#baljsn_tokenizer-usage}
56///
57///
58/// This section illustrates intended use of this component.
59///
60/// ## Example 1: Extracting JSON Data into an Object {#baljsn_tokenizer-example-1-extracting-json-data-into-an-object}
61///
62///
63/// For this example, we will use `baljsn::Tokenizer` to read each node in a
64/// JSON document and populate a simple `Employee` object.
65///
66/// First, we will define the JSON data that the tokenizer will traverse over:
67/// @code
68/// const char *INPUT = " {\n"
69/// " \"street\" : \"Lexington Ave\",\n"
70/// " \"state\" : \"New York\",\n"
71/// " \"zipcode\" : 10022\n"
72/// " }";
73/// @endcode
74/// Next, we will construct populate a `streambuf` with this data:
75/// @code
76/// bdlsb::FixedMemInStreamBuf isb(INPUT, bsl::strlen(INPUT));
77/// @endcode
78/// Then, we will create a `baljsn::Tokenizer` object and associate the above
79/// streambuf with it:
80/// @code
81/// baljsn::Tokenizer tokenizer;
82/// tokenizer.reset(&isb);
83/// @endcode
84/// Next, we will create an address record type and object.
85/// @code
86/// struct Address {
87/// bsl::string d_street;
88/// bsl::string d_state;
89/// int d_zipcode;
90/// } address = { "", "", 0 };
91/// @endcode
92/// Then, we will traverse the JSON data one node at a time:
93/// @code
94/// // Read '{'
95///
96/// int rc = tokenizer.advanceToNextToken();
97/// assert(!rc);
98///
99/// baljsn::Tokenizer::TokenType token = tokenizer.tokenType();
100/// assert(baljsn::Tokenizer::e_START_OBJECT == token);
101///
102/// rc = tokenizer.advanceToNextToken();
103/// assert(!rc);
104/// token = tokenizer.tokenType();
105///
106/// // Continue reading elements till '}' is encountered
107///
108/// while (baljsn::Tokenizer::e_END_OBJECT != token) {
109/// assert(baljsn::Tokenizer::e_ELEMENT_NAME == token);
110///
111/// // Read element name
112///
113/// bslstl::StringRef nodeValue;
114/// rc = tokenizer.value(&nodeValue);
115/// assert(!rc);
116///
117/// bsl::string elementName = nodeValue;
118///
119/// // Read element value
120///
121/// int rc = tokenizer.advanceToNextToken();
122/// assert(!rc);
123///
124/// token = tokenizer.tokenType();
125/// assert(baljsn::Tokenizer::e_ELEMENT_VALUE == token);
126///
127/// rc = tokenizer.value(&nodeValue);
128/// assert(!rc);
129///
130/// // Extract the simple type with the data
131///
132/// if (elementName == "street") {
133/// rc = baljsn::ParserUtil::getValue(&address.d_street, nodeValue);
134/// assert(!rc);
135/// }
136/// else if (elementName == "state") {
137/// rc = baljsn::ParserUtil::getValue(&address.d_state, nodeValue);
138/// assert(!rc);
139/// }
140/// else if (elementName == "zipcode") {
141/// rc = baljsn::ParserUtil::getValue(&address.d_zipcode, nodeValue);
142/// assert(!rc);
143/// }
144///
145/// rc = tokenizer.advanceToNextToken();
146/// assert(!rc);
147/// token = tokenizer.tokenType();
148/// }
149/// @endcode
150/// Finally, we will verify that the `address` aggregate has the correct values:
151/// @code
152/// assert("Lexington Ave" == address.d_street);
153/// assert("New York" == address.d_state);
154/// assert(10022 == address.d_zipcode);
155/// @endcode
156/// @}
157/** @} */
158/** @} */
159
160/** @addtogroup bal
161 * @{
162 */
163/** @addtogroup baljsn
164 * @{
165 */
166/** @addtogroup baljsn_tokenizer
167 * @{
168 */
169
170#include <balscm_version.h>
171
172#include <bdljsn_tokenizer.h>
173
175
176#include <bsls_alignedbuffer.h>
177#include <bsls_assert.h>
178#include <bsls_types.h>
179
180#include <bsl_ios.h>
181#include <bsl_streambuf.h>
182#include <bsl_string.h>
183#include <bsl_string_view.h>
184#include <bsl_vector.h>
185
186
187namespace baljsn {
188
189 // ===============
190 // class Tokenizer
191 // ===============
192
194
195} // close package namespace
196
197
198#endif
199
200// ----------------------------------------------------------------------------
201// Copyright 2015 Bloomberg Finance L.P.
202//
203// Licensed under the Apache License, Version 2.0 (the "License");
204// you may not use this file except in compliance with the License.
205// You may obtain a copy of the License at
206//
207// http://www.apache.org/licenses/LICENSE-2.0
208//
209// Unless required by applicable law or agreed to in writing, software
210// distributed under the License is distributed on an "AS IS" BASIS,
211// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
212// See the License for the specific language governing permissions and
213// limitations under the License.
214// ----------------------------- END-OF-FILE ----------------------------------
215
216/** @} */
217/** @} */
218/** @} */
Definition bdljsn_tokenizer.h:234
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition baljsn_datumdecoderoptions.h:113
bdljsn::Tokenizer Tokenizer
Definition baljsn_tokenizer.h:193