BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdljsn_jsonliterals.h
Go to the documentation of this file.
1/// @file bdljsn_jsonliterals.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdljsn_jsonliterals.h -*-C++-*-
8#ifndef INCLUDED_BDLJSN_JSONLITERALS
9#define INCLUDED_BDLJSN_JSONLITERALS
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdljsn_jsonliterals bdljsn_jsonliterals
15/// @brief Provide user-defined literals for `bdljsn::Json` objects.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdljsn
19/// @{
20/// @addtogroup bdljsn_jsonliterals
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdljsn_jsonliterals-purpose"> Purpose</a>
25/// * <a href="#bdljsn_jsonliterals-classes"> Classes </a>
26/// * <a href="#bdljsn_jsonliterals-description"> Description </a>
27/// * <a href="#bdljsn_jsonliterals-use-of-the-global-allocator"> Use of the Global Allocator </a>
28/// * <a href="#bdljsn_jsonliterals-usage"> Usage </a>
29/// * <a href="#bdljsn_jsonliterals-example-1-creating-a-bdljsn-json-object-with-a-user-defined-literal"> Example 1: Creating a bdljsn::Json Object with a User Defined Literal </a>
30///
31/// # Purpose {#bdljsn_jsonliterals-purpose}
32/// Provide user-defined literals for `bdljsn::Json` objects.
33///
34/// # Classes {#bdljsn_jsonliterals-classes}
35///
36/// - bdljsn::JsonLiterals: a namespace for user-defined literal operators
37///
38/// # Description {#bdljsn_jsonliterals-description}
39/// This component provides a namespace, `bdljsn::JsonLiterals`, in
40/// which operators for the user-defined literal suffix "_json" are defined.
41/// Users can define a using declaration for the `JsonLiterals` namespace, and
42/// apply the "_json" suffix to literal text containing JSON to create
43/// `bdljsn::Json` objects (on platforms that support user defined literals).
44///
45/// For example:
46/// @code
47/// using namespace bdljsn::JsonLiterals;
48/// bdljsn::Json json = R"({"price": 2.1})"_json;
49/// @endcode
50///
51/// ## Use of the Global Allocator {#bdljsn_jsonliterals-use-of-the-global-allocator}
52///
53///
54/// The `operator "" _json` returns a `bdljsn::Json` object that allocates
55/// memory using the currently installed global allocator. Using the global
56/// allocator prevents inadvertently locking the default allocator, as may
57/// happen before `main` when using a JSON literal to initialize an object at
58/// global file-scope static storage duration. Note that while file scoped
59/// static objects can be useful in testing, we discourage their use in
60/// production code.
61///
62/// ## Usage {#bdljsn_jsonliterals-usage}
63///
64///
65/// This section illustrates the intended use of this component.
66///
67/// ### Example 1: Creating a bdljsn::Json Object with a User Defined Literal {#bdljsn_jsonliterals-example-1-creating-a-bdljsn-json-object-with-a-user-defined-literal}
68///
69///
70/// This component provides a namespace in which user-defined literal operators
71/// for `bdljsn::Json` are defined. In this example we use a `_json` literal
72/// to initialize a `bdljsn::Json` object.
73///
74/// First, we define a user declaration for the appropriate namespace:
75/// @code
76/// using namespace bdljsn::JsonLiterals;
77/// @endcode
78/// Then we create a `bdljsn::Json` object:
79/// @code
80/// bdljsn::Json json = R"({ "number": 4, "array": [0, 2, null] })"_json;
81///
82/// assert(bdljsn::JsonType::e_NUMBER == json["number"].type());
83/// assert(bdljsn::JsonType::e_ARRAY == json["array"].type());
84/// @endcode
85/// Notice that the user-defined literal operator will unconditionally invoke
86/// the `bsls::Assert` handler if the literal text is not valid JSON.
87/// @}
88/** @} */
89/** @} */
90
91/** @addtogroup bdl
92 * @{
93 */
94/** @addtogroup bdljsn
95 * @{
96 */
97/** @addtogroup bdljsn_jsonliterals
98 * @{
99 */
100
101#include <bdlscm_version.h>
102
103#include <bdljsn_json.h>
104
106
107
108namespace bdljsn {
109
110#if defined(BSLS_COMPILERFEATURES_SUPPORT_INLINE_NAMESPACE) && \
111 defined(BSLS_COMPILERFEATURES_SUPPORT_USER_DEFINED_LITERALS)
112inline namespace literals {
113inline namespace JsonLiterals {
114/// Return a `bdljsn::Json` object having the value of the JSON described in
115/// the specified `text` of the specified `numBytes`. If `text` is not a
116/// valid JSON document then invoke the currently installed `bsls::Assert`
117/// failure handler.
118bdljsn::Json operator "" _json (const char *text, bsl::size_t numBytes);
119
120} // close JsonLiterals namespace
121} // close literals namespace
122#endif
123
124
125// ============================================================================
126// INLINE DEFINITIONS
127// ============================================================================
128
129
130
131} // close package namespace
132
133
134#endif // INCLUDED_BDLJSN_JSONLITERALS
135
136// ----------------------------------------------------------------------------
137// Copyright 2022 Bloomberg Finance L.P.
138//
139// Licensed under the Apache License, Version 2.0 (the "License");
140// you may not use this file except in compliance with the License.
141// You may obtain a copy of the License at
142//
143// http://www.apache.org/licenses/LICENSE-2.0
144//
145// Unless required by applicable law or agreed to in writing, software
146// distributed under the License is distributed on an "AS IS" BASIS,
147// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148// See the License for the specific language governing permissions and
149// limitations under the License.
150// ----------------------------- END-OF-FILE ----------------------------------
151
152/** @} */
153/** @} */
154/** @} */
Definition bdljsn_json.h:1110
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdljsn_error.h:143