Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
[Tt]ools/*
![Tt]ools/packages.config
[Aa]rtifacts/
.[Dd]otnet/
.[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
46 changes: 24 additions & 22 deletions LitJSON.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
9 changes: 9 additions & 0 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#endregion


using LitJSON;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -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<Attribute>;
if ( skipAttributes.Count > 0 )
{
continue;
}

if (p_data.IsField) {
writer.WritePropertyName (p_data.Info.Name);
WriteValue (((FieldInfo) p_data.Info).GetValue (obj),
Expand Down
11 changes: 11 additions & 0 deletions src/LitJson/JsonSkipAttribute.cs
Original file line number Diff line number Diff line change
@@ -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
{
}
}
124 changes: 124 additions & 0 deletions test/JsonMapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


using LitJson;
using LitJSON;
using NUnit.Framework;
using System;
using System.Collections;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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 ()
{
Expand Down Expand Up @@ -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);

}
}
}