Hi,
 I am getting the following error when I try to deserialize an Object with 
a Builder and @JsonAnySetter on a field.

Error:
*com.fasterxml.jackson.databind.JsonMappingException: Problem deserializing* 
"any" property 'double_score' of class 
com.fasterxml.jackson.databind.deser.AdjectiveScore$Builder (expected type: 
[simple type, class java.lang.Object]; actual type: java.lang.Double), 
problem: object is not an instance of declaring class (through reference 
chain: 
com.fasterxml.jackson.databind.deser.AdjectiveScore$Builder["double_score"])
at 
com.fasterxml.jackson.databind.deser.SettableAnyProperty._throwAsIOE(SettableAnyProperty.java:204)
    at 
com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:175)
    at 
com.fasterxml.jackson.databind.deser.SettableAnyProperty.deserializeAndSet(SettableAnyProperty.java:130)
    at 
com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1449)
    at 
com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer._deserialize(BuilderBasedDeserializer.java:245)
    at 
com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer._deserializeUsingPropertyBased(BuilderBasedDeserializer.java:367)
    at 
com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1198)
    at 
com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserializeFromObject(BuilderBasedDeserializer.java:292)
    at 
com.fasterxml.jackson.databind.deser.BuilderBasedDeserializer.deserialize(BuilderBasedDeserializer.java:156)
    at 
com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
    at 
com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2842)
    at 
com.fasterxml.jackson.databind.deser.AnySetter349Test.testBuilder(AnySetter349Test.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at 
org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
*Caused by: java.lang.IllegalArgumentException: object is not an instance 
of declaring class*
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at 
com.fasterxml.jackson.databind.introspect.AnnotatedMethod.callOnWith(AnnotatedMethod.java:130)
    at 
com.fasterxml.jackson.databind.deser.SettableAnyProperty.set(SettableAnyProperty.java:172)
    ... 32 more


The Following is the POJO:

@JsonInclude(Include.NON_NULL)
@JsonDeserialize(builder = AdjectiveScore.Builder.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class AdjectiveScore {
    public static final String ADJECTIVE_ID_STRING = "adjective_id",
            ADJECTIVE_TEXT_STRING = "adjective_text";

    private final UUID adjectiveId;
    private final String adjectiveText;
    private final Map<String, String> scores;

    private AdjectiveScore(Builder builder) {
        this.adjectiveId = builder.adjectiveId;
        this.adjectiveText = builder.adjectiveText;

        this.scores = builder.scores;
    }

    @JsonProperty(ADJECTIVE_ID_STRING)
    public UUID getAdjectiveId() {
        return adjectiveId;
    }

    @JsonProperty(ADJECTIVE_TEXT_STRING)
    public String getAdjectiveText() {
        return adjectiveText;
    }


    public String getScore(String name) {
        return scores.get(name);
    }

    @JsonAnyGetter
    public Map<String, String> getScores() {
        return scores;
    }


    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Builder {
        private final UUID adjectiveId;
        private String adjectiveText;
        private final Map<String, String> scores = new HashMap<>();

        public Builder(@JsonProperty(ADJECTIVE_ID_STRING) UUID adjectiveId,
                       @JsonProperty(ADJECTIVE_TEXT_STRING) String 
adjectiveText) {
            this.adjectiveId = adjectiveId;
            this.adjectiveText = adjectiveText;
        }

        @JsonAnySetter
        public void parseUnknownProperties(String name, Object value) {
            scores.put(name, (String) value);
        }

        public Builder score(String scoreName, String value) {

            scores.put(scoreName, value);
            return this;
        }

        public AdjectiveScore build() {
            return new AdjectiveScore(this);
        }
    }
}



and the following is the Test to deserialize the JSON to Object:

@Test
public void testBuilder() throws Exception {
     String adjectiveText = "great";
     Integer intScore = 3;
     Double doubleScore = .5;
    UUID adjectiveId = UUID.randomUUID();
    ObjectMapper JSON_MAPPER = new ObjectMapper();
    AdjectiveScore opinion = new AdjectiveScore.Builder(adjectiveId, 
adjectiveText)
            .score("double_score", doubleScore)
            .score("int_score", intScore)
            .build();
    JSON_MAPPER.readValue(JSON_MAPPER.writeValueAsString(opinion), 
AdjectiveScore.class);
}



This test works fine up to the version 2.2.4, AND its starts filing 
versions beyond 2.3.0 until recent version 2.8.6.

Please let me know if I am missing something here.

Thanks,
Charan


-- 
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 post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to