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
b01b1a8e
Commit
b01b1a8e
authored
May 17, 2016
by
Jie Luo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
JSON format for Any message must print @type first, use OrderedDict instead of {}
parent
e4ca694c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
1 deletion
+22
-1
json_format_test.py
python/google/protobuf/internal/json_format_test.py
+16
-0
json_format.py
python/google/protobuf/json_format.py
+6
-1
No files found.
python/google/protobuf/internal/json_format_test.py
View file @
b01b1a8e
...
...
@@ -458,6 +458,22 @@ class JsonFormatTest(JsonFormatBase):
'}
\n
'
))
parsed_message
=
json_format_proto3_pb2
.
TestAny
()
self
.
CheckParseBack
(
message
,
parsed_message
)
# Must print @type first
test_message
=
json_format_proto3_pb2
.
TestMessage
(
bool_value
=
True
,
int32_value
=
20
,
int64_value
=-
20
,
uint32_value
=
20
,
uint64_value
=
20
,
double_value
=
3.14
,
string_value
=
'foo'
)
message
.
Clear
()
message
.
value
.
Pack
(
test_message
)
self
.
assertEqual
(
json_format
.
MessageToJson
(
message
,
False
)[
0
:
68
],
'{
\n
'
' "value": {
\n
'
' "@type": "type.googleapis.com/proto3.TestMessage"'
)
def
testWellKnownInAnyMessage
(
self
):
message
=
any_pb2
.
Any
()
...
...
python/google/protobuf/json_format.py
View file @
b01b1a8e
...
...
@@ -42,6 +42,10 @@ Simple usage example:
__author__
=
'jieluo@google.com (Jie Luo)'
try
:
from
collections
import
OrderedDict
except
ImportError
:
from
ordereddict
import
OrderedDict
#PY26
import
base64
import
json
import
math
...
...
@@ -208,7 +212,8 @@ def _AnyMessageToJsonObject(message, including_default):
"""Converts Any message according to Proto3 JSON Specification."""
if
not
message
.
ListFields
():
return
{}
js
=
{}
# Must print @type first, use OrderedDict instead of {}
js
=
OrderedDict
()
type_url
=
message
.
type_url
js
[
'@type'
]
=
type_url
sub_message
=
_CreateMessageFromTypeUrl
(
type_url
)
...
...
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