jansupol / jsonbapi

0 stars 0 forks source link

Conventional configuration with private field visibility strategy #57

Closed jansupol closed 6 years ago

jansupol commented 6 years ago

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.

jansupol commented 6 years ago
jansupol commented 6 years ago

@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?

jansupol commented 6 years ago

@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.

jansupol commented 6 years ago

@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.

jansupol commented 6 years ago

@m0mus Commented This feature was added to Yasson: https://github.com/eclipse/yasson/pull/65

Closing this issue.