ioinformarics.oss.jackson.module.jsonld.internal.JsonldResourceSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jackson-jsonld Show documentation
Show all versions of jackson-jsonld Show documentation
JSON-LD Module for Jackson
The newest version!
package ioinformarics.oss.jackson.module.jsonld.internal;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.BeanSerializer;
import com.fasterxml.jackson.databind.ser.std.BeanSerializerBase;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldLink;
import ioinformarics.oss.jackson.module.jsonld.util.JsonldResourceUtils;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.*;
/**
* @author Alexander De Leon ([email protected])
*/
public class JsonldResourceSerializer extends BeanSerializer {
public JsonldResourceSerializer(BeanSerializerBase src) {
super(src);
}
@Override
protected void serializeFields(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
Optional type = JsonldResourceUtils.dynamicTypeLookup(bean.getClass());
if(type.isPresent()) {
jgen.writeStringField("@type", type.get());
}
Optional context = JsonldResourceUtils.getContext(bean);
if(context.isPresent()) {
jgen.writeObjectField("@context", context.get());
}
super.serializeFields(bean, jgen, provider);
getLinks(bean).ifPresent(linksMap ->
linksMap.forEach((key, value) -> {
try {
jgen.writeStringField(key, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
protected Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy