Jack9
JCHJava Community | Help. Code. Learn.
•Created by Jack9 on 11/14/2024 in #java-help
Jackson Mapper - String[] conversion possible?
This should be trivial, but I cannot figure out how to do it. In looking at custom annotations and additional registered serializers, I must be missing something.
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.impl.StringArraySerializer;
public class ExampleDto {
@JsonSerialize(using = StringArraySerializer.class)
public String[] field;
}
public class ExampleDtoTest {
@Test
public void testConvertToMap() {
HashMap<String, Object> expectedMap = new HashMap<>() {{
put("field", (new String[]{"1","2","3"}));
}};
ExampleDto exampleDto = new ExampleDto();
exampleDto.field = new String[]{"1","2","3"};
ObjectMapper mapper = new ObjectMapper();
HashMap result = mapper.convertValue(exampleDto, HashMap.class);
assertEquals(String[].class, result.get("field").getClass()); // fails
}
}
29 replies