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

Use of ToStringSerializer via @JsonSerialize on int/long property does not work #117

Closed
wujimin opened this issue Nov 17, 2020 · 4 comments
Milestone

Comments

@wujimin
Copy link

wujimin commented Nov 17, 2020

Description

  • jdk 11 (not test with jdk 8)
  • jackson 2.11.2 / 2.11.3
  • primitive field with @JsonSerializer
        @JsonSerialize(using = ToStringSerializer.class)
        private int primitiveValue;
  • if primitiveValue is 10
    • if not work with afterburner, will serialize to "10"
    • if work with afterburner, will serialize to 10

seems afterburner not handle primitive field @JsonSerialize annotation

Reproduce case

public class AfterBurnerPrimitiveSerializerBug {
    public static class Model {
        @JsonSerialize(using = ToStringSerializer.class)
        private int primitiveValue;

        @JsonSerialize(using = ToStringSerializer.class)
        private Integer value;

        public int getPrimitiveValue() {
            return primitiveValue;
        }

        public Model setPrimitiveValue(int primitiveValue) {
            this.primitiveValue = primitiveValue;
            return this;
        }

        public Integer getValue() {
            return value;
        }

        public Model setValue(Integer value) {
            this.value = value;
            return this;
        }
    }

    public static class AnotherModel extends Model {

    }

    static Model model = new Model().setPrimitiveValue(10).setValue(10);

    static Model anotherModel = new AnotherModel().setPrimitiveValue(10).setValue(10);

    static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {
        // will print: {"primitiveValue":"10","value":"10"}
        System.out.println(mapper.writeValueAsString(model));

        mapper.registerModule(new AfterburnerModule());
        // will print: {"primitiveValue":10,"value":"10"}
        // primitiveValue lost JsonSerialize feature
        System.out.println(mapper.writeValueAsString(anotherModel));
    }
}

output

{"primitiveValue":"10","value":"10"}
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.fasterxml.jackson.module.afterburner.util.MyClassLoader (file:/C:/Users/myUser/.m2/repository/com/fasterxml/jackson/module/jackson-module-afterburner/2.11.2/jackson-module-afterburner-2.11.2.jar) to method java.lang.ClassLoader.findLoadedClass(java.lang.String)
WARNING: Please consider reporting this to the maintainers of com.fasterxml.jackson.module.afterburner.util.MyClassLoader
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
{"primitiveValue":10,"value":"10"}
@wujimin wujimin changed the title It is ineffective when using afterburner in primitive field with @JsonSerializer It is ineffective when using afterburner in primitive field with @JsonSerialize Nov 17, 2020
@cowtowncoder
Copy link
Member

Thank you for reporting this, I hope to look into this soon.

@cowtowncoder
Copy link
Member

cowtowncoder commented Nov 21, 2020

I can reproduce this as shown.

@cowtowncoder cowtowncoder changed the title It is ineffective when using afterburner in primitive field with @JsonSerialize Use of ToStringSerializer via @JsonSerialize on int/long property does not work Nov 21, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Nov 21, 2020
@cowtowncoder
Copy link
Member

Specific reported problem was fixed; also found another related issue (#118) to try to fix.
Patch will be included in 2.12.0 release which should be out in about a week.

@wujimin
Copy link
Author

wujimin commented Nov 23, 2020

thanks, we will optimize our code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants