Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
json.h
Go to the documentation of this file.
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 // http://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 
22 namespace google {
23 namespace cloud {
24 namespace spanner {
25 inline namespace SPANNER_CLIENT_NS {
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  */
39 class Json {
40  public:
41  /// A null value.
42  Json() : rep_("null") {}
43 
44  /// 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  /// 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 ///@{
71 inline bool operator==(Json const& lhs, Json const& rhs) {
72  return std::string(lhs) == std::string(rhs);
73 }
74 inline 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.
80 inline std::ostream& operator<<(std::ostream& os, Json const& json) {
81  return os << std::string(json);
82 }
83 
84 } // namespace SPANNER_CLIENT_NS
85 } // namespace spanner
86 } // namespace cloud
87 } // namespace google
88 
89 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_JSON_H