Skip to content

Increased need for @JsonCreator with Jackson 3 #5246

@wilkinsona

Description

@wilkinsona

Search before asking

  • I searched in the issues and found nothing similar.

Describe the bug

As discussed with @cowtowncoder in spring-projects/spring-boot#46659, there appears to be an increased need for @JsonCreator with Jackson 3.0.0-rc6 compared to 2.19.2.

Version Information

  • 3.0.0-rc6
  • 2.19.2

Reproduction

package com.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import tools.jackson.databind.json.JsonMapper;

public class Example {

	public static void main(String[] args) throws Exception {
		String json = "{\"productId\":5, \"name\":\"test\", \"weight\":42}";
		JsonMapper jackson3Mapper = tools.jackson.databind.json.JsonMapper.builder().build();
		System.out.println(jackson3Mapper.readValue(json, Pojo.class).getProductId());
		ObjectMapper jackson2Mapper = com.fasterxml.jackson.databind.json.JsonMapper.builder().build();
		jackson2Mapper.registerModule(new ParameterNamesModule());
		System.out.println(jackson2Mapper.readValue(json, Pojo.class).getProductId());
	}

	public static class Pojo {

		private final int productId;

		private final String name;

		private final int weight;

		public Pojo() {
			this.productId = 0;
			this.name = null;
			this.weight = 0;
		}

		public Pojo(int productId, String name, int weight) {
			this.productId = productId;
			this.name = name;
			this.weight = weight;
		}

		public int getProductId() {
			return this.productId;
		}

		public String getName() {
			return this.name;
		}

		public int getWeight() {
			return this.weight;
		}

	}

}

The above should be compiled with -parameters. When run, it should produce the following output:

0
5

Jackson 3 uses the default constructor to create Pojo, Jackson 2 uses the three-argument constructor.

Expected behavior

I expected the non-default Pojo(int productId, String name, int weight) constructor to be used in both cases, and not just with Jackson 2. It is used with Jackson 3 when annotated with @JsonCreator or when the default constructor is removed.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 releaseto-evaluateIssue that has been received but not yet evaluated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions