Skip to content

Commit d44241c

Browse files
authored
Merge pull request #31 from netplex/erav
merge Erav as json-smart-action:2.3
2 parents cb03bcb + 1a11a47 commit d44241c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3150
-289
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

accessors-smart/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<id>uriel</id>
1919
<name>Uriel Chemouni</name>
2020
<email>uchemouni@gmail.com</email>
21-
<timezone>GMT+1</timezone>
21+
<timezone>GMT-7</timezone>
2222
<roles>
2323
</roles>
2424
</developer>

json-smart-action/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

json-smart-action/pom.xml

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<!-- this POM is released under an Apache 2.0 license -->
3+
<parent>
4+
<groupId>net.minidev</groupId>
5+
<artifactId>minidev-parent</artifactId>
6+
<version>2.3-SNAPSHOT</version>
7+
</parent>
8+
<modelVersion>4.0.0</modelVersion>
9+
<artifactId>json-smart-action</artifactId>
10+
<name>JSON Small and Fast Parser</name>
11+
<description>
12+
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.
13+
</description>
14+
<packaging>bundle</packaging>
15+
<url>http://www.minidev.net/</url>
16+
<organization>
17+
<name>Chemouni Uriel</name>
18+
<url>http://www.minidev.net/</url>
19+
</organization>
20+
<developers>
21+
<developer>
22+
<id>uriel</id>
23+
<name>Uriel Chemouni</name>
24+
<email>uchemouni@gmail.com</email>
25+
<timezone>GMT-7</timezone>
26+
<roles>
27+
</roles>
28+
</developer>
29+
<developer>
30+
<id>erav</id>
31+
<name>Eitan Raviv</name>
32+
<email>adoneitan@gmail.com</email>
33+
<timezone>GMT+2</timezone>
34+
</developer>
35+
</developers>
36+
<licenses>
37+
<license>
38+
<name>The Apache Software License, Version 2.0</name>
39+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
40+
<distribution>repo</distribution>
41+
<comments>All files under Apache 2</comments>
42+
</license>
43+
</licenses>
44+
<properties>
45+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46+
<maven.compiler.source>1.5</maven.compiler.source>
47+
<maven.compiler.target>1.5</maven.compiler.target>
48+
</properties>
49+
<dependencies>
50+
<dependency>
51+
<groupId>junit</groupId>
52+
<artifactId>junit</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>net.minidev</groupId>
57+
<artifactId>json-smart</artifactId>
58+
</dependency>
59+
</dependencies>
60+
<scm>
61+
<!-- ON GIT HUB -->
62+
<connection>scm:git:https://github.com/netplex/json-smart-v2.git</connection>
63+
<developerConnection>scm:git:https://github.com/netplex/json-smart-v2.git</developerConnection>
64+
<url>https://github.com/netplex/json-smart-v2</url>
65+
</scm>
66+
<distributionManagement>
67+
<snapshotRepository>
68+
<id>ossrh</id>
69+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
70+
</snapshotRepository>
71+
<repository>
72+
<id>ossrh</id>
73+
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
74+
</repository>
75+
</distributionManagement>
76+
<profiles>
77+
<profile>
78+
<id>release-sign-artifacts</id>
79+
<activation>
80+
<property>
81+
<!-- will be set by the release plugin upon performing mvn release:perform -->
82+
<name>performRelease</name>
83+
<value>true</value>
84+
</property>
85+
</activation>
86+
<properties>
87+
<!-- My old Is back -->
88+
<gpg.keyname>2C8DF6EC</gpg.keyname>
89+
<!-- <gpg.keyname>8E322ED0</gpg.keyname> -->
90+
<!-- <gpg.keyname>Uriel Chemouni (dev) <uchemouni@gmail.com></gpg.keyname> -->
91+
<!-- GPG Key ID to use for signing -->
92+
</properties>
93+
<build>
94+
<plugins>
95+
<!-- Enable signing of the artifacts For gpg:sign-and-deploy-file it's
96+
necessary to have a <server> with the repositoryId provided or id="remote-repository"
97+
defined in settings.xml (it contains the repository's login, psw) Signing:
98+
mvn gpg:sign-and-deploy-file -DpomFile=target/myapp-1.0.pom -Dfile=target/myapp-1.0.jar
99+
-Durl=http://oss.sonatype.org/content/repositories/malyvelky/ -DrepositoryId=sonatype_oss
100+
Note normally it uses the defaul key but we can ovveride it by either setting
101+
the property gpg.keyname (done in this POM) or by providing -Dkeyname=66AE163A
102+
on the command line. OR directly w/ gpg (remove space in - -): gpg -u 66AE163A
103+
- -sign - -detach-sign -a target/dbunit-embeddedderby-parenttest.jar Note:
104+
"mvn gpg:sign" results in NPE with v 1.o-a.-4, use "mvn package gpg:sign"
105+
instead; see the issue MGPG-18 -->
106+
<plugin>
107+
<!-- updated on 29/07/2015 -->
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-gpg-plugin</artifactId>
110+
<version>1.6</version>
111+
<executions>
112+
<execution>
113+
<id>sign-artifacts</id>
114+
<phase>verify</phase>
115+
<goals>
116+
<goal>sign</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<!-- Publish also javadocs when releasing - required by Sonatype -->
122+
<plugin>
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-javadoc-plugin</artifactId>
125+
<executions>
126+
<execution>
127+
<id>attach-javadocs</id>
128+
<goals>
129+
<goal>jar</goal>
130+
</goals>
131+
</execution>
132+
</executions>
133+
</plugin>
134+
<!-- Release Plugin (Update version in POM before/after release, create
135+
tag, deploy) to try: mvn release:prepare -DdryRun=true && mvn release:clean
136+
to perform: mvn release:prepare release:perform Read http://nexus.sonatype.org/oss-repository-hosting.html#3
137+
for instructions on releasing to this project's Sonatype repository -->
138+
<plugin>
139+
<groupId>org.apache.maven.plugins</groupId>
140+
<artifactId>maven-release-plugin</artifactId>
141+
<version>2.5.2</version>
142+
<configuration>
143+
<mavenExecutorId>forked-path</mavenExecutorId>
144+
<arguments>-Psonatype-oss-release</arguments>
145+
<autoVersionSubmodules>false</autoVersionSubmodules>
146+
<useReleaseProfile>false</useReleaseProfile>
147+
<releaseProfiles>release</releaseProfiles>
148+
<goals>deploy</goals>
149+
</configuration>
150+
</plugin>
151+
</plugins>
152+
</build>
153+
</profile>
154+
<profile>
155+
<id>include-sources</id>
156+
<build>
157+
<resources>
158+
<resource>
159+
<targetPath>/</targetPath>
160+
<filtering>true</filtering>
161+
<directory>src/main/java</directory>
162+
<includes>
163+
<include>**/*.java</include>
164+
</includes>
165+
</resource>
166+
</resources>
167+
</build>
168+
</profile>
169+
</profiles>
170+
<build>
171+
<plugins>
172+
<plugin>
173+
<!-- updated on 29/07/2015 -->
174+
<groupId>org.apache.maven.plugins</groupId>
175+
<artifactId>maven-source-plugin</artifactId>
176+
<version>2.4</version>
177+
<executions>
178+
<execution>
179+
<id>bind-sources</id>
180+
<goals>
181+
<goal>jar-no-fork</goal>
182+
</goals>
183+
</execution>
184+
</executions>
185+
</plugin>
186+
<plugin>
187+
<!-- updated on 29/10/2015 -->
188+
<groupId>org.apache.maven.plugins</groupId>
189+
<artifactId>maven-compiler-plugin</artifactId>
190+
<version>3.3</version>
191+
<configuration>
192+
<encoding>UTF-8</encoding>
193+
<source>1.6</source>
194+
<target>1.6</target>
195+
<excludes>
196+
<exclude>**/.svn/*</exclude>
197+
<exclude>**/.svn</exclude>
198+
</excludes>
199+
</configuration>
200+
</plugin>
201+
<plugin>
202+
<!-- updated on 29/10/2015 -->
203+
<groupId>org.apache.maven.plugins</groupId>
204+
<artifactId>maven-resources-plugin</artifactId>
205+
<version>2.7</version>
206+
<configuration>
207+
<encoding>UTF-8</encoding>
208+
</configuration>
209+
</plugin>
210+
<plugin>
211+
<!-- updated on 29/10/2015 -->
212+
<groupId>org.apache.maven.plugins</groupId>
213+
<artifactId>maven-jar-plugin</artifactId>
214+
<version>2.6</version>
215+
<configuration>
216+
<excludes>
217+
<exclude>**/.svn/*</exclude>
218+
<exclude>**/.svn</exclude>
219+
</excludes>
220+
</configuration>
221+
</plugin>
222+
<plugin>
223+
<!-- updated on 29/07/2015 -->
224+
<groupId>org.apache.maven.plugins</groupId>
225+
<artifactId>maven-javadoc-plugin</artifactId>
226+
<version>2.10.3</version>
227+
<!-- ONLY NEEDED With jdk 1.7+ -->
228+
<configuration>
229+
<failOnError>false</failOnError>
230+
<!-- <additionalparam>-Xdoclint:none</additionalparam> -->
231+
</configuration>
232+
<executions>
233+
<execution>
234+
<id>attach-javadocs</id>
235+
<goals>
236+
<goal>jar</goal>
237+
</goals>
238+
</execution>
239+
</executions>
240+
</plugin>
241+
<plugin>
242+
<groupId>org.apache.felix</groupId>
243+
<artifactId>maven-bundle-plugin</artifactId>
244+
<version>3.0.0</version>
245+
<extensions>true</extensions>
246+
<configuration>
247+
<instructions>
248+
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
249+
<Bundle-Name>${project.artifactId}</Bundle-Name>
250+
<Bundle-Version>${project.version}</Bundle-Version>
251+
<Export-Package>
252+
net.minidev.json, net.minidev.json.annotate, net.minidev.json.parser, net.minidev.json.reader, net.minidev.json.writer
253+
</Export-Package>
254+
</instructions>
255+
</configuration>
256+
</plugin>
257+
</plugins>
258+
</build>
259+
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
private Map<String, Object> elementsToRemove;
31+
32+
public ElementRemover(Map<String, Object> elementsToRemove) {
33+
this.elementsToRemove = elementsToRemove == null ? Collections.<String, Object> emptyMap() : elementsToRemove;
34+
}
35+
36+
public ElementRemover(JSONObject elementsToRemove) {
37+
this.elementsToRemove = elementsToRemove == null ? Collections.<String, Object> emptyMap() : elementsToRemove;
38+
}
39+
40+
public JSONObject remove(JSONObject objectToClean) {
41+
JSONTraverseAction strategy = new RemoveElementsJsonAction(this.elementsToRemove);
42+
JSONTraverser traversal = new JSONTraverser(strategy);
43+
traversal.traverse(objectToClean);
44+
return (JSONObject) strategy.result();
45+
}
46+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
protected List<String> pathsToFind;
33+
protected PathDelimiter pathDelimiter = new DotDelimiter().withAcceptDelimiterInNodeName(false);
34+
35+
public PathLocator(JSONArray pathsToFind) {
36+
if (pathsToFind == null || pathsToFind.isEmpty()) {
37+
this.pathsToFind = Collections.emptyList();
38+
} else {
39+
this.pathsToFind = new ArrayList<String>();
40+
for (Object s : pathsToFind) {
41+
this.pathsToFind.add((String) s);
42+
}
43+
}
44+
}
45+
46+
public PathLocator(List<String> pathsToFind) {
47+
this.pathsToFind = pathsToFind == null || pathsToFind.size() == 0 ? Collections.<String> emptyList() : pathsToFind;
48+
}
49+
50+
public PathLocator(String... pathsToFind) {
51+
this.pathsToFind = pathsToFind == null || pathsToFind.length == 0 ? Collections.<String> emptyList() : Arrays.asList(pathsToFind);
52+
}
53+
54+
public PathLocator with(PathDelimiter pathDelimiter) {
55+
this.pathDelimiter = pathDelimiter;
56+
return this;
57+
}
58+
59+
public List<String> locate(JSONObject object) {
60+
JSONTraverseAction action = new LocatePathsJsonAction(this.pathsToFind, pathDelimiter);
61+
JSONTraverser traversal = new JSONTraverser(action).with(pathDelimiter);
62+
traversal.traverse(object);
63+
return (List<String>) action.result();
64+
}
65+
}

0 commit comments

Comments
 (0)