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

readTree does not work with defaultTyping enabled but no type info provided #793

Closed
gracefulgopher opened this issue May 14, 2015 · 3 comments
Milestone

Comments

@gracefulgopher
Copy link

I have enabled defaultTyping, and serialized Foo entity with no type info. I'm trying to read json as a tree with mapper.readTree(json), and it throws an exception

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: 
Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY 
type information for class com.fasterxml.jackson.databind.JsonNode
 at [Source: {
  "bar" : "bar"
}; line: 1, column: 1]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:927)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._locateTypeId(AsArrayTypeDeserializer.java:127)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:93)
    at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:68)
    at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeWithType(JsonNodeDeserializer.java:144)
    at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserializeWithType(JsonNodeDeserializer.java:14)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
    at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2136)
    at test.App.main(App.java:23)

However, if I disable defaultTyping, the same code works fine. So, readTree(json) does not actually need type info for the root element, because it works when defaultTyping is disabled (i.e. {"bar" : "bar"}), but it throws the exception when defaultTyping is enabled, that's why it looks like a bug. The same thing happens for valueToTree(foo).
Jackson version is 2.5.3
Full code is provided.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;

public class App {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper()
                .enableDefaultTyping() // works fine with disableDefaultTyping()
                .enable(MapperFeature.AUTO_DETECT_GETTERS)
                .enable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS)
                .disable(MapperFeature.USE_GETTERS_AS_SETTERS)
                .disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)
                .enable(SerializationFeature.INDENT_OUTPUT)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        Foo foo = new Foo("bar");
        String serialized = mapper.writeValueAsString(foo); // {"bar" : "bar"}

        JsonNode jsonNode = mapper.readTree(serialized); // exception here
        JsonNode node = mapper.valueToTree(foo); // and here
    }

    public static class Foo {
        private String bar;

        public Foo() {
        }

        public Foo(String bar) {
            this.bar = bar;
        }

        public String getBar() {
            return bar;
        }

        public void setBar(String bar) {
            this.bar = bar;
        }
    }
}
@cowtowncoder
Copy link
Member

Thanks. Tree handling should just ignore default typing, as trees should represent JSON exactly as-is, so this sounds like a bug.

@cowtowncoder cowtowncoder added this to the 2.5.4 milestone May 15, 2015
@cowtowncoder
Copy link
Member

Looks like earlier fix for #88 had a minor flaw, and it was not skipping type id for JsonNode with standard default typing which is the intended behavior (one can still force type id with custom default typing).

@gracefulgopher
Copy link
Author

Thanks a lot for fast reply and fix!
Waiting for jackson 2.5.4.

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

2 participants