#137 - Merged Link and LinkTemplate.

Link now exposes additional methods like isTemplate(), getTemplateVariables() and expand(…).
This commit is contained in:
Oliver Gierke
2014-01-19 18:38:53 +01:00
parent bc97010cad
commit 6e9caf2556
13 changed files with 164 additions and 153 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,7 +76,6 @@ public class DefaultCurieProvider implements CurieProvider {
private static final long serialVersionUID = 1L;
private final String name;
private final boolean templated = true;
public Curie(String name, String href) {
@@ -87,9 +86,5 @@ public class DefaultCurieProvider implements CurieProvider {
public String getName() {
return name;
}
public boolean isTemplated() {
return templated;
}
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.hateoas.hal;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -27,7 +28,6 @@ import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkTemplate;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.ResourceSupport;
@@ -47,6 +47,7 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -57,6 +58,7 @@ import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.ContainerDeserializerBase;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
@@ -64,6 +66,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.MapSerializer;
import com.fasterxml.jackson.databind.ser.std.NonTypedScalarSerializerBase;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
@@ -81,7 +84,6 @@ public class Jackson2HalModule extends SimpleModule {
super("json-hal-module", new Version(1, 0, 0, null, "org.springframework.hateoas", "spring-hateoas"));
setMixInAnnotation(Link.class, LinkMixin.class);
setMixInAnnotation(LinkTemplate.class, LinkTemplateMixin.class);
setMixInAnnotation(ResourceSupport.class, ResourceSupportMixin.class);
setMixInAnnotation(Resources.class, ResourcesMixin.class);
}
@@ -654,4 +656,41 @@ public class Jackson2HalModule extends SimpleModule {
return (TypeIdResolver) findInstance(resolverClass);
}
}
/**
* {@link JsonSerializer} to only render {@link Boolean} values if they're set to {@literal true}.
*
* @author Oliver Gierke
* @since 0.9
*/
public static class TrueOnlyBooleanSerializer extends NonTypedScalarSerializerBase<Boolean> {
public TrueOnlyBooleanSerializer() {
super(Boolean.class);
}
@Override
public boolean isEmpty(Boolean value) {
return value == null || Boolean.FALSE.equals(value);
}
@Override
public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
JsonGenerationException {
jgen.writeBoolean(value.booleanValue());
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return createSchemaNode("boolean", true);
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException {
if (visitor != null) {
visitor.expectBooleanFormat(typeHint);
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2013 the original author or authors.
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,11 @@
package org.springframework.hateoas.hal;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.hal.Jackson2HalModule.TrueOnlyBooleanSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
/**
* Custom mixin to avoid rel attributes being rendered for HAL.
@@ -25,6 +30,16 @@ import org.springframework.hateoas.Link;
*/
@org.codehaus.jackson.annotate.JsonIgnoreProperties(value = "rel")
@com.fasterxml.jackson.annotation.JsonIgnoreProperties(value = "rel")
class LinkMixin extends Link {
abstract class LinkMixin extends Link {
private static final long serialVersionUID = 4720588561299667409L;
/*
* (non-Javadoc)
* @see org.springframework.hateoas.Link#isTemplate()
*/
@Override
@JsonInclude(Include.NON_EMPTY)
@JsonSerialize(using = TrueOnlyBooleanSerializer.class)
public abstract boolean isTemplate();
}

View File

@@ -1,35 +0,0 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas.hal;
import java.util.List;
import org.springframework.hateoas.LinkTemplate;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Mixin for {@link LinkTemplate}s.
*
* @author Oliver Gierke
*/
abstract class LinkTemplateMixin extends LinkMixin {
private static final long serialVersionUID = -7227899604445243148L;
@JsonIgnore
public abstract List<String> getVariableNames();
}