use of com.fasterxml.jackson.databind.SerializerProvider in project openremote by openremote.
the class EnumWithInterfacesExtension method emitElements.
@SuppressWarnings("rawtypes")
@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
if (matchedEnums.isEmpty()) {
return;
}
ObjectMapper objectMapper = new ObjectMapper().registerModule(new SimpleModule().addSerializer(Enum.class, new JsonSerializer<Enum>() {
@Override
public void serialize(Enum anEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeRawValue(anEnum.getClass().getSimpleName() + "." + anEnum.name());
}
})).setAnnotationIntrospector(// Ignore Shape Object so we get proper enum references
ignoreJsonFormatIntrospector);
if (!supportsNullValuesPredicate.test(settings)) {
// Don't output nulls if settings don't support it
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
// Do a topological sort of matched enums to ensure they are output in the correct order
sortEnums(matchedEnums);
for (TsEnumModel tsEnum : matchedEnums) {
final EnumInterfaceModel enumInterfaceModel = parseEnum(tsEnum, model, settings);
// TODO: TsEnumModel should have interface details like TsBeanModel
String implementsStr = Arrays.stream(tsEnum.getOrigin().getInterfaces()).map(i -> {
TsBeanModel beanModel = model.getBean(i);
return beanModel != null ? beanModel.getName().getFullName() : i.getSimpleName();
}).collect(Collectors.joining(" | "));
String fieldsStr = enumInterfaceModel.getFieldTypeMap().entrySet().stream().map(es -> es.getKey() + ": " + es.getValue()).collect(Collectors.joining(TMPL_ENUM_FIELD_SEPARATOR));
if (!fieldsStr.isEmpty()) {
fieldsStr = TMPL_ENUM_FIELD_SEPARATOR + fieldsStr;
}
final Map<String, String> replacements = new LinkedHashMap<>();
replacements.put("\"", settings.quotes);
replacements.put("/*export*/ ", exportKeyword ? "export " : "");
replacements.put(TMPL_ENUM_NAME, tsEnum.getName().getSimpleName());
replacements.put(TMPL_ENUM_IMPLEMENTS, implementsStr);
replacements.put(TMPL_ENUM_FIELDS, fieldsStr);
Emitter.writeTemplate(writer, settings, ENUM_CLASS_HEADER_TEMPLATE, replacements);
enumInterfaceModel.getMemberModels().forEach(enumInterfaceMemberModel -> {
String memberFieldValues = enumInterfaceMemberModel.getMemberFieldValues().entrySet().stream().map(es -> getMemberValue(tsEnum.getName().getFullName(), es.getKey(), es.getValue(), settings, objectMapper)).collect(Collectors.joining(TMPL_ENUM_MEMBER_VALUE_SEPARATOR));
if (!memberFieldValues.isEmpty()) {
memberFieldValues = TMPL_ENUM_MEMBER_VALUE_SEPARATOR + memberFieldValues;
}
replacements.put(TMPL_ENUM_MEMBER_NAME, enumInterfaceMemberModel.getMemberName());
replacements.put(TMPL_ENUM_MEMBER_FIELD_VALUES, memberFieldValues);
Emitter.writeTemplate(writer, settings, ENUM_CLASS_MEMBER_TEMPLATE, replacements);
});
Emitter.writeTemplate(writer, settings, ENUM_CLASS_FOOTER_TEMPLATE, replacements);
}
}
Aggregations