Skip to content

Instantly share code, notes, and snippets.

@varren
Last active September 24, 2017 06:47
Show Gist options
  • Save varren/53c4723a90dc7dc8f0f0ec3ba0c5ea45 to your computer and use it in GitHub Desktop.
Save varren/53c4723a90dc7dc8f0f0ec3ba0c5ea45 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.*;
public class Main3 {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode types = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("original.json"));
JsonNode postTypes = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("reference.json"));
types.forEach(info -> { // foreach Info in original original.json
postTypes.forEach(newInfo -> { // foreach Info in new update reference.json
if (Objects.equals(info.get("type"), newInfo.get("type"))) {
List<JsonNode> merged = mergeAttributes(
(ArrayNode) info.get("attributes"),
(ArrayNode) newInfo.get("attributes"));
((ObjectNode) info).putPOJO("attributes", merged);
}
});
});
System.out.println("final types: " + types);
}
private static List<JsonNode> mergeAttributes(ArrayNode originalAttrs, ArrayNode newAttrs) {
ArrayList<JsonNode> result = new ArrayList<>();
originalAttrs.forEach(oldAttr ->
newAttrs.forEach(newAttr -> {
if (Objects.equals(newAttr.get("name"), oldAttr.get("name"))) {
newAttr.fieldNames().forEachRemaining(field ->
// put every new field and value like "displayName" to saved original json
((ObjectNode) oldAttr).set(field, newAttr.get(field))
);
result.add(oldAttr);
}
})
);
return result;
}
}
public class Test {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
InputStream data = Test.class.getClassLoader()
.getResourceAsStream("original.json");
InputStream postProcessData = Test.class.getClassLoader()
.getResourceAsStream("reference.json");
Set<Info> types = mapper.readValue(data,
new TypeReference<Set<Info>>() {
});
ObjectReader updater = mapper.readerForUpdating(types);
Set<Info> merged = updater.readValue(postProcessData);
System.out.print(mapper.writeValueAsString(merged));
}
public static class Info {
@JsonProperty("type")
private String type;
@JsonProperty("attributes")
private List<Attribute> attributes = new ArrayList<>();
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean equals(Object o) {
return o instanceof Info &&
Objects.equals(((Info) o).type, type);
}
}
public static class Attribute{
@JsonProperty("name")
private String name;
@JsonProperty("validOperators")
private List<String> validOperators;
@JsonProperty("displayName")
private List<String> displayName;
public List<String> getValidOperators() {
return validOperators;
}
public void setValidOperators(List<String> validOperators) {
this.validOperators = validOperators;
}
public List<String> getDisplayName() {
return displayName;
}
public void setDisplayName(List<String> displayName) {
this.displayName = displayName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
return o instanceof Attribute &&
Objects.equals(((Attribute) o).name, name);
}
}
}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Main3 {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode types = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("original.json"));
JsonNode postTypes = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("reference.json"));
types.forEach(info->{
if (info.get("type").asText().equals("com.person.EmployeeInfo")){
info.get("attributes").forEach(oldAttr->
postTypes.get(0).get("attributes").forEach(newAttr->{
if (newAttr.get("name").equals(oldAttr.get("name"))) {
((ObjectNode)oldAttr).set("displayName", newAttr.get("displayName"));
}
})
);
}});
System.out.println("final types: " + types);
}
}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class Main3 {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode types = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("original.json"));
JsonNode postTypes = mapper.readTree(Main3.class.getClassLoader()
.getResourceAsStream("reference.json"));
JsonNode postTypeNode = postTypes.get(0);
ArrayNode postType = (ArrayNode) postTypeNode.get("attributes");
for (JsonNode domainNode: types) {
if (domainNode.get("type").asText().equals("com.person.EmployeeInfo")) {
ArrayNode attributeNodes = (ArrayNode) domainNode.get("attributes");
for (JsonNode attrNode : attributeNodes) {
for (JsonNode attrNodeNew: postType) {
if (attrNodeNew.get("name").equals(attrNode.get("name"))) {
((ObjectNode)attrNode).set("displayName", attrNodeNew.get("displayName"));
}
}
}
}
}
System.out.println("final types: " + types);
}
}
types.forEach(info -> {
if (info.get("type").asText().equals("com.person.EmployeeInfo")) {
ArrayList<JsonNode> temp = new ArrayList<JsonNode>();
// do calculations
info.get("attributes").forEach(oldAttr ->
postTypes.get(0).get("attributes").forEach(newAttr -> {
if (newAttr.get("name").equals(oldAttr.get("name"))) {
temp.add(((ObjectNode) oldAttr).set("displayName", newAttr.get("displayName")));
}
})
);
// replace attributes with temp
((ObjectNode) info).putPOJO("attributes", temp);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment