Add your own custom serializer and deserializer.
@JsonSerialize(using = MySerializer.class)
@JsonDeserialize(using = MyDeSerializer.class)
public class Foo {
private String name;
private String[] pics;
Serializer:
public class MySerializer extends JsonSerializer<Foo> {
@Override
public void serialize(Foo value, JsonGenerator jgen, SerializerProvider serializers) throws IOException {
int i = 1;
String fieldName = "pics";
jgen.writeStartObject();
jgen.writeStringField("name", value.getName());
for (String stringValue : value.getPics()) {
jgen.writeStringField(fieldName + i, stringValue);
i++;
}
jgen.writeEndObject();
}
}
Deserializer:
public class MyDeSerializer extends JsonDeserializer<Foo> {
@Override
public Foo deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectCodec objectCodec = jp.getCodec();
Foo foo = new Foo();
JsonNode node = objectCodec.readTree(jp);
Iterator<Entry<String, JsonNode>> fields = node.fields();
String[] pics = new String[node.size() - 1];
int i = 0;
while (fields.hasNext()) {
Entry<String, JsonNode> next = fields.next();
if (next.getKey().equalsIgnoreCase("name"))
foo.setName(node.get("name").asText());
else {
pics[i] = node.get(next.getKey()).asText();
i++;
}
}
foo.setPics(pics);
return foo;
}
}