[JSON] make API public but experimental (#32987)

This makes the JSON API visible as part of the C-core API, but in the
`experimental` namespace. It will be used as part of various
experimental APIs that we will be introducing in the near future, such
as the audit logging API.
pull/32945/head
Mark D. Roth 2 years ago committed by GitHub
parent 6c2f4371bb
commit 1432fe4e4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      .gitattributes
  2. 6
      BUILD
  3. 4
      CMakeLists.txt
  4. 3
      Makefile
  5. 4
      build_autogenerated.yaml
  6. 1
      gRPC-Core.podspec
  7. 1
      grpc.gemspec
  8. 5
      include/grpc/grpc_audit_logging.h
  9. 1
      include/grpc/module.modulemap
  10. 218
      include/grpc/support/json.h
  11. 1
      package.xml
  12. 5
      src/core/BUILD
  13. 1
      src/core/ext/filters/client_channel/client_channel.cc
  14. 2
      src/core/ext/filters/client_channel/client_channel_channelz.cc
  15. 1
      src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc
  16. 1
      src/core/ext/filters/client_channel/lb_policy/rls/rls.cc
  17. 1
      src/core/ext/filters/client_channel/lb_policy/xds/cds.cc
  18. 1
      src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_resolver.cc
  19. 1
      src/core/ext/filters/client_channel/lb_policy/xds/xds_wrr_locality.cc
  20. 1
      src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc
  21. 1
      src/core/ext/filters/client_channel/retry_service_config.cc
  22. 1
      src/core/ext/xds/certificate_provider_store.cc
  23. 2
      src/core/ext/xds/xds_audit_logger_registry.cc
  24. 2
      src/core/ext/xds/xds_bootstrap_grpc.cc
  25. 1
      src/core/ext/xds/xds_cluster.cc
  26. 1
      src/core/ext/xds/xds_cluster_specifier_plugin.cc
  27. 2
      src/core/ext/xds/xds_common_types.cc
  28. 1
      src/core/ext/xds/xds_http_fault_filter.cc
  29. 2
      src/core/ext/xds/xds_http_rbac_filter.cc
  30. 2
      src/core/ext/xds/xds_http_stateful_session_filter.cc
  31. 2
      src/core/ext/xds/xds_lb_policy_registry.cc
  32. 1
      src/core/lib/channel/channel_trace.cc
  33. 1
      src/core/lib/channel/channelz.cc
  34. 9
      src/core/lib/channel/channelz_registry.cc
  35. 190
      src/core/lib/json/json.h
  36. 1
      src/core/lib/json/json_reader.cc
  37. 1
      src/core/lib/load_balancing/lb_policy_registry.cc
  38. 1
      src/core/lib/security/authorization/audit_logging.cc
  39. 2
      src/core/lib/security/authorization/audit_logging.h
  40. 1
      src/core/lib/security/authorization/rbac_translator.cc
  41. 1
      src/core/lib/security/credentials/external/aws_external_account_credentials.cc
  42. 1
      src/core/lib/security/credentials/external/external_account_credentials.cc
  43. 1
      src/core/lib/security/credentials/external/file_external_account_credentials.cc
  44. 1
      src/core/lib/security/credentials/external/url_external_account_credentials.cc
  45. 1
      src/core/lib/security/credentials/google_default/google_default_credentials.cc
  46. 6
      src/core/lib/security/credentials/jwt/json_token.cc
  47. 1
      src/core/lib/security/credentials/jwt/jwt_credentials.cc
  48. 1
      src/core/lib/security/credentials/jwt/jwt_verifier.cc
  49. 1
      src/core/lib/security/credentials/oauth2/oauth2_credentials.cc
  50. 1
      src/core/lib/security/util/json_util.cc
  51. 1
      src/cpp/client/secure_credentials.cc
  52. 1
      test/core/channel/channelz_test.cc
  53. 1
      test/core/client_channel/lb_policy/outlier_detection_test.cc
  54. 1
      test/core/client_channel/lb_policy/weighted_round_robin_test.cc
  55. 1
      test/core/client_channel/lb_policy/xds_override_host_test.cc
  56. 3
      test/core/json/fuzzer.cc
  57. 2
      test/core/json/json_object_loader_test.cc
  58. 4
      test/core/json/json_test.cc
  59. 2
      test/core/security/verify_jwt.cc
  60. 1
      test/core/xds/xds_bootstrap_test.cc
  61. 1
      test/core/xds/xds_client_test.cc
  62. 1
      test/core/xds/xds_http_filters_test.cc
  63. 2
      test/cpp/util/channel_trace_proto_helper.cc
  64. 4
      test/cpp/util/channelz_sampler.cc
  65. 6
      tools/distrib/check_include_guards.py
  66. 1
      tools/doxygen/Doxyfile.c++
  67. 1
      tools/doxygen/Doxyfile.c++.internal
  68. 1
      tools/doxygen/Doxyfile.core
  69. 1
      tools/doxygen/Doxyfile.core.internal

3
.gitattributes vendored

@ -19,8 +19,11 @@ binding.gyp linguist-generated=true
src/python/grpcio/grpc_core_dependencies.py linguist-generated=true
src/ruby/ext/grpc/rb_grpc_imports.generated.h linguist-generated=true
src/ruby/ext/grpc/rb_grpc_imports.generated.c linguist-generated=true
include/grpc/module.modulemap linguist-generated=true
test/core/security/grpc_tls_credentials_options_comparator_test.cc linguist-generated=true
tools/doxygen/Doxyfile.c++ linguist-generated=true
tools/doxygen/Doxyfile.c++.internal linguist-generated=true
tools/doxygen/Doxyfile.core linguist-generated=true
tools/doxygen/Doxyfile.core.internal linguist-generated=true
tools/run_tests/sources_and_headers.json linguist-generated=true
tools/run_tests/tests.json linguist-generated=true

@ -203,6 +203,7 @@ GPR_PUBLIC_HDRS = [
"include/grpc/support/atm_gcc_sync.h",
"include/grpc/support/atm_windows.h",
"include/grpc/support/cpu.h",
"include/grpc/support/json.h",
"include/grpc/support/log.h",
"include/grpc/support/log_windows.h",
"include/grpc/support/port_platform.h",
@ -745,6 +746,10 @@ grpc_cc_library(
grpc_cc_library(
name = "gpr_public_hdrs",
hdrs = GPR_PUBLIC_HDRS,
external_deps = [
"absl/strings",
"absl/types:variant",
],
tags = [
"avoid_dep",
"nofixdeps",
@ -1769,7 +1774,6 @@ grpc_cc_library(
"//src/core:handshaker_factory",
"//src/core:handshaker_registry",
"//src/core:iomgr_fwd",
"//src/core:json",
"//src/core:memory_quota",
"//src/core:poll",
"//src/core:ref_counted",

4
CMakeLists.txt generated

@ -1544,6 +1544,7 @@ foreach(_hdr
include/grpc/support/atm_gcc_sync.h
include/grpc/support/atm_windows.h
include/grpc/support/cpu.h
include/grpc/support/json.h
include/grpc/support/log.h
include/grpc/support/log_windows.h
include/grpc/support/port_platform.h
@ -2452,6 +2453,7 @@ foreach(_hdr
include/grpc/support/atm_gcc_sync.h
include/grpc/support/atm_windows.h
include/grpc/support/cpu.h
include/grpc/support/json.h
include/grpc/support/log.h
include/grpc/support/log_windows.h
include/grpc/support/port_platform.h
@ -3090,6 +3092,7 @@ foreach(_hdr
include/grpc/support/atm_gcc_sync.h
include/grpc/support/atm_windows.h
include/grpc/support/cpu.h
include/grpc/support/json.h
include/grpc/support/log.h
include/grpc/support/log_windows.h
include/grpc/support/port_platform.h
@ -4600,6 +4603,7 @@ foreach(_hdr
include/grpc/support/atm_gcc_sync.h
include/grpc/support/atm_windows.h
include/grpc/support/cpu.h
include/grpc/support/json.h
include/grpc/support/log.h
include/grpc/support/log_windows.h
include/grpc/support/port_platform.h

3
Makefile generated

@ -908,6 +908,7 @@ PUBLIC_HEADERS_C += \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \
@ -1777,6 +1778,7 @@ PUBLIC_HEADERS_C += \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \
@ -2270,6 +2272,7 @@ PUBLIC_HEADERS_C += \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \

@ -36,6 +36,7 @@ libs:
- include/grpc/support/atm_gcc_sync.h
- include/grpc/support/atm_windows.h
- include/grpc/support/cpu.h
- include/grpc/support/json.h
- include/grpc/support/log.h
- include/grpc/support/log_windows.h
- include/grpc/support/port_platform.h
@ -195,6 +196,7 @@ libs:
- include/grpc/support/atm_gcc_sync.h
- include/grpc/support/atm_windows.h
- include/grpc/support/cpu.h
- include/grpc/support/json.h
- include/grpc/support/log.h
- include/grpc/support/log_windows.h
- include/grpc/support/port_platform.h
@ -1897,6 +1899,7 @@ libs:
- include/grpc/support/atm_gcc_sync.h
- include/grpc/support/atm_windows.h
- include/grpc/support/cpu.h
- include/grpc/support/json.h
- include/grpc/support/log.h
- include/grpc/support/log_windows.h
- include/grpc/support/port_platform.h
@ -3484,6 +3487,7 @@ libs:
- include/grpc/support/atm_gcc_sync.h
- include/grpc/support/atm_windows.h
- include/grpc/support/cpu.h
- include/grpc/support/json.h
- include/grpc/support/log.h
- include/grpc/support/log_windows.h
- include/grpc/support/port_platform.h

1
gRPC-Core.podspec generated

@ -163,6 +163,7 @@ Pod::Spec.new do |s|
'include/grpc/support/atm_gcc_sync.h',
'include/grpc/support/atm_windows.h',
'include/grpc/support/cpu.h',
'include/grpc/support/json.h',
'include/grpc/support/log.h',
'include/grpc/support/log_windows.h',
'include/grpc/support/port_platform.h',

1
grpc.gemspec generated

@ -103,6 +103,7 @@ Gem::Specification.new do |s|
s.files += %w( include/grpc/support/atm_gcc_sync.h )
s.files += %w( include/grpc/support/atm_windows.h )
s.files += %w( include/grpc/support/cpu.h )
s.files += %w( include/grpc/support/json.h )
s.files += %w( include/grpc/support/log.h )
s.files += %w( include/grpc/support/log_windows.h )
s.files += %w( include/grpc/support/port_platform.h )

@ -27,8 +27,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
// TODO(lwge): Switch to public header when it's ready.
#include "src/core/lib/json/json.h"
#include <grpc/support/json.h>
namespace grpc_core {
namespace experimental {
@ -93,4 +92,4 @@ void RegisterAuditLoggerFactory(std::unique_ptr<AuditLoggerFactory> factory);
} // namespace experimental
} // namespace grpc_core
#endif /* GRPC_GRPC_AUDIT_LOGGING_H */
#endif // GRPC_GRPC_AUDIT_LOGGING_H

@ -40,6 +40,7 @@ header "byte_buffer.h"
header "support/alloc.h"
header "support/atm.h"
header "support/cpu.h"
header "support/json.h"
header "support/log.h"
header "support/log_windows.h"
header "support/port_platform.h"

@ -0,0 +1,218 @@
//
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef GRPC_SUPPORT_JSON_H
#define GRPC_SUPPORT_JSON_H
#include <grpc/support/port_platform.h>
#include <stdint.h>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/types/variant.h"
namespace grpc_core {
namespace experimental {
// A JSON value, which can be any one of null, boolean, number, string,
// object, or array.
class Json {
public:
// The JSON type.
enum class Type {
kNull, // No payload. Default type when using the zero-arg ctor.
kBoolean, // Use boolean() for payload.
kNumber, // Numbers are stored in string form to avoid precision
// and integer capacity issues. Use string() for payload.
kString, // Use string() for payload.
kObject, // Use object() for payload.
kArray, // Use array() for payload.
};
using Object = std::map<std::string, Json>;
using Array = std::vector<Json>;
// Factory method for kBoolean.
static Json FromBool(bool b) {
Json json;
json.value_ = b;
return json;
}
// Factory methods for kNumber.
static Json FromNumber(const std::string& str) {
Json json;
json.value_ = NumberValue{str};
return json;
}
static Json FromNumber(const char* str) {
Json json;
json.value_ = NumberValue{std::string(str)};
return json;
}
static Json FromNumber(std::string&& str) {
Json json;
json.value_ = NumberValue{std::move(str)};
return json;
}
static Json FromNumber(int32_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(uint32_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(int64_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(uint64_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(double value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
// Factory methods for kString.
static Json FromString(const std::string& str) {
Json json;
json.value_ = str;
return json;
}
static Json FromString(const char* str) {
Json json;
json.value_ = std::string(str);
return json;
}
static Json FromString(std::string&& str) {
Json json;
json.value_ = std::move(str);
return json;
}
// Factory methods for kObject.
static Json FromObject(const Object& object) {
Json json;
json.value_ = object;
return json;
}
static Json FromObject(Object&& object) {
Json json;
json.value_ = std::move(object);
return json;
}
// Factory methods for kArray.
static Json FromArray(const Array& array) {
Json json;
json.value_ = array;
return json;
}
static Json FromArray(Array&& array) {
Json json;
json.value_ = std::move(array);
return json;
}
Json() = default;
// Copyable.
Json(const Json& other) = default;
Json& operator=(const Json& other) = default;
// Moveable.
Json(Json&& other) noexcept : value_(std::move(other.value_)) {
other.value_ = absl::monostate();
}
Json& operator=(Json&& other) noexcept {
value_ = std::move(other.value_);
other.value_ = absl::monostate();
return *this;
}
// Returns the JSON type.
Type type() const {
struct ValueFunctor {
Json::Type operator()(const absl::monostate&) { return Type::kNull; }
Json::Type operator()(bool) { return Type::kBoolean; }
Json::Type operator()(const NumberValue&) { return Type::kNumber; }
Json::Type operator()(const std::string&) { return Type::kString; }
Json::Type operator()(const Object&) { return Type::kObject; }
Json::Type operator()(const Array&) { return Type::kArray; }
};
return absl::visit(ValueFunctor(), value_);
}
// Payload accessor for kBoolean.
// Must not be called for other types.
bool boolean() const { return absl::get<bool>(value_); }
// Payload accessor for kNumber or kString.
// Must not be called for other types.
const std::string& string() const {
const NumberValue* num = absl::get_if<NumberValue>(&value_);
if (num != nullptr) return num->value;
return absl::get<std::string>(value_);
}
// Payload accessor for kObject.
// Must not be called for other types.
const Object& object() const { return absl::get<Object>(value_); }
// Payload accessor for kArray.
// Must not be called for other types.
const Array& array() const { return absl::get<Array>(value_); }
bool operator==(const Json& other) const { return value_ == other.value_; }
bool operator!=(const Json& other) const { return !(*this == other); }
private:
struct NumberValue {
std::string value;
bool operator==(const NumberValue& other) const {
return value == other.value;
}
};
using Value = absl::variant<absl::monostate, // kNull
bool, // kBoolean
NumberValue, // kNumber
std::string, // kString
Object, // kObject
Array>; // kArray
explicit Json(Value value) : value_(std::move(value)) {}
Value value_;
};
} // namespace experimental
} // namespace grpc_core
#endif // GRPC_SUPPORT_JSON_H

1
package.xml generated

@ -85,6 +85,7 @@
<file baseinstalldir="/" name="include/grpc/support/atm_gcc_sync.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/atm_windows.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/cpu.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/json.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/log.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/log_windows.h" role="src" />
<file baseinstalldir="/" name="include/grpc/support/port_platform.h" role="src" />

@ -2758,7 +2758,6 @@ grpc_cc_library(
"absl/strings:str_format",
],
deps = [
"json",
"//:gpr",
"//:grpc_base",
],
@ -3334,10 +3333,6 @@ grpc_cc_library(
hdrs = [
"lib/json/json.h",
],
external_deps = [
"absl/strings",
"absl/types:variant",
],
deps = ["//:gpr"],
)

@ -42,6 +42,7 @@
#include <grpc/event_engine/event_engine.h>
#include <grpc/slice.h>
#include <grpc/status.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpc/support/time.h>

@ -22,6 +22,8 @@
#include "absl/strings/str_cat.h"
#include <grpc/support/json.h>
#include "src/core/lib/transport/connectivity_state.h"
// IWYU pragma: no_include <type_traits>

@ -54,6 +54,7 @@
#include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.h"
#include <grpc/event_engine/event_engine.h>
#include <grpc/support/json.h>
// IWYU pragma: no_include <sys/socket.h>

@ -61,6 +61,7 @@
#include <grpc/impl/propagation_bits.h>
#include <grpc/slice.h>
#include <grpc/status.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/client_channel.h"

@ -36,6 +36,7 @@
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/impl/connectivity_state.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/lb_policy/outlier_detection/outlier_detection.h"

@ -38,6 +38,7 @@
#include <grpc/event_engine/event_engine.h>
#include <grpc/grpc.h>
#include <grpc/impl/connectivity_state.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/lb_policy/address_filtering.h"

@ -30,6 +30,7 @@
#include <grpc/event_engine/event_engine.h>
#include <grpc/impl/connectivity_state.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/lb_policy/xds/xds_attributes.h"

@ -29,6 +29,7 @@
#include "absl/strings/strip.h"
#include "absl/types/optional.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/gcp/metadata_query.h"

@ -29,6 +29,7 @@
#include <grpc/grpc.h>
#include <grpc/status.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/channel/channel_args.h"

@ -22,6 +22,7 @@
#include "absl/strings/str_cat.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/config/core_configuration.h"

@ -26,6 +26,8 @@
#include "envoy/config/core/v3/extension.upb.h"
#include "envoy/config/rbac/v3/rbac.upb.h"
#include <grpc/support/json.h>
#include "src/core/ext/xds/xds_common_types.h"
#include "src/core/lib/gprpp/validation_errors.h"
#include "src/core/lib/json/json.h"

@ -35,6 +35,8 @@
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include <grpc/support/json.h>
#include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/json/json.h"

@ -46,6 +46,7 @@
#include "upb/base/string_view.h"
#include "upb/text/encode.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/xds/upb_utils.h"

@ -30,6 +30,7 @@
#include "upb/json/encode.h"
#include "upb/upb.hpp"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/json/json.h"

@ -45,6 +45,8 @@
#include "upb/upb.hpp"
#include "xds/type/v3/typed_struct.upb.h"
#include <grpc/support/json.h>
#include "src/core/ext/xds/upb_utils.h"
#include "src/core/ext/xds/xds_bootstrap_grpc.h"
#include "src/core/ext/xds/xds_client.h"

@ -34,6 +34,7 @@
#include "google/protobuf/wrappers.upb.h"
#include <grpc/status.h>
#include <grpc/support/json.h>
#include "src/core/ext/filters/fault_injection/fault_injection_filter.h"
#include "src/core/ext/filters/fault_injection/fault_injection_service_config_parser.h"

@ -42,6 +42,8 @@
#include "google/protobuf/wrappers.upb.h"
#include "upb/collections/map.h"
#include <grpc/support/json.h>
#include "src/core/ext/filters/rbac/rbac_filter.h"
#include "src/core/ext/filters/rbac/rbac_service_config_parser.h"
#include "src/core/ext/xds/upb_utils.h"

@ -31,6 +31,8 @@
#include "envoy/extensions/http/stateful_session/cookie/v3/cookie.upbdefs.h"
#include "envoy/type/http/v3/cookie.upb.h"
#include <grpc/support/json.h>
#include "src/core/ext/filters/stateful_session/stateful_session_filter.h"
#include "src/core/ext/filters/stateful_session/stateful_session_service_config_parser.h"
#include "src/core/ext/xds/upb_utils.h"

@ -33,6 +33,8 @@
#include "envoy/extensions/load_balancing_policies/wrr_locality/v3/wrr_locality.upb.h"
#include "google/protobuf/wrappers.upb.h"
#include <grpc/support/json.h>
#include "src/core/ext/xds/xds_common_types.h"
#include "src/core/lib/config/core_configuration.h"
#include "src/core/lib/gprpp/time.h"

@ -26,6 +26,7 @@
#include "absl/strings/str_cat.h"
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include "src/core/lib/channel/channelz.h"
#include "src/core/lib/gpr/string.h"

@ -29,6 +29,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/strip.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/time.h>

@ -27,6 +27,7 @@
#include <vector>
#include <grpc/grpc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
@ -205,7 +206,7 @@ char* grpc_channelz_get_server(intptr_t server_id) {
grpc_core::Json json = grpc_core::Json::FromObject({
{"server", server_node->RenderJson()},
});
return gpr_strdup(JsonDump(json).c_str());
return gpr_strdup(grpc_core::JsonDump(json).c_str());
}
char* grpc_channelz_get_server_sockets(intptr_t server_id,
@ -244,7 +245,7 @@ char* grpc_channelz_get_channel(intptr_t channel_id) {
grpc_core::Json json = grpc_core::Json::FromObject({
{"channel", channel_node->RenderJson()},
});
return gpr_strdup(JsonDump(json).c_str());
return gpr_strdup(grpc_core::JsonDump(json).c_str());
}
char* grpc_channelz_get_subchannel(intptr_t subchannel_id) {
@ -260,7 +261,7 @@ char* grpc_channelz_get_subchannel(intptr_t subchannel_id) {
grpc_core::Json json = grpc_core::Json::FromObject({
{"subchannel", subchannel_node->RenderJson()},
});
return gpr_strdup(JsonDump(json).c_str());
return gpr_strdup(grpc_core::JsonDump(json).c_str());
}
char* grpc_channelz_get_socket(intptr_t socket_id) {
@ -276,5 +277,5 @@ char* grpc_channelz_get_socket(intptr_t socket_id) {
grpc_core::Json json = grpc_core::Json::FromObject({
{"socket", socket_node->RenderJson()},
});
return gpr_strdup(JsonDump(json).c_str());
return gpr_strdup(grpc_core::JsonDump(json).c_str());
}

@ -19,197 +19,11 @@
#include <grpc/support/port_platform.h>
#include <stdint.h>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/types/variant.h"
#include <grpc/support/json.h>
namespace grpc_core {
// A JSON value, which can be any one of null, boolean, number, string,
// object, or array.
class Json {
public:
// The JSON type.
enum class Type {
kNull, // No payload. Default type when using the zero-arg ctor.
kBoolean, // Use boolean() for payload.
kNumber, // Numbers are stored in string form to avoid precision
// and integer capacity issues. Use string() for payload.
kString, // Use string() for payload.
kObject, // Use object() for payload.
kArray, // Use array() for payload.
};
using Object = std::map<std::string, Json>;
using Array = std::vector<Json>;
// Factory method for kBoolean.
static Json FromBool(bool b) {
Json json;
json.value_ = b;
return json;
}
// Factory methods for kNumber.
static Json FromNumber(const std::string& str) {
Json json;
json.value_ = NumberValue{str};
return json;
}
static Json FromNumber(const char* str) {
Json json;
json.value_ = NumberValue{std::string(str)};
return json;
}
static Json FromNumber(std::string&& str) {
Json json;
json.value_ = NumberValue{std::move(str)};
return json;
}
static Json FromNumber(int32_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(uint32_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(int64_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(uint64_t value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
static Json FromNumber(double value) {
Json json;
json.value_ = NumberValue{absl::StrCat(value)};
return json;
}
// Factory methods for kString.
static Json FromString(const std::string& str) {
Json json;
json.value_ = str;
return json;
}
static Json FromString(const char* str) {
Json json;
json.value_ = std::string(str);
return json;
}
static Json FromString(std::string&& str) {
Json json;
json.value_ = std::move(str);
return json;
}
// Factory methods for kObject.
static Json FromObject(const Object& object) {
Json json;
json.value_ = object;
return json;
}
static Json FromObject(Object&& object) {
Json json;
json.value_ = std::move(object);
return json;
}
// Factory methods for kArray.
static Json FromArray(const Array& array) {
Json json;
json.value_ = array;
return json;
}
static Json FromArray(Array&& array) {
Json json;
json.value_ = std::move(array);
return json;
}
Json() = default;
// Copyable.
Json(const Json& other) = default;
Json& operator=(const Json& other) = default;
// Moveable.
Json(Json&& other) noexcept : value_(std::move(other.value_)) {
other.value_ = absl::monostate();
}
Json& operator=(Json&& other) noexcept {
value_ = std::move(other.value_);
other.value_ = absl::monostate();
return *this;
}
// Returns the JSON type.
Type type() const {
struct ValueFunctor {
Json::Type operator()(const absl::monostate&) { return Type::kNull; }
Json::Type operator()(bool) { return Type::kBoolean; }
Json::Type operator()(const NumberValue&) { return Type::kNumber; }
Json::Type operator()(const std::string&) { return Type::kString; }
Json::Type operator()(const Object&) { return Type::kObject; }
Json::Type operator()(const Array&) { return Type::kArray; }
};
return absl::visit(ValueFunctor(), value_);
}
// Payload accessor for kBoolean.
// Must not be called for other types.
bool boolean() const { return absl::get<bool>(value_); }
// Payload accessor for kNumber or kString.
// Must not be called for other types.
const std::string& string() const {
const NumberValue* num = absl::get_if<NumberValue>(&value_);
if (num != nullptr) return num->value;
return absl::get<std::string>(value_);
}
// Payload accessor for kObject.
// Must not be called for other types.
const Object& object() const { return absl::get<Object>(value_); }
// Payload accessor for kArray.
// Must not be called for other types.
const Array& array() const { return absl::get<Array>(value_); }
bool operator==(const Json& other) const { return value_ == other.value_; }
bool operator!=(const Json& other) const { return !(*this == other); }
private:
struct NumberValue {
std::string value;
bool operator==(const NumberValue& other) const {
return value == other.value;
}
};
using Value = absl::variant<absl::monostate, // kNull
bool, // kBoolean
NumberValue, // kNumber
std::string, // kString
Object, // kObject
Array>; // kArray
explicit Json(Value value) : value_(std::move(value)) {}
Value value_;
};
using Json = experimental::Json;
} // namespace grpc_core

@ -35,6 +35,7 @@
#include "absl/strings/string_view.h"
#include "absl/types/variant.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/match.h"

@ -31,6 +31,7 @@
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/load_balancing/lb_policy.h"

@ -34,7 +34,6 @@
#include <grpc/support/log.h>
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/json/json.h"
namespace grpc_core {
namespace experimental {

@ -29,9 +29,9 @@
#include "absl/strings/string_view.h"
#include <grpc/grpc_audit_logging.h>
#include <grpc/support/json.h>
#include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/json/json.h"
namespace grpc_core {
namespace experimental {

@ -35,6 +35,7 @@
#include "absl/strings/strip.h"
#include <grpc/grpc_audit_logging.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/gpr/useful.h"

@ -34,6 +34,7 @@
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>

@ -40,6 +40,7 @@
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>

@ -25,6 +25,7 @@
#include "absl/strings/string_view.h"
#include <grpc/slice.h>
#include <grpc/support/json.h>
#include "src/core/lib/iomgr/load_file.h"
#include "src/core/lib/json/json.h"

@ -33,6 +33,7 @@
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>

@ -34,6 +34,7 @@
#include <grpc/grpc_security_constants.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>

@ -36,6 +36,7 @@
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/time.h>
@ -170,7 +171,7 @@ static char* encoded_jwt_header(const char* key_id, const char* algorithm) {
{"typ", Json::FromString(GRPC_JWT_TYPE)},
{"kid", Json::FromString(key_id)},
});
std::string json_str = JsonDump(json);
std::string json_str = grpc_core::JsonDump(json);
return grpc_base64_encode(json_str.c_str(), json_str.size(), 1, 0);
}
@ -197,7 +198,8 @@ static char* encoded_jwt_claim(const grpc_auth_json_key* json_key,
object["sub"] = Json::FromString(json_key->client_email);
}
std::string json_str = JsonDump(Json::FromObject(std::move(object)));
std::string json_str =
grpc_core::JsonDump(Json::FromObject(std::move(object)));
return grpc_base64_encode(json_str.c_str(), json_str.size(), 1, 0);
}

@ -30,6 +30,7 @@
#include "absl/strings/str_cat.h"
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpc/support/sync.h>

@ -45,6 +45,7 @@
#include <grpc/grpc.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpc/support/time.h>

@ -39,6 +39,7 @@
#include <grpc/grpc_security.h>
#include <grpc/slice.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpc/support/time.h>

@ -26,6 +26,7 @@
#include "absl/strings/str_cat.h"
#include <grpc/support/json.h>
#include <grpc/support/string_util.h>
#include "src/core/lib/iomgr/error.h"

@ -32,6 +32,7 @@
#include <grpc/event_engine/event_engine.h>
#include <grpc/grpc_security_constants.h>
#include <grpc/slice.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
#include <grpc/support/time.h>

@ -32,6 +32,7 @@
#include <grpc/grpc.h>
#include <grpc/grpc_security.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include <grpc/support/time.h>
#include "src/core/lib/channel/channel_args.h"

@ -25,6 +25,7 @@
#include "gtest/gtest.h"
#include <grpc/grpc.h>
#include <grpc/support/json.h>
#include "src/core/lib/gprpp/orphanable.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"

@ -40,6 +40,7 @@
#include <grpc/event_engine/event_engine.h>
#include <grpc/grpc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/lb_policy/backend_metric_data.h"

@ -31,6 +31,7 @@
#include "gtest/gtest.h"
#include <grpc/grpc.h>
#include <grpc/support/json.h>
#include "src/core/ext/filters/stateful_session/stateful_session_filter.h"
#include "src/core/ext/xds/xds_health_status.h"

@ -22,6 +22,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include "src/core/lib/json/json.h"
@ -35,7 +36,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
auto json = grpc_core::JsonParse(
absl::string_view(reinterpret_cast<const char*>(data), size));
if (json.ok()) {
auto text2 = JsonDump(*json);
auto text2 = grpc_core::JsonDump(*json);
auto json2 = grpc_core::JsonParse(text2);
GPR_ASSERT(json2.ok());
GPR_ASSERT(*json == *json2);

@ -20,6 +20,8 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <grpc/support/json.h>
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/json/json_reader.h"

@ -18,6 +18,10 @@
#include <string.h>
#include <map>
#include <string>
#include <utility>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"

@ -57,7 +57,7 @@ static void on_jwt_verification_done(void* user_data,
if (sync->success) {
GPR_ASSERT(claims != nullptr);
std::string claims_str =
JsonDump(*grpc_jwt_claims_json(claims), /*indent=*/2);
grpc_core::JsonDump(*grpc_jwt_claims_json(claims), /*indent=*/2);
printf("Claims: \n\n%s\n", claims_str.c_str());
grpc_jwt_claims_destroy(claims);
} else {

@ -37,6 +37,7 @@
#include <grpc/grpc_security.h>
#include <grpc/grpc_security_constants.h>
#include <grpc/support/alloc.h>
#include <grpc/support/json.h>
#include "src/core/ext/xds/certificate_provider_store.h"
#include "src/core/ext/xds/xds_bootstrap_grpc.h"

@ -40,6 +40,7 @@
#include "upb/reflection/def.h"
#include <grpc/grpc.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpcpp/impl/codegen/config_protobuf.h>

@ -38,6 +38,7 @@
#include <grpc/grpc.h>
#include <grpc/status.h>
#include <grpc/support/json.h>
#include <grpc/support/log.h>
#include <grpcpp/impl/codegen/config_protobuf.h>

@ -66,7 +66,7 @@ void VaidateProtoJsonTranslation(const std::string& json_str) {
auto parsed_json = grpc_core::JsonParse(proto_json_str.c_str());
ASSERT_TRUE(parsed_json.ok()) << parsed_json.status();
ASSERT_EQ(parsed_json->type(), grpc_core::Json::Type::kObject);
proto_json_str = JsonDump(*parsed_json);
proto_json_str = grpc_core::JsonDump(*parsed_json);
EXPECT_EQ(json_str, proto_json_str);
}

@ -535,7 +535,9 @@ class ChannelzSampler final {
}
// Dump data in json
std::string DumpJson() { return JsonDump(grpc_core::Json::FromArray(json_)); }
std::string DumpJson() {
return grpc_core::JsonDump(grpc_core::Json::FromArray(json_));
}
// Check if one entity has been recorded
bool CheckID(int64_t id) {

@ -55,8 +55,10 @@ class GuardValidator(object):
self.failed = False
def _is_c_core_header(self, fpath):
return 'include' in fpath and not ('grpc++' in fpath or 'grpcpp'
in fpath or 'event_engine' in fpath)
return 'include' in fpath and not (
'grpc++' in fpath or 'grpcpp' in fpath or 'event_engine' in fpath or
fpath.endswith('/grpc_audit_logging.h') or
fpath.endswith('/json.h'))
def fail(self, fpath, regexp, fcontents, match_txt, correct, fix):
c_core_header = self._is_c_core_header(fpath)

@ -931,6 +931,7 @@ include/grpc/support/atm_gcc_atomic.h \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \

@ -931,6 +931,7 @@ include/grpc/support/atm_gcc_atomic.h \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \

@ -864,6 +864,7 @@ include/grpc/support/atm_gcc_atomic.h \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \

@ -864,6 +864,7 @@ include/grpc/support/atm_gcc_atomic.h \
include/grpc/support/atm_gcc_sync.h \
include/grpc/support/atm_windows.h \
include/grpc/support/cpu.h \
include/grpc/support/json.h \
include/grpc/support/log.h \
include/grpc/support/log_windows.h \
include/grpc/support/port_platform.h \

Loading…
Cancel
Save