Last active
September 24, 2017 06:47
-
-
Save varren/53c4723a90dc7dc8f0f0ec3ba0c5ea45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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