juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] jamesbognar closed pull request #35: Juneau 92 adding uon samples and more comments
Date Thu, 13 Dec 2018 18:00:47 GMT
jamesbognar closed pull request #35: Juneau 92 adding uon samples and more comments 
URL: https://github.com/apache/juneau/pull/35
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlComplexExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlComplexExample.java
index ecc57cc88..62c5c05a8 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlComplexExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlComplexExample.java
@@ -36,6 +36,16 @@
     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
+        /**
+         * Produces
+         * <table><tr><td>innerPojo</td><td><table><tr><td>name</td><td>name0</td></tr>
+         * <tr><td>id</td><td>1.0</td></tr></table></td></tr><tr><td>values</td><td><table>
+         * <tr><td>setOne</td><td><table _type="array"><tr><th>name</th><th>id</th></tr>
+         * <tr><td>name1</td><td>1.1</td></tr><tr><td>name2</td><td>1.1</td></tr>
+         * </table></td></tr><tr><td>setTwo</td><td><table
_type="array"><tr><th>name
+         * </th><th>id</th></tr><tr><td>name1</td><td>1.2</td></tr><tr><td>name2</td><td>1.2
+         * </td></tr></table></td></tr></table></td></tr><tr><td>id</td><td>pojo</td></tr></table>
+         */
         HtmlSerializer htmlSerializer = HtmlSerializer.DEFAULT;
         // Get a reference to a parser - converts that flat format back into the POJO
         HtmlParser htmlParser = HtmlParser.DEFAULT;
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlSimpleExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlSimpleExample.java
index 6f8abd10c..f7db499cb 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlSimpleExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/html/HtmlSimpleExample.java
@@ -13,6 +13,7 @@
 package org.apache.juneau.examples.core.html;
 
 import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.html.HtmlDocSerializer;
 import org.apache.juneau.html.HtmlParser;
 import org.apache.juneau.html.HtmlSerializer;
 
@@ -29,6 +30,8 @@
     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
+        // Produces
+        // <table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr></table>
         HtmlSerializer htmlSerializer = HtmlSerializer.DEFAULT;
         // Get a reference to a parser - converts that flat format back into the POJO
         HtmlParser htmlParser = HtmlParser.DEFAULT;
@@ -45,6 +48,15 @@ public static void main(String[] args) throws Exception{
         assert parse.getId().equals(pojo.getId());
         assert parse.getName().equals(pojo.getName());
 
+        /**
+         *  Produces
+         *  <html><head><style></style><script></script></head><body><section><article><div
class="outerdata">
+         *  <div class="data" id="data"><table><tr><td>name</td><td>name</td></tr><tr><td>id</td><td>id</td></tr>
+         *  </table></div></div></article></section></body></html>
+         */
+        String docSerialized = HtmlDocSerializer.DEFAULT.serialize(pojo);
+        System.out.println(docSerialized);
+
         // 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.
     }
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
index 08530490d..15a9e079b 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
@@ -33,6 +33,12 @@
     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
+        /**
+         * Produces
+         * {"innerPojo":{"name":"name0","id":"1.0"},
+         * "values":{"setOne":[{"name":"name1","id":"1.1"},{"name":"name2","id":"1.1"}],
+         * "setTwo":[{"name":"name1","id":"1.2"},{"name":"name2","id":"1.2"}]},"id":"pojo"}
+         */
         JsonSerializer jsonSerializer = JsonSerializer.DEFAULT;
         // Get a reference to a parser - converts that flat format back into the POJO
         JsonParser jsonParser = JsonParser.DEFAULT;
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
index a5bc373e1..1019f6417 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java
@@ -36,12 +36,42 @@
 	public static void main(String[] args) throws Exception {
 		Pojo aPojo = new Pojo("a","</pojo>");
 		// Json Serializers can be configured using properties defined in JsonSerializer
+		/**
+		 * Produces
+		 * {
+		 * 	"name": "</pojo>",
+		 * 	"id": "a"
+		 * }
+		 */
 		String withWhitespace = JsonSerializer.create().ws().build().serialize(aPojo);
 		// the output will be padded with spaces after format characters
 		System.out.println(withWhitespace);
 
+		/**
+		 * Produces
+		 * {"name":"<\/pojo>","id":"a"}
+		 */
 		String escaped = JsonSerializer.create().escapeSolidus().build().serialize(aPojo);
 		// the output will have escaped /
 		System.out.println(escaped);
+
+		/**
+		 * Produces
+		 * {
+		 * 	name: '</pojo>',
+		 *	id: 'a'
+		 * }
+		 */
+		String configurableJson =JsonSerializer
+				.create()  // Create a JsonSerializerBuilder
+				.simple()  // Simple mode
+				.ws()  // Use whitespace
+				.sq()  // Use single quotes
+				.build()
+				.serialize(aPojo);  // Create a JsonSerializer
+
+		System.out.println(configurableJson);
+
+
 	}
 }
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
index d2b8a8dcd..19eca1261 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java
@@ -19,9 +19,13 @@
 
 package org.apache.juneau.examples.core.json;
 
+import org.apache.juneau.dto.atom.Person;
 import org.apache.juneau.examples.core.pojo.Pojo;
 import org.apache.juneau.json.JsonParser;
 import org.apache.juneau.json.JsonSerializer;
+import org.apache.juneau.json.SimpleJsonSerializer;
+
+import java.util.Map;
 
 /**
  * Sample class which shows the simple usage of JsonSerializer and JsonParser.
@@ -37,6 +41,8 @@
 	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
+		// Produces
+		// {"name":"name","id":"id"}
 		JsonSerializer jsonSerializer = JsonSerializer.DEFAULT;
 		// Get a reference to a parser - converts that flat format back into the POJO
 		JsonParser jsonParser = JsonParser.DEFAULT;
@@ -44,7 +50,6 @@ public static void main(String[] args) throws Exception{
 		Pojo pojo = new Pojo("id","name");
 
 		String flat = jsonSerializer.serialize(pojo);
-
 		// Print out the created POJO in JSON format.
 		System.out.println(flat);
 
@@ -53,6 +58,24 @@ public static void main(String[] args) throws Exception{
 		assert parse.getId().equals(pojo.getId());
 		assert parse.getName().equals(pojo.getName());
 
+		// Produces
+		// {name:'name',id:'id'}
+		String simpleJson = SimpleJsonSerializer.DEFAULT.serialize(pojo);
+		System.out.println(simpleJson);
+
+		// Parse a JSON object (creates a generic ObjectMap).
+		String json = "{name:'John Smith',age:21}";
+		Map m1 = jsonParser.parse(json, Map.class);
+
+		// Parse a JSON string.
+		json = "'foobar'";
+		String s2 = jsonParser.parse(json, String.class);
+
+		// Parse a JSON number as a Long or Float.
+		json = "123";
+		Long l3 = jsonParser.parse(json, Long.class);
+		Float f3 = jsonParser.parse(json, Float.class);
+
 		// 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.
 	}
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
index 9090a39f5..781321398 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java
@@ -16,7 +16,6 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-
 package org.apache.juneau.examples.core.rdf;
 
 import org.apache.juneau.examples.core.pojo.Pojo;
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONComplexExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONComplexExample.java
new file mode 100644
index 000000000..8ad8b0917
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONComplexExample.java
@@ -0,0 +1,48 @@
+package org.apache.juneau.examples.core.uon;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.examples.core.pojo.PojoComplex;
+import org.apache.juneau.uon.UonParser;
+import org.apache.juneau.uon.UonSerializer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+public class UONComplexExample {
+    /**
+     * Serializing PojoComplex bean into UON format.
+     *
+     * @param args
+     * @throws Exception
+     */
+    public static void main(String[] args) throws Exception {
+
+        // Fill some data to a PojoComplex bean
+        HashMap<String, List<Pojo>> values = new HashMap<>();
+        ArrayList<Pojo> setOne = new ArrayList<>();
+        setOne.add(new Pojo("1.1", "name1"));
+        setOne.add(new Pojo("1.1", "name2"));
+        ArrayList<Pojo> setTwo = new ArrayList<>();
+        setTwo.add(new Pojo("1.2", "name1"));
+        setTwo.add(new Pojo("1.2", "name2"));
+        values.put("setOne", setOne);
+        values.put("setTwo", setTwo);
+        PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
+
+        // this creates an RDF serializer with the default XML structure
+        /**Produces
+         * (innerPojo=(name=name0,id='1.0'),
+         * values=(setOne=@((name=name1,id='1.1'),(name=name2,id='1.1')),
+         * setTwo=@((name=name1,id='1.2'),(name=name2,id='1.2'))),id=pojo)
+         */
+        UonSerializer uonSerializer = UonSerializer.DEFAULT;
+        // This will show the final output from the bean
+        System.out.println(uonSerializer.serialize(pojoc));
+
+        PojoComplex obj = UonParser.DEFAULT.parse(uonSerializer.serialize(pojoc), PojoComplex.class);
+
+        assert obj.getId().equals(pojoc.getId());
+
+    }
+}
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONExample.java
new file mode 100644
index 000000000..329f64fd5
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/UONExample.java
@@ -0,0 +1,60 @@
+/*
+ *  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.uon;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.parser.ParseException;
+import org.apache.juneau.serializer.SerializeException;
+import org.apache.juneau.uon.UonParser;
+import org.apache.juneau.uon.UonSerializer;
+
+/**
+ *	Sample class which shows the simple usage of UONSerializer.
+ */
+public class UONExample {
+    /**
+     * Serializing SimplePojo bean into UON type
+     * and Deserialize back to Pojo instance type.
+     * @param args
+     * @throws SerializeException
+     * @throws ParseException
+     */
+    public static void main(String[] args) throws SerializeException, ParseException {
+
+        // Fill some data to a Pojo bean
+        Pojo pojo = new Pojo("id","name");
+
+        /**
+         * Produces
+         * (name=name,id=id)
+         */
+        String serial = UonSerializer.DEFAULT.serialize(pojo);
+        System.out.println(serial);
+
+        // Deserialize back to Pojo instance
+        Pojo obj = UonParser.DEFAULT.parse(serial, Pojo.class);
+
+        assert obj.getId().equals(pojo.getId());
+        assert obj.getName().equals(pojo.getName());
+
+        // The object above can be parsed thanks to the @BeanConstructor annotation on PojoComplex
+        // Using this approach, you can keep your POJOs immutable, and still serialize and
deserialize them.
+
+    }
+}
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/package-info.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/package-info.java
new file mode 100755
index 000000000..1594a5c62
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/uon/package-info.java
@@ -0,0 +1,18 @@
+// ***************************************************************************************************************************
+// * 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.         
                                    *
+// ***************************************************************************************************************************
+
+/**
+ * Examples
+ */
+package org.apache.juneau.examples.core.uon;
+
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlConfigurationExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlConfigurationExample.java
index d8716fa4a..3dc2f0d03 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlConfigurationExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlConfigurationExample.java
@@ -34,7 +34,14 @@ public static void main(String[] args) throws Exception {
 
         Pojo aPojo = new Pojo("a","<pojo>");
 
-        // Xml Serializers can be configured using properties defined in XmlSerializer.
+        /**
+         * Xml Serializers can be configured using properties defined in XmlSerializer.
+         * Produces
+         * <object>
+         * <name>&lt;pojo&gt;</name>
+         * <id>a</id>
+         * </object>
+         */
         String withWhitespace = XmlSerializer.create().ws().build().serialize(aPojo);
         // the output will be padded with spaces after format characters.
         System.out.println(withWhitespace);
@@ -42,25 +49,34 @@ public static void main(String[] args) throws Exception {
         HashMap<String, List<Pojo>> values = new HashMap<>();
         PojoComplex pojoc = new PojoComplex("pojo", new Pojo("1.0", "name0"), values);
 
+        //Produces
+        //<object><innerPojo><name>name0</name><id>1.0</id></innerPojo><id>pojo</id></object>
         String mapescaped = XmlSerializer.create().trimEmptyMaps(true).build().serialize(pojoc);
         // the output will have trimmed Empty maps.
         System.out.println(mapescaped);
 
+        //Produces
+        //<object xmlns="http://www.apache.org/2013/Juneau"><name>&lt;pojo&gt;</name><id>a</id></object>
         String nspaceToRoot = XmlSerializer.create().ns().addNamespaceUrisToRoot(true).build().serialize(aPojo);
         // the output will add default name space to the xml document root.
         System.out.println(nspaceToRoot);
 
         Pojo nPojo = new Pojo("a",null);
 
+        //Produces
+        //<object><id>a</id></object>
         String nullescaped = XmlSerializer.create().trimNullProperties(true).build().serialize(nPojo);
         // the output will have trimmed null properties.
         System.out.println(nullescaped);
 
-
+        //Produces
+        //<object xmlns="http://www.pierobon.org/iis/review1.htm.html#one"><name>&lt;pojo&gt;</name><id>a</id></object>
         String dNamsSpace = XmlSerializer.create().enableNamespaces(true).defaultNamespace("http://www.pierobon.org"
+
                 "/iis/review1.htm.html#one").autoDetectNamespaces(true).addNamespaceUrisToRoot(true).build()
                 .serialize(aPojo);
         // the output will have new default namespace added.
         System.out.println(dNamsSpace);
+
+
     }
 }
diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlSimpleExample.java
b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlSimpleExample.java
index 6bb6a993e..2366da78f 100644
--- a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlSimpleExample.java
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlSimpleExample.java
@@ -37,6 +37,12 @@ public static void main(String[] args) throws SerializeException, ParseException
         Pojo pojo = new Pojo("id","name");
 
         // Serialize to human readable XML and print
+        /**
+         * <object>
+         * <name>name</name>
+         * <id>id</id>
+         * </object>
+         */
         String serial = XmlSerializer.DEFAULT_SQ_READABLE.serialize(pojo);
         System.out.println(serial);
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message