forked from facebook-csharp-sdk/simple-json
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialize and deserialize Enum values as strings
- Loading branch information
Showing
5 changed files
with
227 additions
and
20 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
src/SimpleJson.Tests/PocoDeserializerTests/EnumDeserializeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="<file>.cs" company="The Outercurve Foundation"> | ||
// Copyright (c) 2011, The Outercurve Foundation. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.opensource.org/licenses/mit-license.php | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author> | ||
// <website>https://github.com/facebook-csharp-sdk/simple-json</website> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace SimpleJson.Tests.PocoDeserializerTests | ||
{ | ||
using System; | ||
|
||
#if NUNIT | ||
using TestClass = NUnit.Framework.TestFixtureAttribute; | ||
using TestMethod = NUnit.Framework.TestAttribute; | ||
using TestCleanup = NUnit.Framework.TearDownAttribute; | ||
using TestInitialize = NUnit.Framework.SetUpAttribute; | ||
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute; | ||
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute; | ||
using NUnit.Framework; | ||
#else | ||
#if NETFX_CORE | ||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; | ||
#else | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
#endif | ||
#endif | ||
|
||
[TestClass] | ||
public class EnumDeserializeTests | ||
{ | ||
[TestMethod] | ||
public void DeserializeEnumStringValue() | ||
{ | ||
var json = "{\"Value\":\"Two\"}"; | ||
|
||
var result = SimpleJson.DeserializeObject<X>(json); | ||
Assert.AreEqual(Values.Two, result.Value); | ||
} | ||
|
||
public class X | ||
{ | ||
public Values Value { get; set; } | ||
} | ||
} | ||
|
||
|
||
[TestClass] | ||
public class NullableEnumDeserializeTests | ||
{ | ||
[TestMethod] | ||
public void DeserializeNullEnumValue() | ||
{ | ||
var json = "{}"; | ||
|
||
var result = SimpleJson.DeserializeObject<X>(json); | ||
|
||
Assert.IsNull(result.Value); | ||
} | ||
|
||
[TestMethod] | ||
public void DeserializeNullValue() | ||
{ | ||
var json = "{\"Value\":null}"; | ||
|
||
var result = SimpleJson.DeserializeObject<X>(json); | ||
Assert.IsNull(result.Value); | ||
} | ||
|
||
[TestMethod] | ||
public void DeserializeNullableEnumWithValue() | ||
{ | ||
var json = "{\"Value\":\"Two\"}"; | ||
var result = SimpleJson.DeserializeObject<X>(json); | ||
Assert.AreEqual(Values.Two, result.Value); | ||
} | ||
|
||
public class X | ||
{ | ||
public Values? Value { get; set; } | ||
} | ||
} | ||
|
||
public enum Values | ||
{ | ||
One, | ||
Two, | ||
Three | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/SimpleJson.Tests/PocoJsonSerializerTests/EnumSerializeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="<file>.cs" company="The Outercurve Foundation"> | ||
// Copyright (c) 2011, The Outercurve Foundation. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.opensource.org/licenses/mit-license.php | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
// <author>Nathan Totten (ntotten.com), Jim Zimmerman (jimzimmerman.com) and Prabir Shrestha (prabir.me)</author> | ||
// <website>https://github.com/facebook-csharp-sdk/simple-json</website> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace SimpleJson.Tests.PocoJsonSerializerTests | ||
{ | ||
using System; | ||
|
||
#if NUNIT | ||
using TestClass = NUnit.Framework.TestFixtureAttribute; | ||
using TestMethod = NUnit.Framework.TestAttribute; | ||
using TestCleanup = NUnit.Framework.TearDownAttribute; | ||
using TestInitialize = NUnit.Framework.SetUpAttribute; | ||
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute; | ||
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute; | ||
using NUnit.Framework; | ||
#else | ||
#if NETFX_CORE | ||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; | ||
#else | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
#endif | ||
#endif | ||
|
||
[TestClass] | ||
public class EnumSerializeTests | ||
{ | ||
[TestMethod] | ||
public void SerializeEnumAsStringValue() | ||
{ | ||
var obj = new SerializeEnumClass() | ||
{ | ||
Value = Values.Two | ||
}; | ||
|
||
var json = SimpleJson.SerializeObject(obj); | ||
|
||
Assert.AreEqual("{\"Value\":\"Two\"}", json); | ||
} | ||
|
||
public class SerializeEnumClass | ||
{ | ||
public Values Value { get; set; } | ||
} | ||
} | ||
|
||
[TestClass] | ||
public class NullableEnumSerializeTest | ||
{ | ||
[TestMethod] | ||
public void SerializeNullableEnumWithNoValue() | ||
{ | ||
var obj = new SerializeNullableEnumClass(); | ||
|
||
var json = SimpleJson.SerializeObject(obj); | ||
|
||
Assert.AreEqual("{\"Value\":null}", json); | ||
} | ||
|
||
[TestMethod] | ||
public void SerializeNullableEnumWithValue() | ||
{ | ||
var obj = new SerializeNullableEnumClass | ||
{ | ||
Value = Values.Two | ||
}; | ||
|
||
var json = SimpleJson.SerializeObject(obj); | ||
|
||
Assert.AreEqual("{\"Value\":\"Two\"}", json); | ||
} | ||
|
||
public class SerializeNullableEnumClass | ||
{ | ||
public Values? Value { get; set; } | ||
} | ||
} | ||
|
||
public enum Values | ||
{ | ||
One, | ||
Two, | ||
Three | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters