com.theoryinpractise.halbuilder.json.JsonRepresentationFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of halbuilder-json Show documentation
Show all versions of halbuilder-json Show documentation
HAL+JSON serializer/deserializer extension to the HalBuilder Library
package com.theoryinpractise.halbuilder.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.theoryinpractise.halbuilder.DefaultRepresentationFactory;
import com.theoryinpractise.halbuilder.api.ContentRepresentation;
import com.theoryinpractise.halbuilder.api.RepresentationWriter;
import com.theoryinpractise.halbuilder.impl.ContentType;
import java.io.Reader;
import javax.annotation.Nullable;
/** Simple representation factory configured for JSON usage. */
public class JsonRepresentationFactory extends DefaultRepresentationFactory {
@Nullable private ObjectMapper mapper;
public JsonRepresentationFactory() {
this(null);
}
public JsonRepresentationFactory(@Nullable ObjectMapper mapper) {
withRenderer(HAL_JSON, JsonRepresentationWriter.class);
withReader(HAL_JSON, JsonRepresentationReader.class);
this.mapper = mapper;
}
@Override
public ContentRepresentation readRepresentation(String contentType, Reader reader) {
if (mapper != null && new ContentType(contentType).matches(HAL_JSON)) {
return new JsonRepresentationReader(this, mapper).read(reader);
}
return super.readRepresentation(contentType, reader);
}
@Override
public RepresentationWriter lookupRenderer(String contentType) {
if (mapper != null && new ContentType(contentType).matches(HAL_JSON)) {
return new JsonRepresentationWriter(mapper);
}
return super.lookupRenderer(contentType);
}
}