-
-
Notifications
You must be signed in to change notification settings - Fork 20
Serializer will now ignore: indexer properties, and properties with their name listed in a JsonIgnore above the declaring class #299
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
9c5c5c6
51f3c9b
7bc39ab
049612c
453f180
e3f541c
37207a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Text; | ||
|
|
||
| namespace nanoFramework.Json | ||
| { | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /// <summary> | ||
| /// Hides properties from the json serializer | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| [System.AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] | ||
| public sealed class JsonIgnoreAttribute : Attribute | ||
| { | ||
| // See the attribute guidelines at | ||
| // http://go.microsoft.com/fwlink/?LinkId=85236 | ||
|
|
||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// <summary> | ||
| /// Hides properties from the json serializer | ||
| /// </summary> | ||
| public JsonIgnoreAttribute() { | ||
|
|
||
| PropertyNames = new string[0]; | ||
| } | ||
| /// <summary> | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// array of property names for json serializer to ignore | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public string[] PropertyNames { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Hides properties from the json serializer | ||
| /// </summary> | ||
| /// <param name="getterNamesToIgnore">a comma separated list of property names to ignore in json</param> | ||
| public JsonIgnoreAttribute(string getterNamesToIgnore) | ||
| { | ||
| //split by commas, then load array | ||
| PropertyNames = getterNamesToIgnore.Split(','); | ||
| for(int i = 0; i < PropertyNames.Length; i++) | ||
| { | ||
| PropertyNames[i] = PropertyNames[i].Trim(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //implementation to place above individual properties | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ///// <summary> | ||
| ///// Hides a property from the json serializer | ||
| ///// </summary> | ||
| //[System.AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, | ||
| // Inherited = false, AllowMultiple = true)] | ||
| //public sealed class JsonIgnoreAttribute : Attribute | ||
| //{ | ||
| // // See the attribute guidelines at | ||
| // // http://go.microsoft.com/fwlink/?LinkId=85236 | ||
|
|
||
| // /// <summary> | ||
| // /// Hides a property from the json serializer | ||
| // /// </summary> | ||
| // public JsonIgnoreAttribute() { } | ||
| //} | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,9 +125,56 @@ private static bool ShouldSerializeMethod(MethodInfo method) | |
| return false; | ||
| } | ||
|
|
||
| //Ignore property getters with at least one parameter (this[index] operator overloads) | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ParameterInfo[] parameters = method.GetParameters(); | ||
| if (parameters.Length > 0) | ||
| { | ||
| return false; | ||
| } | ||
| parameters = null; | ||
|
||
|
|
||
| // Ignore properties listed in [JsonIgnore()] attribute | ||
| if (ShouldIgnorePropertyFromClassAttribute(method)) | ||
|
||
| return false; | ||
icy3141 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // per property [JsonIgnore()] attribute - not working due to method.GetCustomAttributes returning empty | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| //var attributes = method.GetCustomAttributes(false); | ||
| //foreach (var attributeInfo in attributes) | ||
| //{ | ||
| // if(typeof(JsonIgnoreAttribute).IsInstanceOfType(attributeInfo)) | ||
| // { | ||
| // return false; | ||
| // } | ||
| //} | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// split out method to check for ignore attribute | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| private static bool ShouldIgnorePropertyFromClassAttribute(MethodInfo method) | ||
| { | ||
| string[] gettersToIgnore = null; | ||
| object[] classAttributes = method.DeclaringType.GetCustomAttributes(true); | ||
|
||
| foreach (object attribute in classAttributes) | ||
| { | ||
| if (attribute is JsonIgnoreAttribute ignoreAttribute) | ||
| { | ||
| gettersToIgnore = ignoreAttribute.PropertyNames; | ||
| break; | ||
| } | ||
| } | ||
| classAttributes = null; | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (gettersToIgnore == null) return false; | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| foreach (string propertyName in gettersToIgnore) | ||
| { | ||
| if (propertyName.Equals(method.Name.Substring(4))) | ||
icy3141 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return true; | ||
icy3141 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return false; | ||
icy3141 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Convert an IEnumerable to a JSON string. | ||
| /// </summary> | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.