Skip to content

Commit

Permalink
Merge branch 'default-serializers'
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt committed Jan 21, 2016
2 parents 556d2df + 587d5e8 commit 626cba0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ Would give you something like this:
}
```

---
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>black.door</groupId>
<artifactId>hate</artifactId>
<version>v1r0t1-SNAPSHOT</version>
<version>v1r0t2</version>

<dependencies>
<dependency>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/black/door/hate/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package black.door.hate;

/**
* Created by nfischer on 12/9/2015.
*/
public interface Constants {
String _links = "_links";
String _embedded = "_embedded";
String HAL_JSON_CONTENT_TYPE = "application/hal+json";
}
2 changes: 2 additions & 0 deletions src/main/java/black/door/hate/HalLink.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package black.door.hate;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
Expand All @@ -12,6 +13,7 @@
*/
@Builder
@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class HalLink {
@NonNull
private URI href;
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/black/door/hate/HalRepresentation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;

import java.io.IOException;
Expand All @@ -18,20 +15,20 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static black.door.hate.Constants._embedded;
import static black.door.hate.Constants._links;
import static black.door.util.Misc.require;

/**
* Created by nfischer on 12/8/2015.
*/
@Getter
public class HalRepresentation {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonSerialize(using = HalRepresentation.HalRepresentationSerializer.class)
public class HalRepresentation implements java.io.Serializable {
private static final ObjectWriter WRITER;
static {
ObjectMapper mapper = new ObjectMapper();
SimpleModule mod = new SimpleModule();
mod.addSerializer(HalRepresentation.class, new HalRepresentationSerializer());
mapper.registerModule(mod);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
WRITER = mapper.writer();
}

Expand Down Expand Up @@ -116,13 +113,13 @@ public void serialize(HalRepresentation halRepresentation,
links.putAll(halRepresentation.links);
links.putAll(halRepresentation.multiLinks);
if(!links.isEmpty())
jsonGenerator.writeObjectField("_links", links);
jsonGenerator.writeObjectField(_links, links);

Map<String, Object> embedded = new HashMap<>();
embedded.putAll(halRepresentation.embedded);
embedded.putAll(halRepresentation.multiEmbedded);
if(!embedded.isEmpty())
jsonGenerator.writeObjectField("_embedded", embedded);
jsonGenerator.writeObjectField(_embedded, embedded);

jsonGenerator.writeEndObject();
}
Expand All @@ -148,6 +145,12 @@ public HalRepresentationBuilder addProperty(String name, Object prop){
return this;
}

public HalRepresentationBuilder addProperties(JsonNode jax){
require(jax.isObject());
jax.fields().forEachRemaining(e -> addProperty(e.getKey(), e.getValue()));
return this;
}

private <T> void add(String name, HalResource res, Map<String, T> rs,
Map<String, List<T>> multiRs,
Function<HalResource, T> trans){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package black.door.hate.example;

import black.door.hate.HalRepresentation;
import black.door.hate.HalResource;
import lombok.Getter;

/**
* Created by nfischer on 12/8/2015.
*/
public class Order extends Thing implements HalResource {
@Getter
public class Order extends Thing{

private double total;
private String currency;
Expand All @@ -24,6 +25,7 @@ public Order(long id, double total, String currency, String status,
this.customer = customer;
}


@Override
public HalRepresentation asEmbedded() {
return HalRepresentation.builder()
Expand All @@ -36,9 +38,9 @@ public HalRepresentation asEmbedded() {
.build();
}


@Override
protected String resName() {
return "orders";
}

}
12 changes: 11 additions & 1 deletion src/test/java/black/door/hate/example/OrdersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@
import static black.door.util.Misc.list;

import black.door.hate.HalRepresentation;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.val;
import org.junit.Test;

import java.util.Map;


/**
* Created by nfischer on 12/8/2015.
*/
public class OrdersTest {

@Test
public void test() throws Exception{
val basket1 = new Basket(97212);
Expand All @@ -28,7 +34,11 @@ public void test() throws Exception{
.addProperty("shippedToday", 20)
.build();

JsonNode node = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL).valueToTree(orderz);
System.out.println(new ObjectMapper().writeValueAsString(node));

System.out.println(orderz.serialize());
}


}

0 comments on commit 626cba0

Please sign in to comment.