Skip to content

Commit 7714fe1

Browse files
committed
Chore: Add example2
The existing example POM was refactored and a new parent POM was created from the original. A second example was added which uses the new parent POM.
1 parent d31e2b2 commit 7714fe1

File tree

5 files changed

+294
-91
lines changed

5 files changed

+294
-91
lines changed

example/example1/pom.xml

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -20,116 +20,31 @@
2020
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2121
<modelVersion>4.0.0</modelVersion>
2222

23-
<!--
24-
This POM intentionally does not reference the parent POM and its artifacts
25-
will not published on Maven Central. The point of it is to serve as
26-
a standalone example of how to use JSON->URL. If it is of use to anyone
27-
I expect that to be developers browsing the source in git.
28-
-->
29-
<groupId>org.jsonurl</groupId>
23+
<parent>
24+
<groupId>org.jsonurl</groupId>
25+
<artifactId>example-parent</artifactId>
26+
<version>1-SNAPSHOT</version>
27+
</parent>
28+
3029
<artifactId>example1</artifactId>
3130
<version>1.0.0-SNAPSHOT</version>
3231
<packaging>jar</packaging>
3332

34-
<properties>
35-
<jdk.minor.version>8</jdk.minor.version>
36-
<jdk.version>1.${jdk.minor.version}</jdk.version>
37-
<char.encoding>UTF-8</char.encoding>
38-
39-
<project.build.sourceEncoding>${char.encoding}</project.build.sourceEncoding>
40-
<project.build.resourceEncoding>${char.encoding}</project.build.resourceEncoding>
41-
<project.reporting.outputEncoding>${char.encoding}</project.reporting.outputEncoding>
42-
43-
<maven.compiler.source>${jdk.version}</maven.compiler.source>
44-
<maven.compiler.target>${jdk.version}</maven.compiler.target>
45-
<maven.compiler.plugin>3.8.1</maven.compiler.plugin>
46-
<maven.surefire.plugin>2.22.2</maven.surefire.plugin>
47-
<maven.jar.plugin>3.2.0</maven.jar.plugin>
48-
49-
<javax.json.version>1.1.4</javax.json.version>
50-
<jsonurl.version>1.0.0-SNAPSHOT</jsonurl.version>
51-
<junit.version>[5.4,)</junit.version>
52-
53-
<jsonurl.scm.path>jsonurl/jsonurl-java</jsonurl.scm.path>
54-
<jsonurl.scm.base>scm:git:git@github.com:${jsonurl.scm.path}.git</jsonurl.scm.base>
55-
<jsonurl.scm.url>https://github.com/${jsonurl.scm.path}</jsonurl.scm.url>
56-
</properties>
57-
5833
<name>${project.groupId}:${project.artifactId}</name>
5934
<description>
6035
A simple, standalone JSON->URL example.
6136
</description>
62-
<licenses>
63-
<license>
64-
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
65-
<name>Apache-2.0</name>
66-
<distribution>repo</distribution>
67-
</license>
68-
</licenses>
69-
<organization>
70-
<name>jsonurl.org</name>
71-
<url>http://jsonurl.org</url>
72-
</organization>
73-
<developers>
74-
<developer>
75-
<id>dmaccormack</id>
76-
<name>David MacCormack</name>
77-
</developer>
78-
</developers>
79-
<issueManagement>
80-
<url>${jsonurl.scm.url}/issues</url>
81-
<system>GitHub Issues</system>
82-
</issueManagement>
83-
<scm>
84-
<url>${jsonurl.scm.url}/modules</url>
85-
<connection>${jsonurl.scm.base}</connection>
86-
<developerConnection>${jsonurl.scm.base}</developerConnection>
87-
</scm>
8837

8938
<dependencies>
9039
<dependency>
9140
<groupId>org.jsonurl</groupId>
9241
<artifactId>jsonurl-jsr374</artifactId>
9342
<version>${jsonurl.version}</version>
9443
</dependency>
95-
<dependency>
96-
<groupId>javax.json</groupId>
97-
<artifactId>javax.json-api</artifactId>
98-
<version>${javax.json.version}</version>
99-
</dependency>
100-
10144
<dependency>
10245
<groupId>org.glassfish</groupId>
10346
<artifactId>javax.json</artifactId>
10447
<version>${javax.json.version}</version>
105-
<scope>test</scope>
106-
</dependency>
107-
<dependency>
108-
<groupId>org.junit.jupiter</groupId>
109-
<artifactId>junit-jupiter-engine</artifactId>
110-
<version>${junit.version}</version>
111-
<scope>test</scope>
112-
</dependency>
113-
<dependency>
114-
<groupId>org.junit.jupiter</groupId>
115-
<artifactId>junit-jupiter-params</artifactId>
116-
<version>${junit.version}</version>
117-
<scope>test</scope>
11848
</dependency>
11949
</dependencies>
120-
121-
<build>
122-
<plugins>
123-
<plugin>
124-
<groupId>org.apache.maven.plugins</groupId>
125-
<artifactId>maven-compiler-plugin</artifactId>
126-
<version>${maven.compiler.plugin}</version>
127-
</plugin>
128-
<plugin>
129-
<groupId>org.apache.maven.plugins</groupId>
130-
<artifactId>maven-surefire-plugin</artifactId>
131-
<version>${maven.surefire.plugin}</version>
132-
</plugin>
133-
</plugins>
134-
</build>
13550
</project>

example/example2/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Copyright 2019 David MacCormack
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
use this file except in compliance with the License. You may obtain a copy
7+
of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing,
12+
software distributed under the License is distributed on an
13+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations
16+
under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>org.jsonurl</groupId>
25+
<artifactId>example-parent</artifactId>
26+
<version>1-SNAPSHOT</version>
27+
</parent>
28+
29+
<artifactId>example2</artifactId>
30+
<version>1.0.0-SNAPSHOT</version>
31+
<packaging>jar</packaging>
32+
33+
<name>${project.groupId}:${project.artifactId}</name>
34+
<description>
35+
A simple, standalone JSON->URL example.
36+
</description>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.jsonurl</groupId>
41+
<artifactId>jsonurl-jsonorg</artifactId>
42+
<version>${jsonurl.version}</version>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.jsonurl.example2;
2+
3+
import java.io.IOException;
4+
import org.json.JSONArray;
5+
import org.json.JSONObject;
6+
import org.jsonurl.JsonUrlStringBuilder;
7+
import org.jsonurl.jsonorg.JsonUrlParser;
8+
import org.jsonurl.jsonorg.JsonUrlWriter;
9+
10+
public class Main {
11+
12+
/**
13+
* Return an example value.
14+
*/
15+
public static final JSONArray getExampleValue() {
16+
return new JSONArray()
17+
.put(new JSONObject()
18+
.put("id", 1)
19+
.put("name", new JSONObject()
20+
.put("firstName", "Montgomery")
21+
.put("lastName", "Burns")
22+
.put("evil", true)
23+
.put("age", JSONObject.NULL)))
24+
.put(new JSONObject()
25+
.put("id", 2)
26+
.put("name", new JSONObject()
27+
.put("firstName", "Homer")
28+
.put("lastName", "Simpson")
29+
.put("evil", false)
30+
.put("age", 36)
31+
.put("oddKeyName=", false)
32+
.put("oddKeyName&", true)
33+
.put("101.5", 101.5)
34+
.put("1e1", 1e1)
35+
.put("15e+1", 15e+1)
36+
.put("16e-1", 16e-1)
37+
.put("\"text\"", "funny+key")));
38+
}
39+
40+
/**
41+
* Serialize the given JsonArray and return a String.
42+
*/
43+
public static final String getExampleValueText(JSONArray value) throws IOException {
44+
JsonUrlStringBuilder text = new JsonUrlStringBuilder();
45+
JsonUrlWriter.write(text, value);
46+
return text.build();
47+
}
48+
49+
/**
50+
* OS entry point.
51+
*/
52+
public static final void main(String[] args) throws IOException {
53+
54+
//
55+
// get my example value as a POJO
56+
//
57+
JSONArray value = getExampleValue();
58+
59+
//
60+
// turn that into text
61+
//
62+
String jsonUrlText = getExampleValueText(value);
63+
64+
System.out.println(jsonUrlText);
65+
66+
//
67+
// parse the text and build a JsonArray
68+
//
69+
JsonUrlParser p = new JsonUrlParser();
70+
JSONArray parsedValue = p.parseArray(jsonUrlText);
71+
//parsedValue.write(new PrintWriter(System.out));
72+
System.out.println(parsedValue.toString());
73+
}
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.jsonurl.example2;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.IOException;
6+
import org.json.JSONArray;
7+
import org.jsonurl.JsonUrlStringBuilder;
8+
import org.jsonurl.example2.Main;
9+
import org.jsonurl.jsonorg.JsonUrlParser;
10+
import org.jsonurl.jsonorg.JsonUrlWriter;
11+
import org.junit.jupiter.api.Test;
12+
13+
class Example2Test {
14+
15+
@Test
16+
void test() throws IOException {
17+
//
18+
// get my example value as a POJO
19+
//
20+
JSONArray value = Main.getExampleValue();
21+
22+
//
23+
// turn that into JSON-&gt;URL text
24+
//
25+
JsonUrlStringBuilder text = new JsonUrlStringBuilder();
26+
JsonUrlWriter.write(text, value);
27+
String jsonUrlText = text.build();
28+
29+
//
30+
// parse the JSON-&gt;URL text and build a JsonArray
31+
//
32+
JsonUrlParser p = new JsonUrlParser();
33+
JSONArray parsedValue = p.parseArray(jsonUrlText);
34+
35+
//
36+
// I'm testing the string values because JSONArray does not override
37+
// the equals() method so the test fails even though the values are
38+
// semantically equivalent.
39+
//
40+
assertEquals(value.toString(), parsedValue.toString());
41+
}
42+
}

0 commit comments

Comments
 (0)