Code review comment: Yes, I will remove all dead code in the class to have a more in-depth investigation on supporting handling serialization of JsonFlatten classes/fields ourselves.

Code before review:
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value == null) {
jgen.writeNull();
return;
}
escapeMapKeys(value);
ObjectNode root = mapper.valueToTree(value);
ObjectNode res = root.deepCopy();
Queue<ObjectNode> source = new LinkedBlockingQueue<>();
Queue<ObjectNode> target = new LinkedBlockingQueue<>();
source.add(root);
target.add(res);
while (!source.isEmpty()) {
ObjectNode current = source.poll();
ObjectNode resCurrent = target.poll();
Iterator<Map.Entry<String, JsonNode>> fields = current.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
ObjectNode node = resCurrent;
String key = field.getKey();
JsonNode outNode = resCurrent.get(key);
if (!classHasJsonFlatten && !jsonPropertiesWithJsonFlatten.contains(key)) {
continue;
}
if (CHECK_IF_FLATTEN_PROPERTY_PATTERN.matcher(key).matches()) {
String[] values = SPLIT_FLATTEN_PROPERTY_PATTERN.split(key);
for (int i = 0; i < values.length; ++i) {
values[i] = values[i].replace("\\.", ".");
if (i == values.length - 1) {
break;
}
String val = values[i];
if (node.has(val)) {
node = (ObjectNode) node.get(val);
} else {
ObjectNode child = new ObjectNode(JsonNodeFactory.instance);
node.set(val, child);
node = child;
}
}
node.set(values[values.length - 1], resCurrent.get(key));
resCurrent.remove(key);
outNode = node.get(values[values.length - 1]);
} else if (CHECK_IF_ESCAPED_MAP_PATTERN.matcher(key).matches()) {
String originalKey = REPLACE_ESCAPED_MAP_PATTERN.matcher(key).replaceAll(".");
resCurrent.remove(key);
resCurrent.set(originalKey, outNode);
}
if (field.getValue() instanceof ObjectNode) {
source.add((ObjectNode) field.getValue());
target.add((ObjectNode) outNode);
} else if (field.getValue() instanceof ArrayNode
&& (field.getValue()).size() > 0
&& (field.getValue()).get(0) instanceof ObjectNode) {
Iterator<JsonNode> sourceIt = field.getValue().elements();
Iterator<JsonNode> targetIt = outNode.elements();
while (sourceIt.hasNext()) {
source.add((ObjectNode) sourceIt.next());
target.add((ObjectNode) targetIt.next());
}
}
}
}
jgen.writeTree(res);
}


Code after addressing the review:
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value == null) {
jgen.writeNull();
return;
}
escapeMapKeys(value);
ObjectNode root = mapper.valueToTree(value);
ObjectNode res = root.deepCopy();
Queue<ObjectNode> source = new LinkedBlockingQueue<>();
Queue<ObjectNode> target = new LinkedBlockingQueue<>();
source.add(root);
target.add(res);
while (!source.isEmpty()) {
ObjectNode current = source.poll();
ObjectNode resCurrent = target.poll();
Iterator<Map.Entry<String, JsonNode>> fields = current.fields();
while (fields.hasNext()) {
Map.Entry<String, JsonNode> field = fields.next();
ObjectNode node = resCurrent;
String key = field.getKey();
JsonNode outNode = resCurrent.get(key);
if (!classHasJsonFlatten && !jsonPropertiesWithJsonFlatten.contains(key)) {
continue;
}
if (CHECK_IF_FLATTEN_PROPERTY_PATTERN.matcher(key).matches()) {
String[] values = SPLIT_FLATTEN_PROPERTY_PATTERN.split(key);
for (int i = 0; i < values.length; ++i) {
values[i] = values[i].replace("\\.", ".");
if (i == values.length - 1) {
break;
}
String val = values[i];
if (node.has(val)) {
node = (ObjectNode) node.get(val);
} else {
ObjectNode child = new ObjectNode(JsonNodeFactory.instance);
node.set(val, child);
node = child;
}
}
node.set(values[values.length - 1], resCurrent.get(key));
resCurrent.remove(key);
outNode = node.get(values[values.length - 1]);
} else if (CHECK_IF_ESCAPED_MAP_PATTERN.matcher(key).matches()) {
String originalKey = REPLACE_ESCAPED_MAP_PATTERN.matcher(key).replaceAll(".");
resCurrent.remove(key);
resCurrent.set(originalKey, outNode);
}
if (field.getValue() instanceof ObjectNode) {
source.add((ObjectNode) field.getValue());
target.add((ObjectNode) outNode);
} else if (field.getValue() instanceof ArrayNode
&& (field.getValue()).size() > 0
&& (field.getValue()).get(0) instanceof ObjectNode) {
Iterator<JsonNode> sourceIt = field.getValue().elements();
Iterator<JsonNode> targetIt = outNode.elements();
while (sourceIt.hasNext()) {
source.add((ObjectNode) sourceIt.next());
target.add((ObjectNode) targetIt.next());
}
}
}
}
jgen.writeTree(res);
}