From c90e6e871ae39cd4686dac6fa64a780e527123a8 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Wed, 31 Jan 2018 15:14:03 +1300 Subject: [PATCH] -Changed enum serialization in dictionary keys to use EnumMemberAttribute --- Src/Newtonsoft.Json.Tests/Issues/Issue1597.cs | 124 ++++++++++++++++++ .../Linq/LinqToJsonTest.cs | 6 +- .../JsonSerializerInternalReader.cs | 2 +- .../JsonSerializerInternalWriter.cs | 6 + 4 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 Src/Newtonsoft.Json.Tests/Issues/Issue1597.cs diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue1597.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue1597.cs new file mode 100644 index 00000000..4d2bdda5 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue1597.cs @@ -0,0 +1,124 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +#if !(NET20 || NET35 || NET40 || PORTABLE40 || PORTABLE) || NETSTANDARD1_0 || NETSTANDARD1_3 || NETSTANDARD2_0 +using System; +using System.Collections; +using System.Collections.Generic; +using System.Dynamic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json.Serialization; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + [TestFixture] + public class Issue1597 : TestFixtureBase + { + [Test] + public void Test() + { + string json = @"{ + ""wish"": 264, + ""collect"": 7498, + ""doing"": 385, + ""on_hold"": 285, + ""dropped"": 221 +}"; + + IReadOnlyDictionary o = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(264, o[CollectionStatus.Wish]); + Assert.AreEqual(7498, o[CollectionStatus.Collect]); + Assert.AreEqual(385, o[CollectionStatus.Doing]); + Assert.AreEqual(285, o[CollectionStatus.OnHold]); + Assert.AreEqual(221, o[CollectionStatus.Dropped]); + } + + [Test] + public void Test_WithNumbers() + { + string json = @"{ + ""0"": 264, + ""1"": 7498, + ""2"": 385, + ""3"": 285, + ""4"": 221 +}"; + + IReadOnlyDictionary o = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(264, o[CollectionStatus.Wish]); + Assert.AreEqual(7498, o[CollectionStatus.Collect]); + Assert.AreEqual(385, o[CollectionStatus.Doing]); + Assert.AreEqual(285, o[CollectionStatus.OnHold]); + Assert.AreEqual(221, o[CollectionStatus.Dropped]); + } + + [Test] + public void Test_Serialize() + { + Dictionary o = new Dictionary(); + o[CollectionStatus.Wish] = 264; + o[CollectionStatus.Collect] = 7498; + o[CollectionStatus.Doing] = 385; + o[CollectionStatus.OnHold] = 285; + o[CollectionStatus.Dropped] = 221; + o[(CollectionStatus)int.MaxValue] = int.MaxValue; + + string json = JsonConvert.SerializeObject(o, Formatting.Indented); + + StringAssert.AreEqual(@"{ + ""Wish"": 264, + ""Collect"": 7498, + ""Doing"": 385, + ""on_hold"": 285, + ""Dropped"": 221, + ""2147483647"": 2147483647 +}", json); + } + + public enum CollectionStatus + { + Wish, + Collect, + Doing, + [EnumMember(Value = "on_hold")] + OnHold, + Dropped + } + } +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 3e52c8ff..ab535c77 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -1451,10 +1451,8 @@ keyword such as type of business."" { JObject o = JObject.Parse("{'FooBarNoEnum':'SOME_OTHER_VALUE'}"); - ExceptionAssert.Throws(() => - { - o["FooBarNoEnum"].ToObject(); - }, "Could not convert 'SOME_OTHER_VALUE' to FooBarNoEnum."); + FooBarNoEnum e = o["FooBarNoEnum"].ToObject(); + Assert.AreEqual(FooBarNoEnum.SomeOtherValue, e); } #endif diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index f078c7b8..cb86bf5a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -955,7 +955,7 @@ namespace Newtonsoft.Json.Serialization { if (value is string) { - return Enum.Parse(contract.NonNullableUnderlyingType, value.ToString(), true); + return EnumUtils.ParseEnum(contract.NonNullableUnderlyingType, value.ToString(), false); } if (ConvertUtils.IsInteger(primitiveContract.TypeCode)) { diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index 51d8b2f9..46d3f015 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -1150,6 +1150,12 @@ namespace Newtonsoft.Json.Serialization default: { escape = true; + + if (primitiveContract.IsEnum && EnumUtils.TryToString(primitiveContract.NonNullableUnderlyingType, name, false, out string enumName)) + { + return enumName; + } + return Convert.ToString(name, CultureInfo.InvariantCulture); } }