0
public class JsonObject {
    private HashMap<String, JsonObject> jsonObjects;
    private List<JsonObject> jsonArray;
    private Integer jsonNumber;
    private String jsonString;
    private Boolean jsonBoolean;
    private Float jsonFloat;
    private String original;

    private VIEW view;

    @SuppressWarnings("unchecked")
    public JsonObject(String json) {
        //... json parser to class field  
    }
    <VIEW> void name(VIEW v) {
        this.view=v;
    }
    // ... getter and setters
}

Use Vaadin FW like UI infrastructure. I wont to declare like this:

    public class Exm extends FormLayout {

        public Exm() {
            String json = "{some json ....}";
            JsonObject<TextField> js = new JsonObject(json);

            js.viewBainder(TextField::getValue,TextField::setValue)
            .viewCaption(TextField::setCaption,TextField::getCaption)
            .viewValueChangeEvents(TextField::addValueChangeListener);

            addComponent(js.getView());
        }
    }

Can I do this without generic class, only generic method?

How i can bind VIEW and abstract methods in class?

Pls. give simple example.

P.S. I am junior in java pls. dont think in hard ob. me! Thanx.

Yerbol
  • 11
  • 3

1 Answers1

1

You can find documentation to data binding with forms in Vaadin here:

https://vaadin.com/docs/v8/framework/datamodel/datamodel-forms.html

The concept is that in Vaadin there is Binder class, that can bind Vaadin field components (thos implementing HasValue) with POJOs (i.e. Java objects having getters and setters for properties) Vaadin 8 data binding is easiest to use with Java8 syntax.

The second part of your question is how to deserialize JSON to POJOs and back to strings, and based on your question I did not get grasp what library are you using, there are many, and details differ based on what you use. What ever you use, you should construct java object with getName() and setName(..) methods (name now being one of the properties in JSON).

How to convert String to JSONObject in Java

Usually we like to call the deserialized version of the JSON DTO object (=data transfer object), since it is used only internally in the application. So if that is called e.g. PersonDTO

TextField nameField = new TextField();

// Shorthand for cases without extra configuration
binder.bind(nameField, PersonDTO::getName, PersonDTO::setName);
Tatu Lund
  • 9,949
  • 1
  • 12
  • 26