Skip to content

Commit 8745243

Browse files
authored
Merge pull request #24 from erav/master
lets see all that changes.
2 parents cb03bcb + ad93cc5 commit 8745243

34 files changed

+2877
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ json-smart/target/
2020
*.classpath
2121
*.project
2222
*.prefs
23+
*.iml
24+
.idea

json-smart/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>net.minidev</groupId>
55
<artifactId>json-smart</artifactId>
6-
<version>2.2.1</version>
6+
<version>2.2.1-b-SNAPSHOT</version>
77
<name>JSON Small and Fast Parser</name>
88
<description>
99
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
@@ -23,6 +23,12 @@
2323
<roles>
2424
</roles>
2525
</developer>
26+
<developer>
27+
<id>erav</id>
28+
<name>Eitan Raviv</name>
29+
<email>adoneitan@gmail.com</email>
30+
<timezone>GMT+2</timezone>
31+
</developer>
2632
</developers>
2733
<licenses>
2834
<license>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package net.minidev.json.actions;
2+
3+
import net.minidev.json.JSONObject;
4+
import net.minidev.json.actions.traverse.JSONTraverser;
5+
import net.minidev.json.actions.traverse.RemoveElementsJsonAction;
6+
import net.minidev.json.actions.traverse.JSONTraverseAction;
7+
8+
import java.util.*;
9+
10+
/**
11+
* <b>Removes key:value elements from every node of a {@link JSONObject} matching the list of user-specified elements.</b>
12+
* <p>
13+
* An element to remove must be specified as a key:value pair
14+
* <p>
15+
* <b>Usage Example:</b>
16+
* <p>
17+
* To remove the element k2:v2 from the {@link JSONObject} {k0:{k2:v2, k3:v3}, k1:{k2:v2, k4:v4}} use the remover like so:
18+
* <pre>
19+
* PathRemover pr = new PathRemover("k2.v2");
20+
* JSONObject cleanObject = pr.remove(new JSONObject(...));
21+
* </pre>
22+
* The resulting object 'cleanObject' would be {k0:{k3:v3}, k1:{k4:v4}}
23+
* <p>
24+
* See unit tests for more examples
25+
*
26+
* @author adoneitan@gmail.com
27+
*
28+
*/
29+
public class ElementRemover
30+
{
31+
private Map<String, Object> elementsToRemove;
32+
33+
public ElementRemover(Map<String, Object> elementsToRemove)
34+
{
35+
this.elementsToRemove = elementsToRemove == null ? Collections.<String, Object>emptyMap() : elementsToRemove;
36+
}
37+
38+
public ElementRemover(JSONObject elementsToRemove)
39+
{
40+
this.elementsToRemove = elementsToRemove == null ? Collections.<String, Object>emptyMap() : elementsToRemove;
41+
}
42+
43+
public JSONObject remove(JSONObject objectToClean)
44+
{
45+
JSONTraverseAction strategy = new RemoveElementsJsonAction(this.elementsToRemove);
46+
JSONTraverser traversal = new JSONTraverser(strategy);
47+
traversal.traverse(objectToClean);
48+
return (JSONObject) strategy.result();
49+
}
50+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package net.minidev.json.actions;
2+
3+
import net.minidev.json.JSONArray;
4+
import net.minidev.json.JSONObject;
5+
import net.minidev.json.actions.path.DotDelimiter;
6+
import net.minidev.json.actions.path.PathDelimiter;
7+
import net.minidev.json.actions.traverse.JSONTraverser;
8+
import net.minidev.json.actions.traverse.LocatePathsJsonAction;
9+
import net.minidev.json.actions.traverse.JSONTraverseAction;
10+
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.List;
15+
16+
/**
17+
* <b>Searches for paths in a {@link JSONObject} and returns those found.</b>
18+
* <p>
19+
* Traverses the specified {@link JSONObject} searching for nodes whose paths (from the root down) match
20+
* any of the user-specified paths. The paths that match are returned.
21+
* <p>
22+
* A path to locate must be specified in the n-gram format - a list of keys from the root down separated by dots:
23+
* K0[[[[.K1].K2].K3]...]
24+
* <br>
25+
* A key to the right of a dot is a direct child of a key to the left of a dot. Keys with a dot in their name are
26+
* not supported.
27+
* <p>
28+
*
29+
* @author adoneitan@gmail.com
30+
*/
31+
public class PathLocator
32+
{
33+
protected List<String> pathsToFind;
34+
protected PathDelimiter pathDelimiter = new DotDelimiter().withAcceptDelimiterInNodeName(false);
35+
36+
public PathLocator(JSONArray pathsToFind)
37+
{
38+
if (pathsToFind == null || pathsToFind.isEmpty()) {
39+
this.pathsToFind = Collections.emptyList();
40+
}
41+
else
42+
{
43+
this.pathsToFind = new ArrayList<String>();
44+
for (Object s : pathsToFind) {
45+
this.pathsToFind.add((String) s);
46+
}
47+
}
48+
}
49+
50+
public PathLocator(List<String> pathsToFind)
51+
{
52+
this.pathsToFind = pathsToFind == null || pathsToFind.size() == 0 ?
53+
Collections.<String>emptyList() : pathsToFind;
54+
}
55+
56+
public PathLocator(String... pathsToFind)
57+
{
58+
this.pathsToFind = pathsToFind == null || pathsToFind.length == 0 ?
59+
Collections.<String>emptyList() : Arrays.asList(pathsToFind);
60+
}
61+
62+
public PathLocator with(PathDelimiter pathDelimiter) {
63+
this.pathDelimiter = pathDelimiter;
64+
return this;
65+
}
66+
67+
public List<String> locate(JSONObject object)
68+
{
69+
JSONTraverseAction action = new LocatePathsJsonAction(this.pathsToFind, pathDelimiter);
70+
JSONTraverser traversal = new JSONTraverser(action).with(pathDelimiter);
71+
traversal.traverse(object);
72+
return (List<String>) action.result();
73+
}
74+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package net.minidev.json.actions;
2+
3+
import net.minidev.json.JSONArray;
4+
import net.minidev.json.JSONObject;
5+
import net.minidev.json.actions.traverse.JSONTraverser;
6+
import net.minidev.json.actions.traverse.RemovePathsJsonAction;
7+
import net.minidev.json.actions.traverse.JSONTraverseAction;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.Collections;
12+
import java.util.List;
13+
14+
/**
15+
* <b>Removes branches of nodes from a {@link JSONObject} matching the list of user-specified paths.</b>
16+
* <p>
17+
* A path to remove must be specified in the n-gram format - a list of keys from the root down separated by dots:
18+
* K0[[[[.K1].K2].K3]...]
19+
* <br>
20+
* A key to the right of a dot is a direct child of a key to the left of a dot. Keys with a dot in their name are
21+
* not supported.
22+
* <p>
23+
* <b>Usage Example:</b>
24+
* <p>
25+
* To remove the field k1.k2 from the {@link JSONObject} {k1:{k2:v2}, k3:{k4:v4}} use the remover like so:
26+
* <pre>
27+
* PathRemover pr = new PathRemover("k1.k2");
28+
* JSONObject cleanObject = pr.remove(new JSONObject(...));
29+
* </pre>
30+
* The resulting object 'cleanObject' would be {k1:{k3:{k4:v4}}}
31+
* <p>
32+
* See unit tests for more examples
33+
*
34+
* @author adoneitan@gmail.com
35+
*
36+
*/
37+
public class PathRemover
38+
{
39+
protected List<String> pathsToRemove;
40+
41+
public PathRemover(JSONArray pathsToRemove)
42+
{
43+
if (pathsToRemove == null || pathsToRemove.isEmpty()) {
44+
this.pathsToRemove = Collections.emptyList();
45+
}
46+
else
47+
{
48+
this.pathsToRemove = new ArrayList<String>();
49+
for (Object s : pathsToRemove) {
50+
this.pathsToRemove.add((String) s);
51+
}
52+
}
53+
}
54+
55+
public PathRemover(List<String> pathsToRemove)
56+
{
57+
this.pathsToRemove = pathsToRemove == null || pathsToRemove.size() == 0 ?
58+
Collections.<String>emptyList() : pathsToRemove;
59+
}
60+
61+
public PathRemover(String... pathsToRemove)
62+
{
63+
this.pathsToRemove = pathsToRemove == null || pathsToRemove.length == 0 ?
64+
Collections.<String>emptyList() : Arrays.asList(pathsToRemove);
65+
}
66+
67+
public JSONObject remove(JSONObject objectToClean)
68+
{
69+
JSONTraverseAction strategy = new RemovePathsJsonAction(this.pathsToRemove);
70+
JSONTraverser traversal = new JSONTraverser(strategy);
71+
traversal.traverse(objectToClean);
72+
return (JSONObject) strategy.result();
73+
}
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package net.minidev.json.actions;
2+
3+
import net.minidev.json.JSONArray;
4+
import net.minidev.json.JSONObject;
5+
import net.minidev.json.actions.navigate.CopyPathsAction;
6+
import net.minidev.json.actions.navigate.JSONNavigator;
7+
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.LinkedList;
11+
import java.util.List;
12+
13+
/**
14+
* <b>Creates a copy of a {@link JSONObject} consisting only of the nodes on the user-specified paths.</b>
15+
* <p>
16+
* Paths that do not exist in the specified object are ignored silently. Specifying an empty list of paths
17+
* to copy or only non-existent paths will result in an empty object being returned.
18+
* <p>
19+
* A path to copy must be specified in the n-gram format - a list of keys from the root down separated by dots:
20+
* K0[[[[.K1].K2].K3]...]
21+
* <br>
22+
* A key to the right of a dot is a direct child of a key to the left of a dot. Keys with a dot in their name are
23+
* not supported.
24+
* <p>
25+
* <b> Sample usage:</b>
26+
* <p>
27+
* To replicate the branch k1.k2 from {k1:{k2:v2}, k3:{k4:v4}} use the {@link PathReplicator} like so:
28+
* <pre>
29+
* PathReplicator pr = new {@link PathReplicator}("k1.k2")
30+
* JSONObject copiedObject = pr.copy(new JSONObject(...))
31+
* </pre>
32+
* The resulting object 'copiedObject' would be {k1:{k2:v2}}
33+
* <p>
34+
* see unit tests for more examples
35+
*
36+
* @author adoneitan@gmail.com
37+
* @since 15 March 2016.
38+
*/
39+
public class PathReplicator
40+
{
41+
protected List<String> pathsToCopy;
42+
43+
public PathReplicator(JSONArray pathsToCopy)
44+
{
45+
if (pathsToCopy == null || pathsToCopy.isEmpty()) {
46+
this.pathsToCopy = Collections.emptyList();
47+
}
48+
else
49+
{
50+
this.pathsToCopy = new LinkedList<String>();
51+
for (Object s : pathsToCopy) {
52+
this.pathsToCopy.add((String) s);
53+
}
54+
}
55+
}
56+
57+
public PathReplicator(List<String> pathsToCopy)
58+
{
59+
this.pathsToCopy = pathsToCopy == null || pathsToCopy.size() == 0 ?
60+
Collections.<String>emptyList() : pathsToCopy;
61+
}
62+
63+
public PathReplicator(String... pathsToCopy)
64+
{
65+
this.pathsToCopy = pathsToCopy == null || pathsToCopy.length == 0 ?
66+
Collections.<String>emptyList() : new LinkedList<String>(Arrays.asList(pathsToCopy));
67+
}
68+
69+
public JSONObject replicate(JSONObject sourceObj) throws Exception
70+
{
71+
CopyPathsAction s = new CopyPathsAction();
72+
JSONNavigator n = new JSONNavigator(s, pathsToCopy);
73+
n.nav(sourceObj);
74+
return (JSONObject) s.result();
75+
}
76+
}

0 commit comments

Comments
 (0)