Skip to content

Commit 4f3a069

Browse files
committed
Commit example1
Added example/example1 to serve as a simple, standalone example of using the JSR-374 JSON->URL binding.
1 parent 0136c0c commit 4f3a069

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

example/example1/pom.xml

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
<!--
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>
30+
<artifactId>example1</artifactId>
31+
<version>1.0.0-SNAPSHOT</version>
32+
<packaging>jar</packaging>
33+
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.jar.plugin>3.2.0</maven.jar.plugin>
47+
48+
<javax.json.version>1.1.4</javax.json.version>
49+
<jsonurl.version>1.0.0-SNAPSHOT</jsonurl.version>
50+
51+
<jsonurl.scm.path>jsonurl/jsonurl-java</jsonurl.scm.path>
52+
<jsonurl.scm.base>scm:git:git@github.com:${jsonurl.scm.path}.git</jsonurl.scm.base>
53+
<jsonurl.scm.url>https://github.com/${jsonurl.scm.path}</jsonurl.scm.url>
54+
</properties>
55+
56+
<name>${project.groupId}:${project.artifactId}</name>
57+
<description>
58+
A simple, standalone JSON->URL example.
59+
</description>
60+
<licenses>
61+
<license>
62+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
63+
<name>Apache-2.0</name>
64+
<distribution>repo</distribution>
65+
</license>
66+
</licenses>
67+
<organization>
68+
<name>jsonurl.org</name>
69+
<url>http://jsonurl.org</url>
70+
</organization>
71+
<developers>
72+
<developer>
73+
<id>dmaccormack</id>
74+
<name>David MacCormack</name>
75+
</developer>
76+
</developers>
77+
<issueManagement>
78+
<url>${jsonurl.scm.url}/issues</url>
79+
<system>GitHub Issues</system>
80+
</issueManagement>
81+
<scm>
82+
<url>${jsonurl.scm.url}/modules</url>
83+
<connection>${jsonurl.scm.base}</connection>
84+
<developerConnection>${jsonurl.scm.base}</developerConnection>
85+
</scm>
86+
87+
<dependencies>
88+
<dependency>
89+
<groupId>org.jsonurl</groupId>
90+
<artifactId>jsonurl-jsr374</artifactId>
91+
<version>${jsonurl.version}</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>javax.json</groupId>
95+
<artifactId>javax.json-api</artifactId>
96+
<version>${javax.json.version}</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.glassfish</groupId>
100+
<artifactId>javax.json</artifactId>
101+
<version>${javax.json.version}</version>
102+
<scope>test</scope>
103+
</dependency>
104+
</dependencies>
105+
106+
<build>
107+
<plugins>
108+
<plugin>
109+
<groupId>org.apache.maven.plugins</groupId>
110+
<artifactId>maven-compiler-plugin</artifactId>
111+
<version>${maven.compiler.plugin}</version>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.jsonurl.example1;
2+
3+
import java.io.IOException;
4+
import java.util.Collections;
5+
import javax.json.Json;
6+
import javax.json.JsonArray;
7+
import javax.json.JsonBuilderFactory;
8+
import javax.json.JsonException;
9+
import javax.json.JsonWriter;
10+
import org.jsonurl.JsonUrlStringBuilder;
11+
import org.jsonurl.jsonp.JsonUrlParser;
12+
import org.jsonurl.jsonp.JsonUrlWriter;
13+
14+
public class Main {
15+
16+
/**
17+
* OS entry point.
18+
*/
19+
public static final void main(String[] args)
20+
throws JsonException, IOException {
21+
22+
JsonBuilderFactory jbf = Json.createBuilderFactory(
23+
Collections.emptyMap());
24+
25+
JsonArray jo = jbf.createArrayBuilder()
26+
.add(jbf.createObjectBuilder()
27+
.add("id", 1)
28+
.add("name", jbf.createObjectBuilder()
29+
.add("firstName", "Montgomery")
30+
.add("lastName", "Burns")))
31+
.add(jbf.createObjectBuilder()
32+
.add("id", 2)
33+
.add("name", jbf.createObjectBuilder()
34+
.add("firstName", "Homer")
35+
.add("lastName", "Simpson")))
36+
.build();
37+
38+
JsonUrlStringBuilder text = new JsonUrlStringBuilder();
39+
40+
JsonUrlWriter.write(text, jo);
41+
String jsonUrl = text.build();
42+
43+
System.out.println(jsonUrl);
44+
45+
JsonUrlParser p = new JsonUrlParser();
46+
JsonWriter jout = Json.createWriter(System.out);
47+
jout.write(p.parse(jsonUrl));
48+
}
49+
}

0 commit comments

Comments
 (0)