-Refactor
This commit is contained in:
@@ -445,6 +445,14 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
Assert.AreEqual(Bar.SerializeAsBaz, bars[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DuplicateNameEnumTest()
|
||||
{
|
||||
ExceptionAssert.Throws<JsonSerializationException>(
|
||||
() => JsonConvert.DeserializeObject<DuplicateNameEnum>("'foo_bar'", new StringEnumConverter()),
|
||||
@"Error converting value ""foo_bar"" to type 'Newtonsoft.Json.Tests.Converters.DuplicateNameEnum'. Path '', line 1, position 9.");
|
||||
}
|
||||
|
||||
// Define other methods and classes here
|
||||
[Flags]
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
@@ -468,6 +476,74 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
[EnumMember(Value = "baz")]
|
||||
SerializeAsBaz
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DataContractSerializerDuplicateNameEnumTest()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
var s = new DataContractSerializer(typeof(DuplicateEnumNameTestClass));
|
||||
|
||||
ExceptionAssert.Throws<InvalidDataContractException>(() =>
|
||||
{
|
||||
s.WriteObject(ms, new DuplicateEnumNameTestClass
|
||||
{
|
||||
Value = DuplicateNameEnum.foo_bar,
|
||||
Value2 = DuplicateNameEnum2.foo_bar_NOT_USED
|
||||
});
|
||||
|
||||
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
|
||||
|
||||
string xml = @"<DuplicateEnumNameTestClass xmlns=""http://schemas.datacontract.org/2004/07/Newtonsoft.Json.Tests.Converters"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">
|
||||
<Value>foo_bar</Value>
|
||||
<Value2>foo_bar</Value2>
|
||||
</DuplicateEnumNameTestClass>";
|
||||
|
||||
Console.WriteLine(xml);
|
||||
|
||||
var o = (DuplicateEnumNameTestClass)s.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(xml)));
|
||||
|
||||
Assert.AreEqual(DuplicateNameEnum.foo_bar, o.Value);
|
||||
Assert.AreEqual(DuplicateNameEnum2.FooBar, o.Value2);
|
||||
|
||||
}, "Type 'Newtonsoft.Json.Tests.Converters.DuplicateNameEnum' contains two members 'foo_bar' 'and 'FooBar' with the same name 'foo_bar'. Multiple members with the same name in one type are not supported. Consider changing one of the member names using EnumMemberAttribute attribute.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !NET20
|
||||
[DataContract]
|
||||
public class DuplicateEnumNameTestClass
|
||||
{
|
||||
[DataMember]
|
||||
public DuplicateNameEnum Value { get; set; }
|
||||
[DataMember]
|
||||
public DuplicateNameEnum2 Value2 { get; set; }
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public enum DuplicateNameEnum
|
||||
{
|
||||
[EnumMember]
|
||||
first = 0,
|
||||
[EnumMember]
|
||||
foo_bar = 1,
|
||||
[EnumMember(Value = "foo_bar")]
|
||||
FooBar = 2,
|
||||
[EnumMember]
|
||||
foo_bar_NOT_USED = 3
|
||||
}
|
||||
|
||||
[DataContract]
|
||||
public enum DuplicateNameEnum2
|
||||
{
|
||||
[EnumMember]
|
||||
first = 0,
|
||||
[EnumMember(Value = "foo_bar")]
|
||||
FooBar = 1,
|
||||
[EnumMember]
|
||||
foo_bar = 2,
|
||||
[EnumMember(Value = "TEST")]
|
||||
foo_bar_NOT_USED = 3
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user