Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonIgnore Attribute #7

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/src/LitJson/AssemblyInfo.cs
/website
*.sublime-project
*.sublime-workspace
*.mdb
.DS_Store
Binary file modified bin/LitJson.dll
Binary file not shown.
9 changes: 6 additions & 3 deletions build/make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ SRC_FILES = \
$(srcdir)/JsonReader.cs \
$(srcdir)/JsonWriter.cs \
$(srcdir)/Lexer.cs \
$(srcdir)/ParserToken.cs
$(srcdir)/ParserToken.cs \
$(srcdir)/JsonAttribute.cs

CLEAN_FILES += $(LITJSON_ASSEMBLY) $(GEN_FILES) $(LITJSON_MDB_FILE)

Expand All @@ -43,6 +44,7 @@ CLEAN_FILES += $(LITJSON_ASSEMBLY) $(GEN_FILES) $(LITJSON_MDB_FILE)
## Tests
##
TEST_ASSEMBLY = $(topdir)/bin/LitJson.Tests.dll
TEST_MDB_FILE = $(TEST_ASSEMBLY).mdb

TEST_MONO_NUNIT = $(shell pkg-config --libs mono-nunit)

Expand All @@ -51,9 +53,10 @@ TEST_SRC_FILES = \
$(testdir)/JsonDataTest.cs \
$(testdir)/JsonMapperTest.cs \
$(testdir)/JsonReaderTest.cs \
$(testdir)/JsonWriterTest.cs
$(testdir)/JsonWriterTest.cs \
$(testdir)/JsonIgnoreTest.cs

CLEAN_FILES += $(TEST_ASSEMBLY) $(builddir)/TestResult.xml
CLEAN_FILES += $(TEST_ASSEMBLY) $(TEST_MDB_FILE) $(builddir)/TestResult.xml


##
Expand Down
23 changes: 23 additions & 0 deletions src/LitJson/JsonAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace LitJson
{
[Flags]
public enum JsonIgnoreWhen
{
Never = 0,
Serializing = 1,
Deserializing = 2
}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class JsonIgnore : Attribute
{
public JsonIgnoreWhen Usage { get; private set; }

public JsonIgnore(JsonIgnoreWhen usage = JsonIgnoreWhen.Serializing | JsonIgnoreWhen.Deserializing)
{
Usage = usage;
}
}
}
56 changes: 52 additions & 4 deletions src/LitJson/JsonMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ namespace LitJson
{
internal struct PropertyMetadata
{
public MemberInfo Info;
public bool IsField;
public Type Type;
public MemberInfo Info;
public bool IsField;
public Type Type;
public JsonIgnoreWhen Ignore;
}


Expand Down Expand Up @@ -224,6 +225,8 @@ private static void AddObjectMetadata (Type type)
p_data.Info = p_info;
p_data.Type = p_info.PropertyType;

ProcessAttributes(ref p_data, p_info);

data.Properties.Add (p_info.Name, p_data);
}

Expand All @@ -233,6 +236,8 @@ private static void AddObjectMetadata (Type type)
p_data.IsField = true;
p_data.Type = f_info.FieldType;

ProcessAttributes(ref p_data, f_info);

data.Properties.Add (f_info.Name, p_data);
}

Expand All @@ -259,6 +264,9 @@ private static void AddTypeProperties (Type type)
PropertyMetadata p_data = new PropertyMetadata ();
p_data.Info = p_info;
p_data.IsField = false;

ProcessAttributes(ref p_data, p_info);

props.Add (p_data);
}

Expand All @@ -267,6 +275,8 @@ private static void AddTypeProperties (Type type)
p_data.Info = f_info;
p_data.IsField = true;

ProcessAttributes(ref p_data, f_info);

props.Add (p_data);
}

Expand All @@ -279,6 +289,17 @@ private static void AddTypeProperties (Type type)
}
}

private static void ProcessAttributes (ref PropertyMetadata p_data, MemberInfo m_info)
{
foreach (Attribute attr in m_info.GetCustomAttributes(true)) {
if (attr is JsonIgnore) {
JsonIgnore ignore_attr = (JsonIgnore)attr;
p_data.Ignore = ignore_attr.Usage;

}
}
}

private static MethodInfo GetConvOp (Type t1, Type t2)
{
lock (conv_ops_lock) {
Expand Down Expand Up @@ -429,6 +450,12 @@ private static object ReadValue (Type inst_type, JsonReader reader)
PropertyMetadata prop_data =
t_data.Properties[property];

// Don't deserialize a field or property that has a JsonIgnore attribute with deserialization usage.
if ((prop_data.Ignore & JsonIgnoreWhen.Deserializing) > 0) {
ReadSkip (reader);
continue;
}

if (prop_data.IsField) {
((FieldInfo) prop_data.Info).SetValue (
instance, ReadValue (prop_data.Type, reader));
Expand Down Expand Up @@ -562,6 +589,11 @@ private static void RegisterBaseExporters ()
datetime_format));
};

base_exporters_table[typeof (float)] =
delegate (object obj, JsonWriter writer) {
writer.Write (Convert.ToDouble ((float)obj));
};

base_exporters_table[typeof (decimal)] =
delegate (object obj, JsonWriter writer) {
writer.Write ((decimal) obj);
Expand Down Expand Up @@ -633,12 +665,24 @@ private static void RegisterBaseImporters ()
RegisterImporter (base_importers_table, typeof (int),
typeof (uint), importer);

importer = delegate (object input) {
return Convert.ToInt64 ((int) input);
};
RegisterImporter (base_importers_table, typeof (int),
typeof (long), importer);

importer = delegate (object input) {
return Convert.ToSingle ((int) input);
};
RegisterImporter (base_importers_table, typeof (int),
typeof (float), importer);

importer = delegate (object input) {
return Convert.ToSingle ((double) input);
};
RegisterImporter (base_importers_table, typeof (double),
typeof (float), importer);

importer = delegate (object input) {
return Convert.ToDouble ((int) input);
};
Expand All @@ -651,7 +695,6 @@ private static void RegisterBaseImporters ()
RegisterImporter (base_importers_table, typeof (double),
typeof (decimal), importer);


importer = delegate (object input) {
return Convert.ToUInt32 ((long) input);
};
Expand Down Expand Up @@ -801,6 +844,11 @@ private static void WriteValue (object obj, JsonWriter writer,

writer.WriteObjectStart ();
foreach (PropertyMetadata p_data in props) {

// Don't serialize a field or property with the JsonIgnore attribute with serialization usage
if ((p_data.Ignore & JsonIgnoreWhen.Serializing) > 0)
continue;

if (p_data.IsField) {
writer.WritePropertyName (p_data.Info.Name);
WriteValue (((FieldInfo) p_data.Info).GetValue (obj),
Expand Down
Loading