Skip to content

@JsonIgnore annotation not working when @JsonCreator present in reflection decoding mode #130

@cedrilino

Description

@cedrilino

As it seems, the @JsonIgnore annotation is ignored if an @JsonCreator annotated constructor is present.
The following test

public class JsonIterTest {

	@BeforeClass
	public static void setUp() {
		JsonIterator.setMode(DecodingMode.REFLECTION_MODE);
	}

	public static class TestObject {
		String field1;
		@JsonIgnore
		ActionListener fieldXXX;

		@JsonCreator
		public TestObject(@JsonProperty("field2") String field) {
			field1 = null;
			fieldXXX = e -> System.out.println("field2 is " + field);
		}

		@Override
		public String toString() {
			return "field1=" + field1 + ", field2=" + fieldXXX;
		}
	}

	@Test
	public void simpleParsingTest() throws IOException {
		JsonIterator iter = JsonIterator.parse("{\"field2\": \"test\"}");
		TestObject t = iter.read(TestObject.class);
		System.out.println(t);
	}
}

fails with:

java.lang.IllegalArgumentException: Can not set java.awt.event.ActionListener field de.q2web.db.io.json.RuleConfigTest$TestObject.field2 to java.lang.String

	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
	at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
	at java.lang.reflect.Field.set(Field.java:764)
	...

In addition i observed that:

  • If we remove the @JsonIgnore annotation, the following exception is thrown:
com.jsoniter.spi.JsonException: no constructor for: interface java.awt.event.ActionListener
	at com.jsoniter.ReflectionObjectDecoder.init(ReflectionObjectDecoder.java:45)
	...
  • If the line ActionListener fieldXXX; is transformed to String fieldXXX; and the line fieldXXX = e -> System.out.println(field); is removed, no exception is thrown, however the console output yields field1=null, field2=test instead of field1=null, field2=null.

  • In dynamic decoding mode (DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH) everything works fine.

Edit: forgot to mention that I use jsoniter:0.9.18 with javassist:3.20.0-GA ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions