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
790f4c8e
Commit
790f4c8e
authored
Apr 08, 2016
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use the original name in JSON formatting.
(JSON parsing already does the right thing.)
parent
84ea2c7a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
2 deletions
+45
-2
Google.Protobuf.nuspec
csharp/src/Google.Protobuf/Google.Protobuf.nuspec
+2
-0
JsonFormatter.cs
csharp/src/Google.Protobuf/JsonFormatter.cs
+43
-2
No files found.
csharp/src/Google.Protobuf/Google.Protobuf.nuspec
View file @
790f4c8e
...
...
@@ -33,10 +33,12 @@
<dependency
id=
"System.Linq.Expressions"
version=
"4.0.0"
/>
<dependency
id=
"System.ObjectModel"
version=
"4.0.0"
/>
<dependency
id=
"System.Reflection"
version=
"4.0.0"
/>
<dependency
id=
"System.Reflection.Extensions"
version=
"4.0.0"
/>
<dependency
id=
"System.Runtime"
version=
"4.0.0"
/>
<dependency
id=
"System.Runtime.Extensions"
version=
"4.0.0"
/>
<dependency
id=
"System.Text.Encoding"
version=
"4.0.0"
/>
<dependency
id=
"System.Text.RegularExpressions"
version=
"4.0.0"
/>
<dependency
id=
"System.Threading"
version=
"4.0.0"
/>
</group>
</dependencies>
</metadata>
...
...
csharp/src/Google.Protobuf/JsonFormatter.cs
View file @
790f4c8e
...
...
@@ -39,6 +39,7 @@ using Google.Protobuf.WellKnownTypes;
using
System.IO
;
using
System.Linq
;
using
System.Collections.Generic
;
using
System.Reflection
;
namespace
Google.Protobuf
{
...
...
@@ -420,9 +421,10 @@ namespace Google.Protobuf
}
else
if
(
value
is
System
.
Enum
)
{
if
(
System
.
Enum
.
IsDefined
(
value
.
GetType
(),
value
))
string
name
=
OriginalEnumValueHelper
.
GetOriginalName
(
value
);
if
(
name
!=
null
)
{
WriteString
(
writer
,
value
.
ToString
()
);
WriteString
(
writer
,
name
);
}
else
{
...
...
@@ -877,5 +879,44 @@ namespace Google.Protobuf
TypeRegistry
=
ProtoPreconditions
.
CheckNotNull
(
typeRegistry
,
nameof
(
typeRegistry
));
}
}
// Effectively a cache of mapping from enum values to the original name as specified in the proto file,
// fetched by reflection.
// The need for this is unfortunate, as is its unbounded size, but realistically it shouldn't cause issues.
private
static
class
OriginalEnumValueHelper
{
// TODO: In the future we might want to use ConcurrentDictionary, at the point where all
// the platforms we target have it.
private
static
readonly
Dictionary
<
System
.
Type
,
Dictionary
<
object
,
string
>>
dictionaries
=
new
Dictionary
<
System
.
Type
,
Dictionary
<
object
,
string
>>();
internal
static
string
GetOriginalName
(
object
value
)
{
var
enumType
=
value
.
GetType
();
Dictionary
<
object
,
string
>
nameMapping
;
lock
(
dictionaries
)
{
if
(!
dictionaries
.
TryGetValue
(
enumType
,
out
nameMapping
))
{
nameMapping
=
GetNameMapping
(
enumType
);
dictionaries
[
enumType
]
=
nameMapping
;
}
}
string
originalName
;
// If this returns false, originalName will be null, which is what we want.
nameMapping
.
TryGetValue
(
value
,
out
originalName
);
return
originalName
;
}
private
static
Dictionary
<
object
,
string
>
GetNameMapping
(
System
.
Type
enumType
)
=>
enumType
.
GetTypeInfo
().
DeclaredFields
.
Where
(
f
=>
f
.
IsStatic
)
.
ToDictionary
(
f
=>
f
.
GetValue
(
null
),
f
=>
f
.
GetCustomAttributes
<
OriginalNameAttribute
>()
.
FirstOrDefault
()
// If the attribute hasn't been applied, fall back to the name of the field.
?.
Name
??
f
.
Name
);
}
}
}
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