Skip to content

Commit

Permalink
Addressed #16
Browse files Browse the repository at this point in the history
Changed optional fields in HalLink to use java Optional.
Added HalLinkSerializer to fix odd issue where Optional is not serialized correctly despite jackson module.
Made indentation uniform
  • Loading branch information
nrktkt committed Jun 18, 2016
1 parent 4b37a4a commit ad6e068
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 343 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ More info in the [wiki](https://github.com/blackdoor/hate/wiki).
<dependency>
<groupId>black.door</groupId>
<artifactId>hate</artifactId>
<version>v1r3t2</version>
<version>v1r4t0</version>
</dependency>
</dependencies>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<groupId>black.door</groupId>
<artifactId>hate</artifactId>
<version>v1r3t2</version>
<version>v1r4t0</version>

<developers>
<developer>
Expand Down
67 changes: 53 additions & 14 deletions src/main/java/black/door/hate/HalLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -18,31 +23,31 @@
*/
@Getter
@EqualsAndHashCode
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonSerialize(using = HalLink.HalLinkSerializer.class)
public class HalLink implements LinkOrResource{

private @NonNull final String href;

@Getter(AccessLevel.NONE) @JsonProperty
private final Boolean templated;

private final String type;
private final URL deprecation;
private final String name;
private final URI profile;
private final String title;
private final String hreflang;
private final Optional<String> type;
private final Optional<URL> deprecation;
private final Optional<String> name;
private final Optional<URI> profile;
private final Optional<String> title;
private final Optional<String> hreflang;

@java.beans.ConstructorProperties({"href", "templated", "type", "deprecation", "name", "profile", "title", "hreflang"})
HalLink(String href, Boolean templated, String type, URL deprecation, String name, URI profile, String title, String hreflang) {
this.href = href;
this.templated = templated;
this.type = type;
this.deprecation = deprecation;
this.name = name;
this.profile = profile;
this.title = title;
this.hreflang = hreflang;
this.type = Optional.ofNullable(type);
this.deprecation = Optional.ofNullable(deprecation);
this.name = Optional.ofNullable(name);
this.profile = Optional.ofNullable(profile);
this.title = Optional.ofNullable(title);
this.hreflang = Optional.ofNullable(hreflang);
}

public static HalLinkBuilder builder() {
Expand Down Expand Up @@ -143,4 +148,38 @@ public HalLink build() {
}

}

public static class HalLinkSerializer extends StdSerializer<HalLink> {

protected HalLinkSerializer() {
this(HalLink.class);
}

protected HalLinkSerializer(Class<HalLink> clazz) {
super(clazz);
}

@Override
public void serialize(HalLink value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();

provider.defaultSerializeField("href", value.href, gen);
if(value.templated != null)
provider.defaultSerializeField("templated", value.templated, gen);
if(value.type.isPresent())
provider.defaultSerializeField("type", value.type, gen);
if(value.deprecation.isPresent())
provider.defaultSerializeField("deprecation", value.deprecation, gen);
if(value.name.isPresent())
provider.defaultSerializeField("name", value.name, gen);
if(value.profile.isPresent())
provider.defaultSerializeField("profile", value.profile, gen);
if(value.title.isPresent())
provider.defaultSerializeField("title", value.title, gen);
if(value.hreflang.isPresent())
provider.defaultSerializeField("hreflang", value.hreflang, gen);

gen.writeEndObject();
}
}
}
25 changes: 11 additions & 14 deletions src/main/java/black/door/hate/HalRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static black.door.hate.Constants.*;
import static black.door.util.Misc.require;
import static java.util.Map.Entry;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -55,17 +56,12 @@ public class HalRepresentation implements java.io.Serializable {
Map<String, HalResource> embedded,
Map<String, List<HalResource>> multiEmbedded,
Map<String, Object> properties) {
require(null != links);
require(null != multiLinks);
require(null != embedded);
require(null != multiEmbedded);
require(null != properties);

this.links = links;
this.multiLinks = multiLinks;
this.embedded = embedded;
this.multiEmbedded = multiEmbedded;
this.properties = properties;

this.links = requireNonNull(links);
this.multiLinks = requireNonNull(multiLinks);
this.embedded = requireNonNull(embedded);
this.multiEmbedded = requireNonNull(multiEmbedded);
this.properties = requireNonNull(properties);
}

static ObjectMapper getMapper(){
Expand Down Expand Up @@ -130,8 +126,8 @@ protected HalRepresentationSerializer(Class<HalRepresentation> t) {

@Override
public void serialize(HalRepresentation halRepresentation,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException{
jsonGenerator.writeStartObject();

Expand Down Expand Up @@ -171,6 +167,7 @@ public void serialize(HalRepresentation halRepresentation,
.collect(toList())
));


//put all embedded resources and collections of embedded resources into one object
Map<String, Object> embedded = new HashMap<>();
embedded.putAll(embeddz);
Expand Down Expand Up @@ -266,7 +263,7 @@ public HalRepresentationBuilder addProperties(JsonNode jax){
* @param multiRs
*/
private <T extends LinkOrResource> void add(String name, T res, Map<String, T> rs,
Map<String, List<T>> multiRs){
Map<String, List<T>> multiRs){

if(res == null && ignoreNullResources)
return;
Expand Down
25 changes: 23 additions & 2 deletions src/test/java/black/door/hate/HalLinkTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package black.door.hate;

import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.val;
import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import static org.junit.Assert.*;

Expand All @@ -14,13 +18,30 @@
*/
public class HalLinkTest {

private ObjectMapper mapper = new ObjectMapper()
.findAndRegisterModules();

@Test
public void testSerialization(){
public void testSerialization() throws Exception {
val link = HalLink.builder()
.href(UriTemplate.fromTemplate("/~{username}"))
.build();

assertTrue(new ObjectMapper().valueToTree(link).get("templated").asBoolean());
JsonNode node = mapper.valueToTree(link);
assertTrue(node.get("templated").asBoolean());
assertFalse(node.has("name"));
System.out.println(mapper.writeValueAsString(link));

val link2 = HalLink.builder()
.href(URI.create("/path"))
.deprecation(new URL("https://google.com"))
.hreflang("eng")
.name("link")
.profile(URI.create("thing.black"))
.title("title")
.build();

System.out.println(mapper.writeValueAsString(link2));
}

@Test
Expand Down
Loading

0 comments on commit ad6e068

Please sign in to comment.