I'm trying to find a generic way to serialize value objects that make use of @JsonValue
to a plain string. In particular, I need to suppress the double quotes that Jackson creates. Example:
class Scratch
{
public static class ValueType
{
@JsonValue
private final String value;
public ValueType( String value )
{
this.value = value;
}
}
public static void main( String[] args ) throws IOException
{
ObjectMapper objectMapper = new ObjectMapper();
ValueType valueType = new ValueType( "1234" );
String plainValue = objectMapper.writeValueAsString( valueType );
System.out.println( plainValue ); // prints '"1234"', should print '1234'
}
}
Instead of "1234"
, I want the variable plainValue
to hold the plain value only, i.e. 1234
. Of course I could just strip the double quotes after the string was created, but I was hoping there could be a simple way to directly serialize the object to the required output?
Note that modifying ValueType
is not an option, so for example I cannot add @JsonRawValue
.