You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/07/22 13:44:24 UTC

[sling-samples] branch master updated: SLING-10644 - example generating and testing JSON

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-samples.git


The following commit(s) were added to refs/heads/master by this push:
     new 700d6a4  SLING-10644 - example generating and testing JSON
700d6a4 is described below

commit 700d6a42e27e543da20a6b71405fd3431f6307f9
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Jul 22 15:43:34 2021 +0200

    SLING-10644 - example generating and testing JSON
---
 json-generation/README.md                          | 14 ++++
 json-generation/pom.xml                            | 81 ++++++++++++++++++++++
 .../json/generation/JsonGenerationTest.java        | 61 ++++++++++++++++
 3 files changed, 156 insertions(+)

diff --git a/json-generation/README.md b/json-generation/README.md
new file mode 100644
index 0000000..92ebeb2
--- /dev/null
+++ b/json-generation/README.md
@@ -0,0 +1,14 @@
+JSON Generation using JSR 374 APIs
+---
+
+This small sample demonstrates
+[how to generate JSON](./src/test/java/org/apache/sling/samples/json/generation/JsonGenerationTest.java)
+using the JSR-374's
+[JsonGenerator](https://javadoc.io/doc/javax.json/javax.json-api/latest/javax/json/stream/JsonGenerator.html)
+(thanks @enorman for mentioning that option) and how to test 
+the results using [JsonPath](https://github.com/json-path/JsonPath).
+
+Generating JSON in this way looks like a good default method for Sling, as
+the only required dependency is our `org.apache.sling.commons.johnzon` module,
+which embeds those JSR-374 APIs as well as the [Apache Johnzon](https://johnzon.apache.org/) 
+core in a relatively small bundle (150kB as I write this).
diff --git a/json-generation/pom.xml b/json-generation/pom.xml
new file mode 100644
index 0000000..6678220
--- /dev/null
+++ b/json-generation/pom.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+-->
+<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">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.sling</groupId>
+    <artifactId>sling-bundle-parent</artifactId>
+    <version>43</version>
+    <relativePath />
+  </parent>
+
+  <artifactId>org.apache.sling.samples.json.generation</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+
+  <name>Apache Sling JSON generation sample</name>
+  <description>Demonstrates generating JSON using the JSR-374 APIs</description>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+  </properties>
+
+  <build>
+    <plugins>
+        <plugin>
+            <groupId>biz.aQute.bnd</groupId>
+            <artifactId>bnd-baseline-maven-plugin</artifactId>
+            <configuration>
+              <skip>true</skip>
+            </configuration>
+        </plugin> 
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.johnzon</artifactId>
+      <version>1.2.6</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.jayway.jsonpath</groupId>
+      <artifactId>json-path</artifactId>
+      <version>2.6.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+        <groupId>com.jayway.jsonpath</groupId>
+        <artifactId>json-path-assert</artifactId>
+        <version>2.6.0</version>
+        <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter</artifactId>
+      <version>5.4.2</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  
+</project>
\ No newline at end of file
diff --git a/json-generation/src/test/java/org/apache/sling/samples/json/generation/JsonGenerationTest.java b/json-generation/src/test/java/org/apache/sling/samples/json/generation/JsonGenerationTest.java
new file mode 100644
index 0000000..b836323
--- /dev/null
+++ b/json-generation/src/test/java/org/apache/sling/samples/json/generation/JsonGenerationTest.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ package org.apache.sling.samples.json.generation;
+
+import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
+
+import javax.json.Json;
+import javax.json.stream.JsonGenerator;
+
+import org.junit.jupiter.api.Test;
+
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class JsonGenerationTest {
+    @Test
+    public void basicTest() {
+        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        final JsonGenerator generator = Json.createGenerator(bos);
+
+        // Generation with JSR-374
+        generator
+            .writeStartObject()
+                .write("firstName", "John")
+                .write("age", 25)
+                .writeStartObject("address")
+                    .write("streetAddress", "21 2nd Street")
+                .writeEnd()
+                .writeStartArray("phoneNumber")
+                    .writeStartObject().write("type", "home").writeEnd()
+                    .writeStartObject().write("type", "mobile").writeEnd()
+                    .writeStartObject().write("type", "work").writeEnd()
+                .writeEnd()
+            .writeEnd();
+        generator.close();
+
+        // Test with jsonpath
+        final String json = Json.createReader(new StringReader(bos.toString())).readObject().toString();
+        assertThat(json, hasJsonPath("age", equalTo(25)));
+        assertThat(json, hasJsonPath("address.streetAddress", equalTo("21 2nd Street")));
+        assertThat(json, hasJsonPath("phoneNumber[0].type", equalTo("home")));
+        assertThat(json, hasJsonPath("phoneNumber[2].type", equalTo("work")));
+    }
+}