Skip to content

Using JsonValue and JsonFormat on one field does not work as expectedΒ #2822

@nils-christian

Description

@nils-christian

Hi,

We tried to use the both annotations JsonValue and JsonFormat on one field, but the serialization does not work as expected. Assume following code:

public static void main( final String[] args ) throws JsonProcessingException {
	final ObjectMapper objectMapper = new ObjectMapper( );
	objectMapper.enable( SerializationFeature.INDENT_OUTPUT );
	objectMapper.setVisibility( PropertyAccessor.ALL, Visibility.NONE );
	objectMapper.setVisibility( PropertyAccessor.FIELD, Visibility.ANY );

	System.out.println( objectMapper.writeValueAsString( new A( "Some Value", new B( BigDecimal.ONE ) ) ) );
}

private static class A {

	private final String description;
	private final B b;

	public A( final String description, final B b ) {
		this.description = description;
		this.b = b;
	}

}

private static class B {

	@JsonValue
	@JsonFormat( shape = Shape.STRING )
	private final BigDecimal value;

	public B( final BigDecimal value ) {
		this.value = value;
	}

}

Note that the field value of B has two annotations. The resulting string is

{
  "description" : "Some Value",
  "b" : 1
}

We would expect the value of B beeing written as a string, so we would expect

{
  "description" : "Some Value",
  "b" : "1"
}

If we remove the JsonValue annotation, we get the correct (but nested) value

{
  "description" : "Some Value",
  "b" : {
    "value" : "1"
  }
}

Is this a bug or the correct behaviour?

Thank you and best regards

Nils

Edit: We are using Jackson 2.11.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions