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

Conventional configuration with private field visibility strategy #57

Closed
jsonbrobot opened this issue Sep 28, 2017 · 5 comments
Closed

Comments

@jsonbrobot
Copy link

Currently only public class fields are serialized. Use "PrivateFieldVisibilityStrategy" per convention:

public class PrivateVisibilityStrategy implements PropertyVisibilityStrategy {

    @Override
    public boolean isVisible(Field field) {
        return true;
    }

    @Override
    public boolean isVisible(Method method) {
        return false;
    }
}

The strategy above should work out-of-the-box without any additional steps (JsonbConfig) required.

@jsonbrobot
Copy link
Author

@bravehorsie Commented
This strategy will eliminate usage of getters,setters by default. In case private field has public setter / getter it won't be used. What is the motivation for doing so?

@jsonbrobot
Copy link
Author

@m0mus Commented
I don't think it should be a part of the standard. We give users an ability to change visibility strategy and it's up to implementation which strategies will be provided out of the box.
I think it's a good idea if we add it to Yasson for convenience.

@jsonbrobot
Copy link
Author

@steappe Commented
I'm also using this private field strategy. The motivation is as follows:

  1. I'm starting with a domain model that's been agreed by the team. In this domain model, there are some value objects that I like to use as DTOs. These value objects have private fields. These fields are read-only, and most of (in not all) these fields have read-only access methods that don't follow the getXxx convention, as I prefer fluid method names for my domain model.
  2. I annotate the fields of these value objects with JSON-B annotations, and add a JsonbCreator annotation to their constructor.

In short, I generally prefer a field approach over a (get/set) property approach because then I don't have to change the methods of my domain model to suit the constraints of a serialization / deserialization framework. The same applies when I use JPA for instance.

Adding a private field strategy to Yasson for convenience would be great.

@jsonbrobot
Copy link
Author

@m0mus Commented
This feature was added to Yasson:
eclipse-ee4j/yasson#65

Closing this issue.

@jsonbrobot
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant