Google Cloud Spanner C++ Client 2.13.0
A C++ Client Library for Google Cloud Spanner
Loading...
Searching...
No Matches
json.h
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_JSON_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_JSON_H
17
18#include "google/cloud/spanner/version.h"
19#include <ostream>
20#include <string>
21
22namespace google {
23namespace cloud {
24namespace spanner {
25GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
26
27/**
28 * A simple representation for the Spanner JSON type: a lightweight,
29 * text-based, language-independent data interchange format. JSON (the
30 * JavaScript Object Notation) defines a small set of formatting rules
31 * for the portable representation of structured data. See RFC 7159.
32 *
33 * A `Json` value can be constructed from, and converted to a `std::string`.
34 * `Json` values can be compared (by string) for equality, and streamed.
35 *
36 * There is no syntax checking of JSON strings in this interface. The user
37 * is expected to only construct `Json` values from well-formatted strings.
38 */
39class Json {
40 public:
41 /// A null value.
42 Json() : rep_("null") {}
43
44 /// @name Regular value type, supporting copy, assign, move.
45 ///@{
46 Json(Json const&) = default;
47 Json& operator=(Json const&) = default;
48 Json(Json&&) = default;
49 Json& operator=(Json&&) = default;
50 ///@}
51
52 /**
53 * Construction from a JSON-formatted string. Note that there is no check
54 * here that the argument string is indeed well-formatted. Error detection
55 * will be delayed until the value is passed to Spanner.
56 */
57 explicit Json(std::string s) : rep_(std::move(s)) {}
58
59 /// @name Conversion to a JSON-formatted string.
60 ///@{
61 explicit operator std::string() const& { return rep_; }
62 explicit operator std::string() && { return std::move(rep_); }
63 ///@}
64
65 private:
66 std::string rep_; // a (presumably) JSON-formatted string
67};
68
69/// @name Relational operators
70///@{
71inline bool operator==(Json const& lhs, Json const& rhs) {
72 return std::string(lhs) == std::string(rhs);
73}
74inline bool operator!=(Json const& lhs, Json const& rhs) {
75 return !(lhs == rhs);
76}
77///@}
78
79/// Outputs a JSON-formatted string to the provided stream.
80inline std::ostream& operator<<(std::ostream& os, Json const& json) {
81 return os << std::string(json);
82}
83
84/**
85 * `JsonB` is a variant of `Json` (see above). While both classes share the
86 * same, thin client-side API, `JsonB` stores the data in a decomposed,
87 * binary format, whereas `Json` stores an exact copy of the RFC 7159 text.
88 *
89 * This means that `JsonB` is slower to input, but faster to process as it
90 * avoids reparsing. Therefore, applications that utilize the structured
91 * state of a JSON value should prefer `JsonB`.
92 *
93 * It also means that the `JsonB` stored representation does NOT preserve:
94 * - white space,
95 * - the order of object keys, or
96 * - duplicate object keys.
97 *
98 * Note: `JsonB` is only applicable to PostgreSQL databases (i.e., those
99 * created using `DatabaseDialect::POSTGRESQL`).
100 */
101class JsonB {
102 public:
103 /// A null value.
104 JsonB() : rep_("null") {}
105
106 /// @name Regular value type, supporting copy, assign, move.
107 ///@{
108 JsonB(JsonB const&) = default;
109 JsonB& operator=(JsonB const&) = default;
110 JsonB(JsonB&&) = default;
111 JsonB& operator=(JsonB&&) = default;
112 ///@}
113
114 /**
115 * Construction from a JSON-formatted string. Note that there is no check
116 * here that the argument string is indeed well-formatted. Error detection
117 * will be delayed until the value is passed to Spanner.
118 */
119 explicit JsonB(std::string s) : rep_(std::move(s)) {}
120
121 /// @name Conversion to a JSON-formatted string.
122 ///@{
123 explicit operator std::string() const& { return rep_; }
124 explicit operator std::string() && { return std::move(rep_); }
125 ///@}
126
127 private:
128 std::string rep_; // a (presumably) JSON-formatted string
129};
130
131/// @name Relational operators
132///@{
133inline bool operator==(JsonB const& lhs, JsonB const& rhs) {
134 return std::string(lhs) == std::string(rhs);
135}
136inline bool operator!=(JsonB const& lhs, JsonB const& rhs) {
137 return !(lhs == rhs);
138}
139///@}
140
141/// Outputs a JSON-formatted string to the provided stream.
142inline std::ostream& operator<<(std::ostream& os, JsonB const& json) {
143 return os << std::string(json);
144}
145
146GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
147} // namespace spanner
148} // namespace cloud
149} // namespace google
150
151#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_JSON_H
JsonB is a variant of Json (see above).
Definition: json.h:101
JsonB & operator=(JsonB &&)=default
JsonB()
A null value.
Definition: json.h:104
operator std::string() const &
Definition: json.h:123
JsonB & operator=(JsonB const &)=default
JsonB(std::string s)
Construction from a JSON-formatted string.
Definition: json.h:119
operator std::string() &&
Definition: json.h:124
JsonB(JsonB const &)=default
A simple representation for the Spanner JSON type: a lightweight, text-based, language-independent da...
Definition: json.h:39
Json & operator=(Json &&)=default
Json(Json const &)=default
Json & operator=(Json const &)=default
operator std::string() const &
Definition: json.h:61
Json()
A null value.
Definition: json.h:42
operator std::string() &&
Definition: json.h:62
Json(std::string s)
Construction from a JSON-formatted string.
Definition: json.h:57
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
bool operator==(JsonB const &lhs, JsonB const &rhs)
Definition: json.h:133
bool operator==(Json const &lhs, Json const &rhs)
Definition: json.h:71
bool operator!=(JsonB const &lhs, JsonB const &rhs)
Definition: json.h:136
bool operator!=(Json const &lhs, Json const &rhs)
Definition: json.h:74