Skip to content

Commit

Permalink
Merge pull request #2 from civitaspo/v0.0.2
Browse files Browse the repository at this point in the history
V0.0.2
  • Loading branch information
civitaspo committed Oct 29, 2015
2 parents af00a16 + 6ac47e9 commit 33cd354
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/org/embulk/filter/json_key/JsonKeyFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public JsonNode doFilter(JsonNode rootNode)
// TODO: addKey is overwriting existing values.
private JsonNode addKey(JsonNode node, List<String> nestedKey, JsonNode value)
{
if (node == null) {
return null;
}

if (node.isObject()) {
return addKeyToObject(node, nestedKey, value);
}
Expand Down Expand Up @@ -131,7 +135,9 @@ else if (nestedKey.size() == 1) {
String parentKey = nestedKey.get(0);
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
JsonNode newNode = addKey(object.get(parentKey), newNestedKey, value);
object.set(parentKey, newNode);
if (newNode != null) {
object.set(parentKey, newNode);
}
return object;
}
}
Expand All @@ -156,7 +162,9 @@ else if (nestedKey.size() == 1) {
if (NumberUtils.isNumber(parentIdx)) {
int idx = Integer.parseInt(parentIdx);
JsonNode newNode = addKey(object.get(idx), newNestedKey, value);
object.set(idx, newNode);
if (newNode != null) {
object.set(idx, newNode);
}
}
return object;
}
Expand Down Expand Up @@ -184,6 +192,10 @@ public JsonNode doFilter(JsonNode rootNode)
// if NullNode has come before end of a nested key, return NullNode not EmptyObjectNode.
private JsonNode dropKey(JsonNode node, List<String> nestedKey)
{
if (node == null) {
return null;
}

if (node.isObject()) {
return dropKeyFromObject(node, nestedKey);
}
Expand All @@ -209,7 +221,9 @@ else if (nestedKey.size() == 1) {
String parentKey = nestedKey.get(0);
List<String> newNestedKey = nestedKey.subList(1, nestedKey.size());
JsonNode newNode = dropKey(object.get(parentKey), newNestedKey);
object.set(parentKey, newNode);
if (newNode != null) {
object.set(parentKey, newNode);
}
return object;
}
}
Expand All @@ -233,7 +247,9 @@ else if (nestedKey.size() == 1) {
if (NumberUtils.isNumber(parentIdx)) {
int idx = Integer.parseInt(parentIdx);
JsonNode newNode = dropKey(object.get(idx), newNestedKey);
object.set(idx, newNode);
if (newNode != null) {
object.set(idx, newNode);
}
}
return object;
}
Expand Down

0 comments on commit 33cd354

Please sign in to comment.