io.github.stewseo.client.json.jackson.JacksonJsonpMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yelp-fusion-client Show documentation
Show all versions of yelp-fusion-client Show documentation
java client to build api objects, handle http transport, and parse/deserialize/serialize json to/from json
package io.github.stewseo.client.json.jackson;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import io.github.stewseo.client.json.JsonpDeserializer;
import io.github.stewseo.client.json.JsonpDeserializerBase;
import io.github.stewseo.client.json.JsonpMapper;
import io.github.stewseo.client.json.JsonpMapperBase;
import io.github.stewseo.client.json.JsonpSerializer;
import jakarta.json.spi.JsonProvider;
import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.EnumSet;
public class JacksonJsonpMapper extends JsonpMapperBase {
private final JacksonJsonProvider provider;
private final ObjectMapper objectMapper;
private JacksonJsonpMapper(ObjectMapper objectMapper, JacksonJsonProvider provider) {
this.objectMapper = objectMapper;
this.provider = provider;
}
public JacksonJsonpMapper(ObjectMapper objectMapper) {
this(objectMapper,
// Creating the json factory from the mapper ensures it will be returned by JsonParser.getCodec()
new JacksonJsonProvider(objectMapper.getFactory())
);
}
public JacksonJsonpMapper() {
this(new ObjectMapper()
.configure(SerializationFeature.INDENT_OUTPUT, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
);
}
@Override
public JsonpMapper withAttribute(String name, T value) {
return new JacksonJsonpMapper(this.objectMapper, this.provider).addAttribute(name, value);
}
/**
* Returns the underlying Jackson mapper.
*/
public ObjectMapper objectMapper() {
return this.objectMapper;
}
@Override
public JsonProvider jsonProvider() {
return provider;
}
@Override
protected JsonpDeserializer getDefaultDeserializer(Type type) {
return new JacksonValueParser<>(type);
}
@Override
public void serialize(T value, JsonGenerator generator) {
if (!(generator instanceof JacksonJsonpGenerator)) {
throw new IllegalArgumentException("Jackson's ObjectMapper can only be used with the JacksonJsonpProvider");
}
JsonpSerializer serializer = findSerializer(value);
if (serializer != null) {
serializer.serialize(value, generator, this);
return;
}
com.fasterxml.jackson.core.JsonGenerator jkGenerator = ((JacksonJsonpGenerator)generator).jacksonGenerator();
try {
objectMapper.writeValue(jkGenerator, value);
} catch (IOException ioe) {
throw JacksonUtils.convertException(ioe);
}
}
private class JacksonValueParser extends JsonpDeserializerBase {
private final Type type;
protected JacksonValueParser(Type type) {
super(EnumSet.allOf(JsonParser.Event.class));
this.type = type;
}
@Override
public T deserialize(JsonParser parser, JsonpMapper mapper, JsonParser.Event event) {
if (!(parser instanceof JacksonJsonpParser)) {
throw new IllegalArgumentException("Jackson's ObjectMapper can only be used with the JacksonJsonpProvider");
}
com.fasterxml.jackson.core.JsonParser jkParser = ((JacksonJsonpParser)parser).jacksonParser();
try {
return objectMapper.readValue(jkParser, objectMapper().constructType(type));
} catch(IOException ioe) {
throw JacksonUtils.convertException(ioe);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy