From 1eee3202fce4953a9c0c3adbf357ed10bd71e01e Mon Sep 17 00:00:00 2001 From: Brendan McCarthy Date: Fri, 17 Mar 2017 18:46:18 +1000 Subject: Add use_snake_case_for_field_names option to JsonPrintOptions --- src/google/protobuf/util/internal/json_objectwriter.cc | 10 ++++++++-- src/google/protobuf/util/internal/json_objectwriter.h | 8 ++++++++ src/google/protobuf/util/json_util.cc | 1 + src/google/protobuf/util/json_util.h | 5 ++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/google/protobuf/util/internal/json_objectwriter.cc b/src/google/protobuf/util/internal/json_objectwriter.cc index 6e4edd88..c36459a4 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.cc +++ b/src/google/protobuf/util/internal/json_objectwriter.cc @@ -176,8 +176,14 @@ void JsonObjectWriter::WritePrefix(StringPiece name) { if (!name.empty() || empty_key_ok) { WriteChar('"'); if (!name.empty()) { - ArrayByteSource source(name); - JsonEscaping::Escape(&source, &sink_); + if (use_snake_case_for_field_names_) { + string snake_name = ToSnakeCase(name); + ArrayByteSource source(snake_name); + JsonEscaping::Escape(&source, &sink_); + } else { + ArrayByteSource source(name); + JsonEscaping::Escape(&source, &sink_); + } } stream_->WriteString("\":"); if (!indent_string_.empty()) WriteChar(' '); diff --git a/src/google/protobuf/util/internal/json_objectwriter.h b/src/google/protobuf/util/internal/json_objectwriter.h index 31edc292..fbef5461 100644 --- a/src/google/protobuf/util/internal/json_objectwriter.h +++ b/src/google/protobuf/util/internal/json_objectwriter.h @@ -94,6 +94,7 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { sink_(out), indent_string_(indent_string.ToString()), use_websafe_base64_for_bytes_(false), + use_snake_case_for_field_names_(false), empty_name_ok_for_next_key_(false) {} virtual ~JsonObjectWriter(); @@ -118,6 +119,10 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { use_websafe_base64_for_bytes_ = value; } + void set_use_snake_case_for_field_names(bool value) { + use_snake_case_for_field_names_ = value; + } + // Whether empty strings should be rendered for the next JSON key. This // setting is only valid until the next key is rendered, after which it gets // reset to false. @@ -217,6 +222,9 @@ class LIBPROTOBUF_EXPORT JsonObjectWriter : public StructuredObjectWriter { // to regular base64 encoding. bool use_websafe_base64_for_bytes_; + // Whether to use snake_case or lowerCamelCase for field names + bool use_snake_case_for_field_names_; + // Whether empty strings should be rendered for the next JSON key. This // setting is only valid until the next key is rendered, after which it gets // reset to false. diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index 129b6eaf..c86cb8cb 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -86,6 +86,7 @@ util::Status BinaryToJsonStream(TypeResolver* resolver, io::CodedOutputStream out_stream(json_output); converter::JsonObjectWriter json_writer(options.add_whitespace ? " " : "", &out_stream); + json_writer.set_use_snake_case_for_field_names(options.use_snake_case_for_field_names); if (options.always_print_primitive_fields) { converter::DefaultValueObjectWriter default_value_writer( resolver, type, &json_writer); diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h index 53b1d1ba..439d137f 100644 --- a/src/google/protobuf/util/json_util.h +++ b/src/google/protobuf/util/json_util.h @@ -64,10 +64,13 @@ struct JsonPrintOptions { // Whether to always print enums as ints. By default they are rendered as // strings. bool always_print_enums_as_ints; + // Whether to convert field names to snake case + bool use_snake_case_for_field_names; JsonPrintOptions() : add_whitespace(false), always_print_primitive_fields(false), - always_print_enums_as_ints(false) { + always_print_enums_as_ints(false), + use_snake_case_for_field_names(false) { } }; -- cgit v1.2.3