Replace rapidjson with nlohmann/json#14467
Conversation
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
@asraa I'll let you tag a reviewer once you've got CI sorted, SG? |
|
Yep! This is still in DRAFT mode, I'll move it out of draft mode when I finish some test TODOs (benchmarking) |
| com_github_nlohmann_json = dict( | ||
| project_name = "nlohmann JSON", | ||
| project_desc = "Fast JSON parser/generator for C++", | ||
| project_url = "https://nlohmann.github.io/json", | ||
| version = "3.9.1", | ||
| sha256 = "4cf0df69731494668bdd6460ed8cb269b68de9c19ad8c27abc24cd72605b2d5b", | ||
| strip_prefix = "json-{version}", | ||
| urls = ["https://github.com/nlohmann/json/archive/v{version}.tar.gz"], | ||
| # This will be a replacement for rapidJSON used in extensions and may also be a fast | ||
| # replacement for protobuf JSON. | ||
| use_category = ["controlplane", "dataplane_core"], | ||
| release_date = "2020-07-06", | ||
| cpe = "cpe:2.3:a:json_project:json:*", | ||
| ), |
There was a problem hiding this comment.
Are you planning on deleting the rapidjson dep?
https://github.com/envoyproxy/envoy/blob/master/bazel/repository_locations.bzl#L434-L447
There was a problem hiding this comment.
I am planning on keeping it as a runtime switchable options in extensions -- although I am down to remove :)
|
/wait |
| strip_prefix = "json-{version}", | ||
| urls = ["https://github.com/nlohmann/json/archive/v{version}.tar.gz"], | ||
| # This will be a replacement for rapidJSON used in extensions and may also be a fast | ||
| # replacement for protobuf JSON. |
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
There is an existing clang-tidy issue about a potential memory leak in the constructor of envoy/source/common/json/json_loader.cc Line 520 in 12c42b5 I couldn't find related issues, tests in rapidjson support this construction. Im not sure I want to spend too much time on a rapidjson issue |
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
Only failing existing clang-tidy. Would appreciate any comment! (especially around the new way of holding line_number_) |
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
Would it be helpful if I displayed the diff between the old/new json loader? The full diff is here https://gist.github.com/asraa/67c51ac7ab48570e471a744fb4c02068 The main differences are:
|
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
ping @jmarantz this is ready. |
| @@ -0,0 +1,668 @@ | |||
| #include "common/json/json_internal.h" | |||
There was a problem hiding this comment.
qq: this file is all new code, and so needs to be reviewed line-by-line? This is not a copy of of something existing?
There was a problem hiding this comment.
Sort of, the diff is above (between the legacy and this one). They have some major differences (listed above) that made it difficult to inherit from the same SAX callbacks.
| @@ -0,0 +1,22 @@ | |||
| #pragma once | |||
|
|
|||
| #include <list> | |||
There was a problem hiding this comment.
list is not used in this header.
| @@ -0,0 +1,22 @@ | |||
| #pragma once | |||
|
|
|||
| #include <list> | |||
There was a problem hiding this comment.
list is not used in this file.
jmarantz
left a comment
There was a problem hiding this comment.
Feel free to ignore the style nits if this is not really code you edited.
Basically this looks good. Will need main merge.
| * dispatcher: supports a stack of `Envoy::ScopeTrackedObject` instead of a single tracked object. This will allow Envoy to dump more debug information on crash. | ||
| * http: added support for :ref:`:ref:`preconnecting <envoy_v3_api_msg_config.cluster.v3.Cluster.PreconnectPolicy>`. Preconnecting is off by default, but recommended for clusters serving latency-sensitive traffic, especially if using HTTP/1.1. | ||
| * http: change frame flood and abuse checks to the upstream HTTP/2 codec to ON by default. It can be disabled by setting the `envoy.reloadable_features.upstream_http2_flood_checks` runtime key to false. | ||
| * json: introduced new JSON parser (https://github.com/nlohmann/json) to replace RapidJSON. The new parser is enabled by default. To revert to the legacy RapidJSON parser, enable the runtime feature `envoy.reloadable_features.legacy_json`. |
There was a problem hiding this comment.
consider making the initial check-in leave the existing default, and then do another PR to flip the default. That way if a problem is discovered, we won't need to roll back the whole PR.
There was a problem hiding this comment.
like that idea very much. done
source/common/json/json_internal.cc
Outdated
| // Extract position information (line, column, token) in the error. | ||
| auto start = error.find("line"); | ||
| auto npos = error.find(":"); | ||
| error_position_ = absl::StrCat(error.substr(start, npos - start), ", token ", token); |
There was a problem hiding this comment.
would like to see some checking of these find() results against std::npos or absl::npos. And while we're at it, let's not also have a local called npos :)
There was a problem hiding this comment.
done, i added a reference, there should always be a ": error string" but I found in the documentation that position information may not be present, so that's handled now.
Thank you!
| checkType(Type::Object); | ||
| auto value_itr = value_.object_value_.find(name); | ||
| if (value_itr == value_.object_value_.end() || !value_itr->second->isType(Type::Boolean)) { | ||
| throw Exception(fmt::format("key '{}' missing or not a boolean from lines {}-{}", name, |
There was a problem hiding this comment.
what's our general strategy to avoid this occurring during request processing?
There was a problem hiding this comment.
There is one usage of loadFromString in the core dataplane code here, as noted in the description I will swap this with protobuf's json parser in a future PR
There is one usage in core to parse metadata in the HeaderFormatter:
These get[String/Boolean] methods that can throw are largely unused, but there are <10 usages in filters that I will replace with those with defaults that don't throw in that PR.
#14893
source/common/json/json_internal.cc
Outdated
| auto value_itr = value_.object_value_.find(name); | ||
| if (value_itr != value_.object_value_.end()) { | ||
| return getDouble(name); | ||
| } else { |
There was a problem hiding this comment.
I think we prefer the form
if (exceptional-condition) {
return exceptional-value;
}
return normal value;
But I can't find a style-guide ref for that. THere's a few different instances of that in this file. Field::getDouble() above is an example where you are doing it the way I think we prefer.
There was a problem hiding this comment.
existing style, fixed in the legacy code as well.
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
Signed-off-by: Asra Ali <asraa@google.com>
|
@envoyproxy/senior-maintainers Could you PTAL? This adds the code for a new json parser, does not flip the switch. |
mattklein123
left a comment
There was a problem hiding this comment.
LGTM at a high level. Very excited to see this moving forward!
Commit Message: Replace rapidjson with nlohmann/json
Additional Description:
envoy.reloadable_features.remove_legacy_json. It is disabled by default. I will enable the new parser in a separate PR.envoy/source/common/router/header_formatter.cc
Line 61 in e073b32
json_internal.ccimplementations is the custom stream class (see json_loader.cc line 243-280. It has a reference to the handler to update line numbers)Risk Level: Low for core, maybe medium for those using JSON on the dataplane.
Release Notes: Added release notes about runtime feature
Testing:
json_loader_test.ccis parametrized over both runtime values.Fixes Remove RapidJSON from Envoy #4705