Версия сериализатора.
package com.example;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.module.SimpleSerializers;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
public class Stackoverflow62009220_B {
public static void main(String[] args) throws JsonProcessingException {
// see https://www.baeldung.com/jackson-call-default-serializer-from-custom-serializer
convert("{\"elementName\":{\"id\":{\"type\":\"pid\",\"\":\"abcdef123\"}}}");
// j = {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}
// (simple json object)
String j = "{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}";
convert(j);
// g = {"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}],"obj":{"":"is_empty_field","num":1,"str":"aa","null_val":null,"empty_val":"","array":[3,5],"obj":{"a":"A","b":22}}}
// (includes an array containing object j, and an object j containing array)
String g = " {\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}],\"obj\":{\"\":\"is_empty_field\",\"num\":1,\"str\":\"aa\",\"null_val\":null,\"empty_val\":\"\",\"array\":[3,5],\"obj\":{\"a\":\"A\",\"b\":22}}}";
convert(g);
}
private static void convert(String str) throws JsonProcessingException {
JsonNode input = (new ObjectMapper()).readTree(str);
System.out.println("in:");
System.out.println(input);
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
SimpleSerializers serializers = new SimpleSerializers();
serializers.addSerializer(ObjectNode.class, new MyObjectNodeSerializer());
module.setSerializers(serializers);
mapper.registerModule(module);
String output = mapper.writer().writeValueAsString(input);
System.out.println("out:");
System.out.println(output);
System.out.println("----------");
}
}
class MyObjectNodeSerializer extends StdSerializer<ObjectNode> {
public MyObjectNodeSerializer() {
super(ObjectNode.class);
}
public static MyObjectNodeSerializer create() {
return new MyObjectNodeSerializer();
}
@Override
public void serialize(ObjectNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
for (Iterator<String> it = value.fieldNames(); it.hasNext();) {
String childName = it.next();
JsonNode childNode = value.get(childName);
if (Objects.equals("", childName)) {
childName = "value";
}
if (childNode instanceof ArrayNode) {
gen.writeFieldName(childName);
MyArrayNodeSerializer.create().serialize((ArrayNode) childNode, gen, provider);
} else if (childNode instanceof ObjectNode) {
gen.writeFieldName(childName);
this.serialize((ObjectNode) childNode, gen, provider);
} else {
provider.defaultSerializeField(childName, childNode, gen);
}
}
gen.writeEndObject();
}
}
class MyArrayNodeSerializer extends StdSerializer<ArrayNode> {
public MyArrayNodeSerializer() {
super(ArrayNode.class);
}
public static MyArrayNodeSerializer create() {
return new MyArrayNodeSerializer();
}
@Override
public void serialize(ArrayNode value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartArray();
for (Iterator<JsonNode> it = value.elements(); it.hasNext();) {
JsonNode childNode = it.next();
if (childNode instanceof ArrayNode) {
this.serialize((ArrayNode) childNode, gen, provider);
} else if (childNode instanceof ObjectNode) {
MyObjectNodeSerializer.create().serialize((ObjectNode) childNode, gen, provider);
} else {
provider.defaultSerializeValue(childNode, gen);
}
}
gen.writeEndArray();
}
}