Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
1eee3202
Commit
1eee3202
authored
Mar 17, 2017
by
Brendan McCarthy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add use_snake_case_for_field_names option to JsonPrintOptions
parent
44dc5558
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
21 additions
and
3 deletions
+21
-3
json_objectwriter.cc
src/google/protobuf/util/internal/json_objectwriter.cc
+8
-2
json_objectwriter.h
src/google/protobuf/util/internal/json_objectwriter.h
+8
-0
json_util.cc
src/google/protobuf/util/json_util.cc
+1
-0
json_util.h
src/google/protobuf/util/json_util.h
+4
-1
No files found.
src/google/protobuf/util/internal/json_objectwriter.cc
View file @
1eee3202
...
...
@@ -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
(
' '
);
...
...
src/google/protobuf/util/internal/json_objectwriter.h
View file @
1eee3202
...
...
@@ -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.
...
...
src/google/protobuf/util/json_util.cc
View file @
1eee3202
...
...
@@ -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
);
...
...
src/google/protobuf/util/json_util.h
View file @
1eee3202
...
...
@@ -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
)
{
}
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment