-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
|
||||
}
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System.Diagnostics;
|
||||
#if !(NET35 || NET20 || PORTABLE40)
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -52,6 +53,54 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
[TestFixture]
|
||||
public class DynamicTests : TestFixtureBase
|
||||
{
|
||||
private void UpdateValueCount(IDictionary<string, int> counts, dynamic d)
|
||||
{
|
||||
string s = d.ToString();
|
||||
|
||||
int c;
|
||||
if (!counts.TryGetValue(s, out c))
|
||||
{
|
||||
c = 0;
|
||||
}
|
||||
|
||||
c++;
|
||||
counts[s] = c;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeLargeDynamic()
|
||||
{
|
||||
dynamic d;
|
||||
|
||||
using (var jsonFile = System.IO.File.OpenText("large.json"))
|
||||
using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
|
||||
{
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
d = serializer.Deserialize(jsonTextReader);
|
||||
}
|
||||
|
||||
IDictionary<string, int> counts = new Dictionary<string, int>();
|
||||
|
||||
Stopwatch sw = new Stopwatch();
|
||||
sw.Start();
|
||||
|
||||
foreach (dynamic o in d)
|
||||
{
|
||||
foreach (dynamic friend in o.friends)
|
||||
{
|
||||
UpdateValueCount(counts, friend.id);
|
||||
UpdateValueCount(counts, ((string) friend.name).Split(' ')[0]);
|
||||
}
|
||||
}
|
||||
|
||||
//foreach (KeyValuePair<string, int> keyValuePair in counts)
|
||||
//{
|
||||
// Console.WriteLine(keyValuePair.Key + ": " + keyValuePair.Value);
|
||||
//}
|
||||
|
||||
Console.WriteLine("Time (secs): " + sw.Elapsed.TotalSeconds);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JObjectPropertyNames()
|
||||
{
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
#if !(NET20 || NET35 || PORTABLE || ASPNETCORE50)
|
||||
using System.Numerics;
|
||||
@@ -792,6 +793,20 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
Assert.AreEqual(JTokenType.Integer, v.Type);
|
||||
StringComparison e6 = v.ToObject<StringComparison>();
|
||||
Assert.AreEqual(StringComparison.OrdinalIgnoreCase, e6);
|
||||
|
||||
// does not support EnumMember. breaking change to add
|
||||
ExceptionAssert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
d = new JValue("value_a");
|
||||
EnumA e7 = (EnumA)d;
|
||||
Assert.AreEqual(EnumA.ValueA, e7);
|
||||
}, "Requested value 'value_a' was not found.");
|
||||
}
|
||||
|
||||
public enum EnumA
|
||||
{
|
||||
[EnumMember(Value = "value_a")]
|
||||
ValueA
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -74,4 +74,4 @@ using System.Security;
|
||||
// by using the '*' as shown below:
|
||||
|
||||
[assembly: AssemblyVersion("6.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17911")]
|
||||
[assembly: AssemblyFileVersion("6.0.6.17924")]
|
||||
|
||||
@@ -95,6 +95,24 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
[TestFixture]
|
||||
public class JsonSerializerTest : TestFixtureBase
|
||||
{
|
||||
#if !NET20
|
||||
public enum EnumA
|
||||
{
|
||||
[EnumMember(Value = "value_a")]
|
||||
ValueA
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeEnumsByName()
|
||||
{
|
||||
var e1 = JsonConvert.DeserializeObject<EnumA>("'ValueA'");
|
||||
Assert.AreEqual(EnumA.ValueA, e1);
|
||||
|
||||
var e2 = JsonConvert.DeserializeObject<EnumA>("'value_a'", new StringEnumConverter());
|
||||
Assert.AreEqual(EnumA.ValueA, e2);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(NETFX_CORE || ASPNETCORE50 || NET20)
|
||||
[MetadataType(typeof(CustomerValidation))]
|
||||
public partial class CustomerWithMetadataType
|
||||
|
||||
Reference in New Issue
Block a user