jansupol / jsonbapi

0 stars 0 forks source link

How to parse (nested) interfaces? How to map interfaces to concrete implementation? #65

Open jansupol opened 6 years ago

jansupol commented 6 years ago

I have a interface model like:

public interface Customer {

    String getName();
    Address getAddress();
}

public interface Address {

    String getStreet();
    String getCity();
}

For each interface there is a single implementation. Important to note that the model contains nested properties/types using (again) interfaces.

How can this be parsed?

JsonbConfig config = new JsonbConfig()
// TODO: what configuration to add to map interfaces to concrete implementations?
Jsonb jsonb = JsonbBuilder.create(config)
jsonb.fromJson(json, Customer.class);

Above gives exception: javax.json.bind.JsonbException: interface com.acme.Customer not instantiable

Note: I cannot change the interfaces nor the concrete implementations as these are provided.

jansupol commented 6 years ago
jansupol commented 6 years ago

@bravehorsie Commented There is already a pull request for this in the Yasson RI. https://github.com/eclipse/yasson/pull/64 @m0mus Can we move annotation and JsonbConfig in the above pull request to the spec for next release? Looks like a handy feauture.

jansupol commented 6 years ago

@marceloverdijk Commented Can that solution be used without an @Annotation? In my case I cannot change the classes itself.

jansupol commented 6 years ago

@bravehorsie Commented You can also add mapping with JsonbConfig see https://github.com/eclipse/yasson/pull/64/files#diff-42990ea8091951c02b2166a51a23f6a6 second test method. In your case it is also enough to call: Customer customer = jsonb.fromJson(json, CustomerImpl.class);

jansupol commented 6 years ago

@marceloverdijk Commented Thanks! Looking forward to see this incorporated in the jsonb spec.