Skip to content

Commit

Permalink
Switch to use absl::string_view when accepting JSON strings for parsing.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 491953185
  • Loading branch information
aaronnash authored and Copybara-Service committed Nov 30, 2022
1 parent eb53e7c commit 0f15c80
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cc/google/fhir/json/json_sax_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class JsonSaxHandler {
} // namespace

absl::Status ParseJsonValue(
const std::string& raw_json, FhirJson& json_value) {
const absl::string_view raw_json, FhirJson& json_value) {
JsonSaxHandler sax_handler(&json_value);

if (nlohmann::json::sax_parse(raw_json, &sax_handler)) {
Expand Down
5 changes: 3 additions & 2 deletions cc/google/fhir/json/json_sax_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include <memory>
#include <string>

#include "absl/status/statusor.h"
#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "google/fhir/json/fhir_json.h"

namespace google {
Expand All @@ -28,7 +29,7 @@ namespace internal {
// Parse input string into FhirJson, returns error if `raw_json` is not
// well-formatted.
absl::Status ParseJsonValue(
const std::string& raw_json, FhirJson& json_value);
absl::string_view raw_json, FhirJson& json_value);

} // namespace internal
} // namespace fhir
Expand Down
7 changes: 4 additions & 3 deletions cc/google/fhir/json_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "google/protobuf/message.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "google/fhir/error_reporter.h"
#include "google/fhir/json/fhir_json.h"
Expand All @@ -46,7 +47,7 @@ class Parser {
// For reading JSON into a new resource, it is recommended to use
// JsonFhirStringToProto or JsonFhirStringToProtoWithoutValidating.
::absl::StatusOr<ParseResult> MergeJsonFhirStringIntoProto(
const std::string& raw_json, google::protobuf::Message* target,
absl::string_view raw_json, google::protobuf::Message* target,
absl::TimeZone default_timezone, const bool validate,
ErrorHandler& error_handler) const;

Expand All @@ -66,7 +67,7 @@ class Parser {
// timezone for timelike data that does not specify timezone.
template <typename R>
::absl::StatusOr<R> JsonFhirStringToProto(
const std::string& raw_json, const absl::TimeZone default_timezone,
absl::string_view raw_json, const absl::TimeZone default_timezone,
ErrorHandler& error_handler) const {
R resource;
RETURN_IF_ERROR(MergeJsonFhirStringIntoProto(
Expand All @@ -81,7 +82,7 @@ class Parser {
// Takes a default timezone for timelike data that does not specify timezone.
template <typename R>
::absl::StatusOr<R> JsonFhirStringToProtoWithoutValidating(
const std::string& raw_json, const absl::TimeZone default_timezone,
absl::string_view raw_json, const absl::TimeZone default_timezone,
ErrorHandler& error_handler) const {
R resource;
RETURN_IF_ERROR(MergeJsonFhirStringIntoProto(
Expand Down
2 changes: 1 addition & 1 deletion cc/google/fhir/json_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class Parser {
} // namespace internal

absl::StatusOr<ParseResult> Parser::MergeJsonFhirStringIntoProto(
const std::string& raw_json, Message* target,
const absl::string_view raw_json, Message* target,
const absl::TimeZone default_timezone, const bool validate,
ErrorHandler& error_handler) const {
internal::FhirJson json_object;
Expand Down
2 changes: 1 addition & 1 deletion cc/google/fhir/r4/json_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const Printer* GetPrinter() {

} // namespace

absl::Status MergeJsonFhirStringIntoProto(const std::string& raw_json,
absl::Status MergeJsonFhirStringIntoProto(const absl::string_view raw_json,
google::protobuf::Message* target,
absl::TimeZone default_timezone,
const bool validate,
Expand Down
7 changes: 4 additions & 3 deletions cc/google/fhir/r4/json_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "google/fhir/error_reporter.h"
#include "google/fhir/json/fhir_json.h"
Expand All @@ -34,7 +35,7 @@ namespace r4 {
// See cc/google/fhir/json_format.h for documentation on these methods

absl::Status MergeJsonFhirStringIntoProto(
const std::string& raw_json, google::protobuf::Message* target,
absl::string_view raw_json, google::protobuf::Message* target,
absl::TimeZone default_timezone, const bool validate,
ErrorHandler& error_handler = FailFastErrorHandler::FailOnErrorOrFatal());

Expand All @@ -46,7 +47,7 @@ absl::Status MergeJsonFhirObjectIntoProto(

template <typename R>
absl::StatusOr<R> JsonFhirStringToProto(
const std::string& raw_json, const absl::TimeZone default_timezone,
absl::string_view raw_json, const absl::TimeZone default_timezone,
ErrorHandler& error_handler = FailFastErrorHandler::FailOnErrorOrFatal()) {
R resource;
FHIR_RETURN_IF_ERROR(MergeJsonFhirStringIntoProto(
Expand All @@ -67,7 +68,7 @@ absl::StatusOr<R> JsonFhirObjectToProto(

template <typename R>
absl::StatusOr<R> JsonFhirStringToProtoWithoutValidating(
const std::string& raw_json, const absl::TimeZone default_timezone) {
absl::string_view raw_json, const absl::TimeZone default_timezone) {
R resource;

// This uses a "Fail On Fatal Only" error handler, meaning it will succeed on
Expand Down
1 change: 1 addition & 0 deletions cc/google/fhir/stu3/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ cc_library(
"//cc/google/fhir/status:statusor",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
],
)

Expand Down
2 changes: 1 addition & 1 deletion cc/google/fhir/stu3/json_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const Printer* GetPrinter() {

} // namespace

absl::Status MergeJsonFhirStringIntoProto(const std::string& raw_json,
absl::Status MergeJsonFhirStringIntoProto(const absl::string_view raw_json,
google::protobuf::Message* target,
absl::TimeZone default_timezone,
const bool validate,
Expand Down
7 changes: 4 additions & 3 deletions cc/google/fhir/stu3/json_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <string>

#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "google/fhir/error_reporter.h"
#include "google/fhir/json_format.h"
#include "google/fhir/status/status.h"
Expand All @@ -32,13 +33,13 @@ namespace stu3 {
// See cc/google/fhir/json_format.h for documentation on these methods

absl::Status MergeJsonFhirStringIntoProto(
const std::string& raw_json, google::protobuf::Message* target,
absl::string_view raw_json, google::protobuf::Message* target,
absl::TimeZone default_timezone, const bool validate,
ErrorHandler& error_handler = FailFastErrorHandler::FailOnErrorOrFatal());

template <typename R>
absl::StatusOr<R> JsonFhirStringToProto(
const std::string& raw_json, const absl::TimeZone default_timezone,
absl::string_view raw_json, const absl::TimeZone default_timezone,
ErrorHandler& error_handler = FailFastErrorHandler::FailOnErrorOrFatal()) {
R resource;
FHIR_RETURN_IF_ERROR(MergeJsonFhirStringIntoProto(
Expand All @@ -48,7 +49,7 @@ absl::StatusOr<R> JsonFhirStringToProto(

template <typename R>
absl::StatusOr<R> JsonFhirStringToProtoWithoutValidating(
const std::string& raw_json, const absl::TimeZone default_timezone,
absl::string_view raw_json, const absl::TimeZone default_timezone,
ErrorHandler& error_handler = FailFastErrorHandler::FailOnErrorOrFatal()) {
R resource;
FHIR_RETURN_IF_ERROR(MergeJsonFhirStringIntoProto(
Expand Down

0 comments on commit 0f15c80

Please sign in to comment.