in jsonschema-kafkaconnect-converter/src/main/java/com/amazonaws/services/schemaregistry/kafkaconnect/jsonschema/typeconverters/MapTypeConverter.java [81:128]
public JsonNode toJson(final Schema schema,
final Object value,
final JsonSchemaDataConfig config) {
connectValueToJsonNodeConverter = new ConnectValueToJsonNodeConverter(config);
Map<?, ?> map = (Map<?, ?>) value;
boolean objectMode;
if (schema == null) {
objectMode = true;
for (Map.Entry<?, ?> entry : map.entrySet()) {
if (!(entry.getKey() instanceof String)) {
objectMode = false;
break;
}
}
} else {
boolean isKeySchemaStringType = Schema.Type.STRING.equals(schema.keySchema()
.type());
boolean isKeySchemaOptional = schema.keySchema()
.isOptional();
objectMode = isKeySchemaStringType && !isKeySchemaOptional;
}
ObjectNode obj = null;
ArrayNode list = null;
if (objectMode) {
obj = JSON_NODE_FACTORY.objectNode();
} else {
list = JSON_NODE_FACTORY.arrayNode();
}
for (Map.Entry<?, ?> entry : map.entrySet()) {
Schema keySchema = schema == null ? null : schema.keySchema();
Schema valueSchema = schema == null ? null : schema.valueSchema();
JsonNode mapKey = connectValueToJsonNodeConverter.convertToJson(keySchema, entry.getKey());
JsonNode mapValue = connectValueToJsonNodeConverter.convertToJson(valueSchema, entry.getValue());
if (objectMode) {
obj.set(mapKey.asText(), mapValue);
} else {
list.add(JSON_NODE_FACTORY.objectNode()
.setAll(new HashMap<String, JsonNode>() {{
put(JsonSchemaConverterConstants.KEY_FIELD, mapKey);
put(JsonSchemaConverterConstants.VALUE_FIELD, mapValue);
}}));
}
}
return objectMode ? obj : list;
}