Skip to content

Commit

Permalink
adding support for @JsonBackreference (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jun 7, 2016
1 parent 0a13818 commit a1d3ae9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
@@ -1,8 +1,10 @@
package com.monitorjbl.json;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand Down Expand Up @@ -244,7 +246,8 @@ boolean valueAllowed(Object value, Class cls) {
|| (serializerProvider.getConfig() != null
&& serializerProvider.getConfig().getSerializationInclusion() == Include.ALWAYS
&& cls.getAnnotation(JsonSerialize.class) == null)
|| (cls.getAnnotation(JsonSerialize.class) != null && readClassAnnotation(cls, JsonSerialize.class, "include") == Inclusion.ALWAYS);
|| (cls.getAnnotation(JsonSerialize.class) != null
&& readClassAnnotation(cls, JsonSerialize.class, "include") == Inclusion.ALWAYS);
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -362,13 +365,28 @@ boolean annotatedWithIgnore(Field f) {
JsonIgnore jsonIgnore = f.getAnnotation(JsonIgnore.class);
JsonIgnoreProperties classIgnoreProperties = f.getDeclaringClass().getAnnotation(JsonIgnoreProperties.class);
JsonIgnoreProperties fieldIgnoreProperties = null;
boolean backReferenced = false;

//make sure the referring field didn't specify properties to ignore
if(referringField != null) {
fieldIgnoreProperties = referringField.getAnnotation(JsonIgnoreProperties.class);
}

//make sure the referring field didn't specify a backreference annotation
if(f.getAnnotation(JsonBackReference.class) != null && referringField != null) {
for(Field lastField : referringField.getDeclaringClass().getDeclaredFields()) {
JsonManagedReference fieldManagedReference = lastField.getAnnotation(JsonManagedReference.class);
if(fieldManagedReference != null && lastField.getType().equals(f.getDeclaringClass())) {
backReferenced = true;
break;
}
}
}

return (jsonIgnore != null && jsonIgnore.value()) ||
(classIgnoreProperties != null && Arrays.asList(classIgnoreProperties.value()).contains(f.getName())) ||
(fieldIgnoreProperties != null && Arrays.asList(fieldIgnoreProperties.value()).contains(f.getName()));
(fieldIgnoreProperties != null && Arrays.asList(fieldIgnoreProperties.value()).contains(f.getName())) ||
backReferenced;
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -7,6 +7,8 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
import com.google.common.primitives.Ints;
import com.monitorjbl.json.model.TestBackreferenceObject;
import com.monitorjbl.json.model.TestBackreferenceObject.TestForwardReferenceObject;
import com.monitorjbl.json.model.TestChildObject;
import com.monitorjbl.json.model.TestNonNulls;
import com.monitorjbl.json.model.TestNulls;
Expand Down Expand Up @@ -670,4 +672,20 @@ public void testIgnorePropertiesOnField() throws Exception {
assertNotNull(obj.get("subWithIgnores").get("otherVal"));
assertNull(obj.get("subWithIgnores").get("val"));
}

@Test
public void testBackReferenceSupport() throws Exception {
TestForwardReferenceObject forward = new TestForwardReferenceObject();
TestBackreferenceObject back = new TestBackreferenceObject();

forward.setId("forward");
forward.setParent(back);
back.setId("back");
back.setChildren(asList(forward));

String serialized = sut.writeValueAsString(JsonView.with(forward));
Map<String, Map<String, Object>> obj = sut.readValue(serialized, HashMap.class);

System.out.println(obj);
}
}
@@ -0,0 +1,52 @@
package com.monitorjbl.json.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import java.util.List;

public class TestBackreferenceObject {

private String id;
@JsonBackReference
private List<TestForwardReferenceObject> children;


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public List<TestForwardReferenceObject> getChildren() {
return children;
}

public void setChildren(List<TestForwardReferenceObject> children) {
this.children = children;
}

public static class TestForwardReferenceObject{
private String id;
@JsonManagedReference
private TestBackreferenceObject parent;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public TestBackreferenceObject getParent() {
return parent;
}

public void setParent(TestBackreferenceObject parent) {
this.parent = parent;
}
}
}

0 comments on commit a1d3ae9

Please sign in to comment.