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
52db5139
Commit
52db5139
authored
Jan 15, 2016
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change handling of unknown enums: we now write out the value as a number.
parent
f437b67f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
36 deletions
+23
-36
JsonFormatterTest.cs
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
+8
-11
JsonParserTest.cs
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
+5
-5
JsonFormatter.cs
csharp/src/Google.Protobuf/JsonFormatter.cs
+8
-14
JsonParser.cs
csharp/src/Google.Protobuf/JsonParser.cs
+2
-6
No files found.
csharp/src/Google.Protobuf.Test/JsonFormatterTest.cs
View file @
52db5139
...
@@ -165,34 +165,31 @@ namespace Google.Protobuf
...
@@ -165,34 +165,31 @@ namespace Google.Protobuf
}
}
[
Test
]
[
Test
]
public
void
UnknownEnumValue
Omitted
_SingleField
()
public
void
UnknownEnumValue
Numeric
_SingleField
()
{
{
var
message
=
new
TestAllTypes
{
SingleForeignEnum
=
(
ForeignEnum
)
100
};
var
message
=
new
TestAllTypes
{
SingleForeignEnum
=
(
ForeignEnum
)
100
};
AssertJson
(
"{ }"
,
JsonFormatter
.
Default
.
Format
(
message
));
AssertJson
(
"{
'singleForeignEnum': 100
}"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
}
[
Test
]
[
Test
]
public
void
UnknownEnumValue
Omitted
_RepeatedField
()
public
void
UnknownEnumValue
Numeric
_RepeatedField
()
{
{
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
ForeignEnum
.
FOREIGN_BAZ
,
(
ForeignEnum
)
100
,
ForeignEnum
.
FOREIGN_FOO
}
};
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
ForeignEnum
.
FOREIGN_BAZ
,
(
ForeignEnum
)
100
,
ForeignEnum
.
FOREIGN_FOO
}
};
AssertJson
(
"{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ', 'FOREIGN_FOO' ] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
AssertJson
(
"{ 'repeatedForeignEnum': [ 'FOREIGN_BAZ',
100,
'FOREIGN_FOO' ] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
}
[
Test
]
[
Test
]
public
void
UnknownEnumValue
Omitted
_MapField
()
public
void
UnknownEnumValue
Numeric
_MapField
()
{
{
// This matches the C++ behaviour.
var
message
=
new
TestMap
{
MapInt32Enum
=
{
{
1
,
MapEnum
.
MAP_ENUM_FOO
},
{
2
,
(
MapEnum
)
100
},
{
3
,
MapEnum
.
MAP_ENUM_BAR
}
}
};
var
message
=
new
TestMap
{
MapInt32Enum
=
{
{
1
,
MapEnum
.
MAP_ENUM_FOO
},
{
2
,
(
MapEnum
)
100
},
{
3
,
MapEnum
.
MAP_ENUM_BAR
}
}
};
AssertJson
(
"{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '3': 'MAP_ENUM_BAR' } }"
,
JsonFormatter
.
Default
.
Format
(
message
));
AssertJson
(
"{ 'mapInt32Enum': { '1': 'MAP_ENUM_FOO', '
2': 100, '
3': 'MAP_ENUM_BAR' } }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
}
[
Test
]
[
Test
]
public
void
UnknownEnumValue
Omitted
_RepeatedField_AllEntriesUnknown
()
public
void
UnknownEnumValue_RepeatedField_AllEntriesUnknown
()
{
{
// *Maybe* we should hold off on writing the "[" until we find that we've got at least one value to write...
// but this is what happens at the moment, and it doesn't seem too awful.
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
(
ForeignEnum
)
200
,
(
ForeignEnum
)
100
}
};
var
message
=
new
TestAllTypes
{
RepeatedForeignEnum
=
{
(
ForeignEnum
)
200
,
(
ForeignEnum
)
100
}
};
AssertJson
(
"{ 'repeatedForeignEnum': [ ] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
AssertJson
(
"{ 'repeatedForeignEnum': [
200, 100
] }"
,
JsonFormatter
.
Default
.
Format
(
message
));
}
}
[
Test
]
[
Test
]
...
...
csharp/src/Google.Protobuf.Test/JsonParserTest.cs
View file @
52db5139
...
@@ -876,18 +876,18 @@ namespace Google.Protobuf
...
@@ -876,18 +876,18 @@ namespace Google.Protobuf
}
}
[
Test
]
[
Test
]
[
TestCase
(
"\"FOREIGN_BAR\""
)]
[
TestCase
(
"\"FOREIGN_BAR\""
,
ForeignEnum
.
FOREIGN_BAR
)]
[
TestCase
(
"5"
)]
[
TestCase
(
"5"
,
ForeignEnum
.
FOREIGN_BAR
)]
public
void
EnumValid
(
string
value
)
[
TestCase
(
"100"
,
(
ForeignEnum
)
100
)]
public
void
EnumValid
(
string
value
,
ForeignEnum
expectedValue
)
{
{
string
json
=
"{ \"singleForeignEnum\": "
+
value
+
" }"
;
string
json
=
"{ \"singleForeignEnum\": "
+
value
+
" }"
;
var
parsed
=
TestAllTypes
.
Parser
.
ParseJson
(
json
);
var
parsed
=
TestAllTypes
.
Parser
.
ParseJson
(
json
);
Assert
.
AreEqual
(
new
TestAllTypes
{
SingleForeignEnum
=
ForeignEnum
.
FOREIGN_BAR
},
parsed
);
Assert
.
AreEqual
(
new
TestAllTypes
{
SingleForeignEnum
=
expectedValue
},
parsed
);
}
}
[
Test
]
[
Test
]
[
TestCase
(
"\"NOT_A_VALID_VALUE\""
)]
[
TestCase
(
"\"NOT_A_VALID_VALUE\""
)]
[
TestCase
(
"100"
)]
[
TestCase
(
"5.5"
)]
[
TestCase
(
"5.5"
)]
public
void
Enum_Invalid
(
string
value
)
public
void
Enum_Invalid
(
string
value
)
{
{
...
...
csharp/src/Google.Protobuf/JsonFormatter.cs
View file @
52db5139
...
@@ -205,11 +205,6 @@ namespace Google.Protobuf
...
@@ -205,11 +205,6 @@ namespace Google.Protobuf
{
{
continue
;
continue
;
}
}
// Omit awkward (single) values such as unknown enum values
if
(!
field
.
IsRepeated
&&
!
field
.
IsMap
&&
!
CanWriteSingleValue
(
value
))
{
continue
;
}
// Okay, all tests complete: let's write the field value...
// Okay, all tests complete: let's write the field value...
if
(!
first
)
if
(!
first
)
...
@@ -397,7 +392,14 @@ namespace Google.Protobuf
...
@@ -397,7 +392,14 @@ namespace Google.Protobuf
}
}
else
if
(
value
is
System
.
Enum
)
else
if
(
value
is
System
.
Enum
)
{
{
WriteString
(
builder
,
value
.
ToString
());
if
(
System
.
Enum
.
IsDefined
(
value
.
GetType
(),
value
))
{
WriteString
(
builder
,
value
.
ToString
());
}
else
{
WriteValue
(
builder
,
(
int
)
value
);
}
}
}
else
if
(
value
is
float
||
value
is
double
)
else
if
(
value
is
float
||
value
is
double
)
{
{
...
@@ -704,10 +706,6 @@ namespace Google.Protobuf
...
@@ -704,10 +706,6 @@ namespace Google.Protobuf
bool
first
=
true
;
bool
first
=
true
;
foreach
(
var
value
in
list
)
foreach
(
var
value
in
list
)
{
{
if
(!
CanWriteSingleValue
(
value
))
{
continue
;
}
if
(!
first
)
if
(!
first
)
{
{
builder
.
Append
(
PropertySeparator
);
builder
.
Append
(
PropertySeparator
);
...
@@ -725,10 +723,6 @@ namespace Google.Protobuf
...
@@ -725,10 +723,6 @@ namespace Google.Protobuf
// This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal.
// This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal.
foreach
(
DictionaryEntry
pair
in
dictionary
)
foreach
(
DictionaryEntry
pair
in
dictionary
)
{
{
if
(!
CanWriteSingleValue
(
pair
.
Value
))
{
continue
;
}
if
(!
first
)
if
(!
first
)
{
{
builder
.
Append
(
PropertySeparator
);
builder
.
Append
(
PropertySeparator
);
...
...
csharp/src/Google.Protobuf/JsonParser.cs
View file @
52db5139
...
@@ -617,13 +617,9 @@ namespace Google.Protobuf
...
@@ -617,13 +617,9 @@ namespace Google.Protobuf
return
(
float
)
value
;
return
(
float
)
value
;
case
FieldType
.
Enum
:
case
FieldType
.
Enum
:
CheckInteger
(
value
);
CheckInteger
(
value
);
var
enumValue
=
field
.
EnumType
.
FindValueByNumber
((
int
)
value
);
if
(
enumValue
==
null
)
{
throw
new
InvalidProtocolBufferException
(
$"Invalid enum value:
{
value
}
for enum type:
{
field
.
EnumType
.
FullName
}
"
);
}
// Just return it as an int, and let the CLR convert it.
// Just return it as an int, and let the CLR convert it.
return
enumValue
.
Number
;
// Note that we deliberately don't check that it's a known value.
return
(
int
)
value
;
default
:
default
:
throw
new
InvalidProtocolBufferException
(
$"Unsupported conversion from JSON number for field type
{
field
.
FieldType
}
"
);
throw
new
InvalidProtocolBufferException
(
$"Unsupported conversion from JSON number for field type
{
field
.
FieldType
}
"
);
}
}
...
...
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