Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Last active December 27, 2015 19:09
Show Gist options
  • Save volgar1x/7375330 to your computer and use it in GitHub Desktop.
Save volgar1x/7375330 to your computer and use it in GitHub Desktop.
{
"parents" : [ {
"id" : 1,
"name" : "DADDY",
"children" : [ 1, 2 ]
} ],
"children" : [ {
"id" : 1,
"name" : "JUNIOR"
}, {
"id" : 2,
"name" : "JUNIOR'S SLUT SISTER"
} ]
}
package org.photon;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.util.*;
public class Main {
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public static class Parent {
public long id;
public String name;
@JsonIdentityReference(alwaysAsId = true)
@JsonManagedReference
public Set<Child> children = new HashSet<>();
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public static class Child {
public long id;
public String name;
@JsonIdentityReference(alwaysAsId = true)
@JsonBackReference
public Parent parent;
}
public static class Registry {
public Set<Parent> parents = new HashSet<>();
public Set<Child> children = new HashSet<>();
}
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
Parent parent = new Parent();
parent.id = 1;
parent.name = "DADDY";
Child child1 = new Child();
child1.id = 1;
child1.name = "JUNIOR";
Child child2 = new Child();
child2.id = 2;
child2.name = "JUNIOR'S SLUT SISTER";
child1.parent = parent;
parent.children.add(child1);
child2.parent = parent;
parent.children.add(child2);
Registry registry = new Registry();
registry.parents.add(parent);
registry.children.add(child1);
registry.children.add(child2);
try {
mapper.writeValue(System.out, registry);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment