On Sat, Aug 20, 2022 at 11:46 PM Mantas Gridinas <[email protected]> wrote:
>
> Whats the issue with generating a concrete type that would have fields for 
> your interface, and then you would have that model class implement your 
> interface? You would still need to provide a concrete type to deserialize to, 
> but later down the chain you would reference to your object only by interface.

There are probably other solutions to this, but one of FasterXML
Jackson modules, "Mr Bean" module:

https://github.com/FasterXML/jackson-modules-base/tree/2.14/mrbean

does this as well. It will be invoked when an attempt is made to
deserialize a non-polymorphic (no `@JsonTypeInfo` or default typing)
for which on explicit concrete type mapping exists.
It only handles relatively basic getter/setter cases, plain Beans.
Contributions for extending functionality would be appreciated of
course, as well (there are a few RFE issues for things like adding
`equals()` and `hashCode()`; default values).
A blog article showing usage:

http://www.cowtowncoder.com/blog/archives/2011/08/entry_459.html

may be helpful as well.

-+ Tatu +-

>
> On Fri, Aug 19, 2022 at 6:06 AM Daniel Young <[email protected]> wrote:
>>
>> Here is my code:
>>
>> ```java
>> import com.fasterxml.jackson.core.JsonProcessingException;
>> import com.fasterxml.jackson.core.type.TypeReference;
>> import com.fasterxml.jackson.databind.ObjectMapper;
>>
>> public class TestJackson {
>>
>>     static interface Entity {
>>         String getName();
>>     }
>>
>>     @Test
>>     public void test() throws JsonProcessingException {
>>
>>         String json = "{\"name\": \"good\"}";
>>
>>         ObjectMapper objectMapper = new ObjectMapper();
>>         Entity entity = objectMapper.readValue(json, Entity.class);
>> //        Entity entity = objectMapper.readValue(json, new 
>> TypeReference<Entity>(){});
>>
>>         System.out.println("name: " + entity.getName());
>>     }
>> }
>> ```
>> Get exception:
>> ```java
>> Exception in thread "main" 
>> com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot 
>> construct instance of `io.gitlab.donespeak.turtal.TestJackson$Entity` (no 
>> Creators, like default construct, exist): abstract types either need to be 
>> mapped to concrete types, have custom deserializer, or contain additional 
>> type information
>>  at [Source: (String)"{"name": "good"}"; line: 1, column: 1]
>>     at 
>> com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
>>     at 
>> com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1589)
>>     at 
>> com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1055)
>>     at 
>> com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:265)
>>     at 
>> com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
>>     at 
>> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
>>     at 
>> com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
>>     at io.gitlab.donespeak.turtal.TestJackson.test(TestJackson.java:23)
>>     at io.gitlab.donespeak.turtal.TestJackson.main(TestJackson.java:10)
>>
>> Process finished with exit code 1
>> ```
>>
>> I think dynamic proxy could be a solution for deserializing json to a 
>> interface.
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "jackson-user" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/jackson-user/9776d0ca-bd05-4d6b-98fe-711012e3703fn%40googlegroups.com.
>
>
>
> --
> // Mantas
>
> --
> You received this message because you are subscribed to the Google Groups 
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/jackson-user/CAE9hW8QMX5wviwzmsesUPfW4tDPgD1Piq0XL%3DSrmZea8iVCTkg%40mail.gmail.com.

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/CAL4a10jwNefHYWYtBD66XfO843sDmEsED_zgBgjyeKwCZ8YobQ%40mail.gmail.com.

Reply via email to