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
022a9b26
Commit
022a9b26
authored
Jan 15, 2016
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow the original field name (rather than camel-cased) when parsing JSON
parent
1fc48592
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
3 deletions
+23
-3
JsonParserTest.cs
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
+8
-0
MessageDescriptor.cs
csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
+15
-3
No files found.
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
View file @
022a9b26
...
...
@@ -71,6 +71,14 @@ namespace Google.Protobuf
Assert
.
Throws
<
InvalidProtocolBufferException
>(()
=>
JsonParser
.
Default
.
Parse
<
TestMap
>(
json
));
}
[
Test
]
public
void
OriginalFieldNameAccepted
()
{
var
json
=
"{ \"single_int32\": 10 }"
;
var
expected
=
new
TestAllTypes
{
SingleInt32
=
10
};
Assert
.
AreEqual
(
expected
,
TestAllTypes
.
Parser
.
ParseJson
(
json
));
}
[
Test
]
public
void
SourceContextRoundtrip
()
{
...
...
csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
View file @
022a9b26
...
...
@@ -92,11 +92,22 @@ namespace Google.Protobuf.Reflection
new
FieldDescriptor
(
field
,
file
,
this
,
index
,
generatedCodeInfo
?.
PropertyNames
[
index
]));
fieldsInNumberOrder
=
new
ReadOnlyCollection
<
FieldDescriptor
>(
fieldsInDeclarationOrder
.
OrderBy
(
field
=>
field
.
FieldNumber
).
ToArray
());
// TODO: Use field => field.Proto.JsonName when we're confident it's appropriate. (And then use it in the formatter, too.)
jsonFieldMap
=
new
ReadOnlyDictionary
<
string
,
FieldDescriptor
>(
fieldsInNumberOrder
.
ToDictionary
(
field
=>
JsonFormatter
.
ToCamelCase
(
field
.
Name
))
);
jsonFieldMap
=
CreateJsonFieldMap
(
fieldsInNumberOrder
);
file
.
DescriptorPool
.
AddSymbol
(
this
);
Fields
=
new
FieldCollection
(
this
);
}
private
static
ReadOnlyDictionary
<
string
,
FieldDescriptor
>
CreateJsonFieldMap
(
IList
<
FieldDescriptor
>
fields
)
{
var
map
=
new
Dictionary
<
string
,
FieldDescriptor
>();
foreach
(
var
field
in
fields
)
{
map
[
JsonFormatter
.
ToCamelCase
(
field
.
Name
)]
=
field
;
map
[
field
.
Name
]
=
field
;
}
return
new
ReadOnlyDictionary
<
string
,
FieldDescriptor
>(
map
);
}
/// <summary>
/// The brief name of the descriptor's target.
/// </summary>
...
...
@@ -255,9 +266,10 @@ namespace Google.Protobuf.Reflection
// TODO: consider making this public in the future. (Being conservative for now...)
/// <value>
/// Returns a read-only dictionary mapping the field names in this message as they're
used
/// Returns a read-only dictionary mapping the field names in this message as they're
available
/// in the JSON representation to the field descriptors. For example, a field <c>foo_bar</c>
/// in the message would result in an entry with a key <c>fooBar</c>.
/// in the message would result two entries, one with a key <c>fooBar</c> and one with a key
/// <c>foo_bar</c>, both referring to the same field.
/// </value>
internal
IDictionary
<
string
,
FieldDescriptor
>
ByJsonName
()
=>
messageDescriptor
.
jsonFieldMap
;
...
...
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