juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject incubator-juneau git commit: Adding more examples to Juneau.
Date Mon, 09 Jan 2017 02:59:44 GMT
Repository: incubator-juneau
Updated Branches:
  refs/heads/more-examples [created] 2c690b641


Adding more examples to Juneau.


Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/2c690b64
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/2c690b64
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/2c690b64

Branch: refs/heads/more-examples
Commit: 2c690b6416740bb03646540dfcd765e23f63ac47
Parents: 615190a
Author: John D. Ament <johndament@apache.org>
Authored: Sun Jan 8 21:59:30 2017 -0500
Committer: John D. Ament <johndament@apache.org>
Committed: Sun Jan 8 21:59:30 2017 -0500

----------------------------------------------------------------------
 juneau-examples/juneau-core-examples/README.md  | 37 +++++++++++++++
 juneau-examples/juneau-core-examples/pom.xml    | 44 ++++++++++++++++++
 .../core/json/JsonConfigurationExample.java     | 44 ++++++++++++++++++
 .../examples/core/json/JsonSimpleExample.java   | 49 ++++++++++++++++++++
 .../apache/juneau/examples/core/pojo/Pojo.java  | 41 ++++++++++++++++
 .../juneau/examples/core/rdf/RdfExample.java    | 33 +++++++++++++
 juneau-examples/pom.xml                         | 37 +++++++++++++++
 pom.xml                                         |  3 +-
 8 files changed, 287 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/README.md
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/README.md b/juneau-examples/juneau-core-examples/README.md
new file mode 100644
index 0000000..b04941b
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/README.md
@@ -0,0 +1,37 @@
+# Juneau Core Examples
+
+Juneau Core Examples provide some insight on how to leverage Juneau within your applications.
+
+Juneau Core is focused on serialization and deserialization, these examples are focused on
how to use
+Juneau to serialize and deserialize your POJOs.
+
+To use any of the examples, you need to add Apache Juneau as a dependency
+
+```xml
+<dependency>
+    <groupId>org.apache.juneau</groupId>
+    <artifactId>juneau-core</artifactId>
+    <version>${juneau.version}</version>
+</dependency>
+```
+
+## JSON Examples
+
+Juneau provides out of the box JSON support, reading and writing JSON structures into Plain
Old Java Objects (POJOs)
+
+- `JsonSimpleExample` - How to use the JsonSerializer and JsonParser to convert POJOs to
String and then strings back to POJOs
+- `JsonConfigurationExample` - How to create JsonParser and Seralizer with different properties
set.
+
+## RDF Examples
+
+Juneau provides RDF support assuming Apache Jena is on the classpath.  First, you need to
add Jena to the classpath.
+
+```xml
+<dependency>
+    <groupId>org.apache.jena</groupId>
+    <artifactId>jena-core</artifactId>
+    <version>${jena.version}</version> // Juneau is tested against 2.7.1
+</dependency>
+```
+
+- `RDFExample` - An example on how to serialize a POJO into RDF format
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/pom.xml
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/pom.xml b/juneau-examples/juneau-core-examples/pom.xml
new file mode 100644
index 0000000..308df8f
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/pom.xml
@@ -0,0 +1,44 @@
+<?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">
+    <parent>
+        <artifactId>juneau-examples</artifactId>
+        <groupId>org.apache.juneau</groupId>
+        <version>6.0.2-incubating-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>juneau-core-examples</artifactId>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.juneau</groupId>
+            <artifactId>juneau-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.jena</groupId>
+            <artifactId>jena-core</artifactId>
+        </dependency>
+    </dependencies>
+
+</project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
new file mode 100644
index 0000000..8adb404
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
@@ -0,0 +1,44 @@
+/*
+ *  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.juneau.examples.core.json;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.json.JsonSerializer;
+import org.apache.juneau.json.JsonSerializerContext;
+
+public class JsonConfigurationExample {
+    public static void main(String[] args) throws Exception {
+        Pojo aPojo = new Pojo("a","</pojo>");
+        // Json Serializers can be configured using properties defined in JsonSerializerContext
+        String withWhitespace = new JsonSerializer()
+                .setProperty(JsonSerializerContext.JSON_useWhitespace, true)
+                .serialize(aPojo);
+        // the output will be padded with spaces after format characters
+        System.out.println(withWhitespace);
+
+        String escaped = new JsonSerializer()
+                .setProperty(JsonSerializerContext.JSON_escapeSolidus, true)
+                .serialize(aPojo);
+        // the output will have escaped /
+        System.out.println(escaped);
+
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
new file mode 100644
index 0000000..31df733
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
@@ -0,0 +1,49 @@
+/*
+ *  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.juneau.examples.core.json;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.json.JsonParser;
+import org.apache.juneau.json.JsonSerializer;
+
+public class JsonSimpleExample {
+    public static void main(String[] args) throws Exception{
+        // Juneau provides static constants with the most commonly used configurations
+        // Get a reference to a serializer - converting POJO to flat format
+        JsonSerializer jsonSerializer = JsonSerializer.DEFAULT;
+        // Get a reference to a parser - converts that flat format back into the POJO
+        JsonParser jsonParser = JsonParser.DEFAULT;
+
+        Pojo pojo = new Pojo("id","name");
+
+        String flat = jsonSerializer.serialize(pojo);
+
+        // Print out the created POJO in JSON format.
+        System.out.println(flat);
+
+        Pojo parse = jsonParser.parse(flat, Pojo.class);
+
+        assert parse.getId().equals(pojo.getId());
+        assert parse.getName().equals(pojo.getName());
+
+        // The object above can be parsed thanks to the @BeanConstructor(properties = id,name)
annotation on Pojo
+        // Using this approach, you can keep your POJOs immutable, and still serialize and
deserialize them.
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java
b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java
new file mode 100644
index 0000000..c400899
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java
@@ -0,0 +1,41 @@
+/*
+ *  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.juneau.examples.core.pojo;
+
+import org.apache.juneau.annotation.BeanConstructor;
+
+public class Pojo {
+    private final String id;
+    private final String name;
+
+    @BeanConstructor(properties = "id,name")
+    public Pojo(String id, String name) {
+        this.id = id;
+        this.name = name;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
new file mode 100644
index 0000000..5b21ecf
--- /dev/null
+++ b/juneau-examples/juneau-core-examples/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
@@ -0,0 +1,33 @@
+/*
+ *  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.juneau.examples.core.rdf;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.jena.RdfSerializer;
+
+public class RdfExample {
+    public static void main(String[] args) throws Exception {
+        Pojo pojo = new Pojo("rdf","This is RDF format.");
+        // this creates an RDF serializer with the default XML structure
+        RdfSerializer rdfSerializer = RdfSerializer.DEFAULT_XML;
+        // This will show the final output from the bean
+        System.out.println(rdfSerializer.serialize(pojo));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/juneau-examples/pom.xml
----------------------------------------------------------------------
diff --git a/juneau-examples/pom.xml b/juneau-examples/pom.xml
new file mode 100644
index 0000000..8a56bfc
--- /dev/null
+++ b/juneau-examples/pom.xml
@@ -0,0 +1,37 @@
+<?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">
+    <parent>
+        <artifactId>juneau</artifactId>
+        <groupId>org.apache.juneau</groupId>
+        <version>6.0.2-incubating-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>juneau-examples</artifactId>
+    <packaging>pom</packaging>
+    <modules>
+        <module>juneau-core-examples</module>
+    </modules>
+
+</project>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/2c690b64/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index ccf54c8..8921374 100644
--- a/pom.xml
+++ b/pom.xml
@@ -99,7 +99,8 @@
 		<module>juneau-server-test</module>
 		<module>juneau-all</module>
 		<module>juneau-distrib</module>
-	</modules>
+        <module>juneau-examples</module>
+    </modules>
 
 	<distributionManagement>
 		<!-- Uncomment to generate Maven site in /tmp/site --> 


Mime
View raw message