From cce900ebf045cd74b234fe80d7c603e838e8f7f2 Mon Sep 17 00:00:00 2001 From: Faredoon Date: Fri, 25 May 2018 21:58:26 +1000 Subject: [PATCH] Added support for ignoring/skipping properties or fields based on a new JsonSkipAttribute. --- .gitignore | 7 +- LitJSON.sln | 46 ++++++------ src/LitJson/JsonMapper.cs | 9 +++ src/LitJson/JsonSkipAttribute.cs | 11 +++ test/JsonMapperTest.cs | 124 +++++++++++++++++++++++++++++++ 5 files changed, 174 insertions(+), 23 deletions(-) create mode 100644 src/LitJson/JsonSkipAttribute.cs diff --git a/.gitignore b/.gitignore index 76f3876..831b403 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,9 @@ [Tt]ools/* ![Tt]ools/packages.config [Aa]rtifacts/ -.[Dd]otnet/ \ No newline at end of file +.[Dd]otnet/ +/.vs/LitJSON/v15/Server/sqlite3/storage.ide-wal +/.vs/LitJSON/v15/Server/sqlite3/storage.ide-shm +/.vs/LitJSON/v15/Server/sqlite3/storage.ide +/.vs/LitJSON/v15/Server/sqlite3/db.lock +/.vs/LitJSON/v15/.suo diff --git a/LitJSON.sln b/LitJSON.sln index 807fa55..046307c 100644 --- a/LitJSON.sln +++ b/LitJSON.sln @@ -1,11 +1,10 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.26124.0 MinimumVisualStudioVersion = 15.0.26124.0 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LitJSON", "src\LitJson\LitJSON.csproj", "{CFBC33D5-BB2D-4F22-B157-0862C4A77039}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LitJSON", "src\LitJson\LitJSON.csproj", "{CFBC33D5-BB2D-4F22-B157-0862C4A77039}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LitJSON.Tests", "test\LitJSON.Tests.csproj", "{E031C1B4-CD87-475E-9283-4571435C65DB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LitJSON.Tests", "test\LitJSON.Tests.csproj", "{E031C1B4-CD87-475E-9283-4571435C65DB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -16,33 +15,36 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x64.ActiveCfg = Debug|x64 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x64.Build.0 = Debug|x64 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x86.ActiveCfg = Debug|x86 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x86.Build.0 = Debug|x86 + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x64.ActiveCfg = Debug|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x64.Build.0 = Debug|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x86.ActiveCfg = Debug|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Debug|x86.Build.0 = Debug|Any CPU {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|Any CPU.ActiveCfg = Release|Any CPU {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|Any CPU.Build.0 = Release|Any CPU - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x64.ActiveCfg = Release|x64 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x64.Build.0 = Release|x64 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x86.ActiveCfg = Release|x86 - {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x86.Build.0 = Release|x86 + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x64.ActiveCfg = Release|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x64.Build.0 = Release|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x86.ActiveCfg = Release|Any CPU + {CFBC33D5-BB2D-4F22-B157-0862C4A77039}.Release|x86.Build.0 = Release|Any CPU {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x64.ActiveCfg = Debug|x64 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x64.Build.0 = Debug|x64 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x86.ActiveCfg = Debug|x86 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x86.Build.0 = Debug|x86 + {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x64.ActiveCfg = Debug|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x64.Build.0 = Debug|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x86.ActiveCfg = Debug|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Debug|x86.Build.0 = Debug|Any CPU {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|Any CPU.ActiveCfg = Release|Any CPU {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|Any CPU.Build.0 = Release|Any CPU - {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x64.ActiveCfg = Release|x64 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x64.Build.0 = Release|x64 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x86.ActiveCfg = Release|x86 - {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x86.Build.0 = Release|x86 + {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x64.ActiveCfg = Release|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x64.Build.0 = Release|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x86.ActiveCfg = Release|Any CPU + {E031C1B4-CD87-475E-9283-4571435C65DB}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9A5469A6-8024-4079-916D-98C17F3C01BB} EndGlobalSection EndGlobal diff --git a/src/LitJson/JsonMapper.cs b/src/LitJson/JsonMapper.cs index 0fc106a..f915a29 100644 --- a/src/LitJson/JsonMapper.cs +++ b/src/LitJson/JsonMapper.cs @@ -9,6 +9,7 @@ #endregion +using LitJSON; using System; using System.Collections; using System.Collections.Generic; @@ -825,6 +826,14 @@ private static void WriteValue (object obj, JsonWriter writer, writer.WriteObjectStart (); foreach (PropertyMetadata p_data in props) { + + var skipAttributesList = p_data.Info.GetCustomAttributes(typeof(JsonSkipAttribute), true); + var skipAttributes = skipAttributesList as ICollection; + if ( skipAttributes.Count > 0 ) + { + continue; + } + if (p_data.IsField) { writer.WritePropertyName (p_data.Info.Name); WriteValue (((FieldInfo) p_data.Info).GetValue (obj), diff --git a/src/LitJson/JsonSkipAttribute.cs b/src/LitJson/JsonSkipAttribute.cs new file mode 100644 index 0000000..e7a010e --- /dev/null +++ b/src/LitJson/JsonSkipAttribute.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace LitJSON +{ + [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)] + public sealed class JsonSkipAttribute : Attribute + { + } +} diff --git a/test/JsonMapperTest.cs b/test/JsonMapperTest.cs index f4426c5..2034e6b 100644 --- a/test/JsonMapperTest.cs +++ b/test/JsonMapperTest.cs @@ -10,6 +10,7 @@ using LitJson; +using LitJSON; using NUnit.Framework; using System; using System.Collections; @@ -201,6 +202,93 @@ public class NullableEnumTest public NullableEnum? TestEnum; } + public class ClassWithSkippedProperty + { + private int _x; + private int _y; + public ClassWithSkippedProperty(int x, int y) + { + this._x = x; + this._y = y; + } + + [JsonSkip] + public static ClassWithSkippedProperty Zero + { + get + { + return new ClassWithSkippedProperty(0, 0); + } + } + + public int X + { + get + { + return _x; + } + set + { + _x = value; + } + } + public int Y + { + get + { + return _y; + } + set + { + _y = value; + } + } + + } + + public class ClassWithSkippedField + { + private int _x; + private int _y; + private ClassWithSkippedField(int x, int y) + { + this._x = x; + this._y = y; + } + + [JsonSkip] + public ClassWithSkippedField Instance; + + public static ClassWithSkippedField GetNewInstance(int x, int y) + { + return new ClassWithSkippedField(x, y); + } + + public int X + { + get + { + return _x; + } + set + { + _x = value; + } + } + public int Y + { + get + { + return _y; + } + set + { + _y = value; + } + } + + } + [TestFixture] public class JsonMapperTest { @@ -410,6 +498,24 @@ public void ExportValueTypesTest () Assert.AreEqual (expected, json); } + [Test] + public void ExportSkippedPropertyTest() + { + ClassWithSkippedProperty objWithSkippedProperty = new ClassWithSkippedProperty(2, 4); + var json = JsonMapper.ToJson(objWithSkippedProperty); + var expectedJSON = "{\"X\":2,\"Y\":4}"; + Assert.AreEqual(json, expectedJSON); + } + + [Test] + public void ExportSkippedFieldTest() + { + ClassWithSkippedField objWithSkippedField = ClassWithSkippedField.GetNewInstance(2, 4); + var json = JsonMapper.ToJson(objWithSkippedField); + var expectedJSON = "{\"X\":2,\"Y\":4}"; + Assert.AreEqual(json, expectedJSON); + } + [Test] public void ImportArrayOfStringsTest () { @@ -1033,5 +1139,23 @@ public void NullableEnumExportTest() expectedJson = "{\"TestEnum\":null}"; Assert.AreEqual(expectedJson, JsonMapper.ToJson(value)); } + + [Test] + public void SKipAttribute_PropertyTest() + { + var result = JsonMapper.ToJson(new ClassWithSkippedProperty(8, 2)); + string expectedJson = "{\"X\":8,\"Y\":2}"; + Assert.AreEqual(expectedJson, result); + + } + + [Test] + public void SKipAttribute_FieldTest() + { + var result = JsonMapper.ToJson(ClassWithSkippedField.GetNewInstance(8, 2)); + string expectedJson = "{\"X\":8,\"Y\":2}"; + Assert.AreEqual(expectedJson, result); + + } } }