-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Serializer will now ignore: indexer properties, and properties with their name listed in a JsonIgnore above the declaring class #299
base: main
Are you sure you want to change the base?
Conversation
Serializer will now ignore: indexer properties and properties with their name listed in a JsonIgnore above the declaring class.
@icy3141 can you please replace the ZIP with the actual images (in the PR initial comment)? ZIP files are a security risk and they are blocked on most corporate networks. And they require several other steps to actually get to see the images. 😉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting approach. Thanks for proposing this.
This is a high impact library so the quality bar it's quite high here! 😉
- Can you please add Unit Tests for this?
- We need to access the performance impact on this. Can you please add the test to the benchmark project and have it run with and without the attribute to gauge the impact?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick review and it's mainly about the form to fit with the rest of the style and specific point about performances.
Please add an option in the json where JsonIgnore can be configurable because checking every time the class will have a bad performance impact.
Also please cache the class ignore methods in the case the option is activated. So don't call the reflection all the time but only once.
And then just check if the function is part of the list or not.
And please add UnitsTest and also please run the performance tool before and after your changes so we can compare.
nanoFramework.Json/JsonSerializer.cs
Outdated
parameters = null; | ||
|
||
// Ignore properties listed in [JsonIgnore()] attribute | ||
if (ShouldIgnorePropertyFromClassAttribute(method)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For performance reasons, please add a new option in the sterilizer so you call this only if the ignore option is on. The cost is VERY high in terms of performances when this is calls every time.
Also, please cache this because as you place it on the class level, you should call it only once, not for every property.
By having those 2 things implemented, you can have a decent performance.
And please run the performance tool without your code and with your code, so we can compare.
nanoFramework.Json/JsonSerializer.cs
Outdated
private static bool ShouldIgnorePropertyFromClassAttribute(MethodInfo method) | ||
{ | ||
string[] gettersToIgnore = null; | ||
object[] classAttributes = method.DeclaringType.GetCustomAttributes(true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that should be cashed for a specific class
…-level attributes when setting is enabled, unit tests, and fixed coding style problems.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me now. So all up, as a summary, it seems we are good on performance side and you even managed to optimize a bit the existing code. Correct?
nanoFramework.Json.Benchmark/SerializationBenchmarks/ReferenceTypesSerializationBenchmark.cs
Outdated
Show resolved
Hide resolved
Configuration.Settings.UseIgnoreAttribute = false; | ||
}); | ||
} | ||
[Benchmark] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add an extra line between the functions, so one is missing before the attribute. Same in the next few functions. It helps for readibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for more style errors. Even though I like organized code, I'm not used to checking that aspect quite so much. Plus it was a little messy from moving stuff around to get tests working. Will be better next time, don't want to waste your valuable time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing to apologize about! You're doing great! 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on José remark, you're doing great! No worry, you should have seen my very first PR on a .NET repository :-D Now, with habit, it's easy to spot them. We've been added StyleCop linter on the IoT repository as it's where we do have the most contributions. Not yet into those classes one. That will slowly come but it's not urgent. And anyway, we won't put it in place in the tests as it doesn't make too much sense.
So all good! You'll be indeed much better next time and at some point like us being able to spot those :-D
It's really to make it easier to read the code and navigate.
nanoFramework.Json.Benchmark/SerializationBenchmarks/ReferenceTypesSerializationBenchmark.cs
Outdated
Show resolved
Hide resolved
//test serialize and deserialize | ||
bool jsonSuccess = testObject.IsEqual(dserResult); | ||
Assert.IsTrue(jsonSuccess); | ||
OutputHelper.WriteLine("Serialization/Deserialization was " + (jsonSuccess ? "" : "NOT ") + "successful."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you arrive here, then it's always successful. So no need to test the value of jsonSuccess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me now. So all up, as a summary, it seems we are good on performance side and you even managed to optimize a bit the existing code. Correct?
I think so, as long as my caching solution is acceptable. I will get to those last few fixes you suggested as soon as I can. The one thing I didn't do is run the performance tests with the code before I touched it. I can do that as well and provide my findings when I do my next push.
|
||
//test serialize and deserialize | ||
bool jsonSuccess = testObject.IsEqual(dserResult); | ||
Assert.IsTrue(jsonSuccess); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note that you can as well add an error message in case it's not successful. So you know where it broke and maybe other values you want to track in the test result.
bool areIgnoredPropsPresent = jsonString.Contains("MyIgnoredProperty") | ||
|| jsonString.Contains("AnotherIgnoredProperty"); | ||
Assert.IsFalse(areIgnoredPropsPresent); | ||
OutputHelper.WriteLine("Ignore was " + (areIgnoredPropsPresent ? "NOT " : "") + "successful."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, the value will always be false
style changes from Ellerbach Co-authored-by: Laurent Ellerbach <[email protected]>
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
{ | ||
classAttributes = type.GetCustomAttributes(false); | ||
} | ||
|
||
Hashtable hashtable = new(); | ||
|
||
// Iterate through all of the methods, looking for internal GET properties | ||
MethodInfo[] methods = type.GetMethods(); | ||
|
||
foreach (MethodInfo method in methods) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing here:
For each property you are creating gettersToIgnore based on classAttributes. Which means doing the same work for each property (foreach loop -> ShouldSerializeMethod -> ShouldIgnorePropertyFromClassAttribute -> creating array). Faster approach should be creating ignored property array before foreach (MethodInfo method in methods)
.
BTW @josesimoes does nanoFramework support attributes on properties?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@torbacz there is nothing preventing you from decorating properties with attributes.
I guess that you want to know if you can reach custom attributes for properties. That's a different story: that is not supported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was my question 😅 Because then we could just decorate each property. Also I'm not quite sure, if we should implement such solution. It's not resolving any problems. If you have class with properties which you don't want to send, just create derived class and pass it to JSON lib.
Don't get me wrong, I like new features but I'm concern about performance, adding new feature where there is a possibility for workaround is making lib much complicated over time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's wait for the benchmark results and then decide.
I do share your concern. Impacting performance is something to avoid at all costs. Unless the trade-off it's relevant.
As a side comment: I keep being surprised on how much interest this library has gathered from the community! And the investments on improving it are also surprising to me. 😄
I think we should compare benchmarks before and after adding new functionality. It's simple "if" condition, yet it may impact performance. |
Absolutely! I've asked for the benchmarks from the begging of this. |
@icy3141 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement ( “Agreement” ) is agreed to by the party signing below ( “You” ), 1. Definitions. “Code” means the computer software code, whether in human-readable or machine-executable form, “Project” means any of the projects owned or managed by .NET Foundation and offered under a license “Submit” is the act of uploading, submitting, transmitting, or distributing code or other content to any “Submission” means the Code and any other copyrightable material Submitted by You, including any 2. Your Submission. You must agree to the terms of this Agreement before making a Submission to any 3. Originality of Work. You represent that each of Your Submissions is entirely Your original work. Should You wish to Submit materials that are not Your original work, You may Submit them separately 4. Your Employer. References to “employer” in this Agreement include Your employer or anyone else 5. Licenses. a. Copyright License. You grant .NET Foundation, and those who receive the Submission directly license in the Submission to reproduce, prepare derivative works of, publicly display, publicly perform, b. Patent License. You grant .NET Foundation, and those who receive the Submission directly or c. Other Rights Reserved. Each party reserves all rights not expressly granted in this Agreement. 6. Representations and Warranties. You represent that You are legally entitled to grant the above 7. Notice to .NET Foundation. You agree to notify .NET Foundation in writing of any facts or 8. Information about Submissions. You agree that contributions to Projects and information about 9. Governing Law/Jurisdiction. This Agreement is governed by the laws of the State of Washington, and 10. Entire Agreement/Assignment. This Agreement is the entire agreement between the parties, and .NET Foundation dedicates this Contribution License Agreement to the public domain according to the Creative Commons CC0 1. |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
@icy3141 any chance you can get this moving forward? If not, please let us know so one of the team members can pick it up. |
Description
Motivation and Context
Increases flexibility for JSON-serializable objects. Before this change, it would have caused an error to serialize an object that defined an indexer property (this[index] operator overload). Also it doesn't appear there was any way to mark a class member NOT to be serialized.
How Has This Been Tested?
I am very new to github, so I am attaching my test project as a zip. I hope to learn the proper procedures for the future. I apologize for any inconvenience.
EDIT: zip file replaced with screenshots of test code and link to project repo
https://github.com/icy3141/JsonIgnoreTestProject/tree/main/JsonIgnoreTestProject
Screenshots
Types of changes
Checklist: