You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@juneau.apache.org by GitBox <gi...@apache.org> on 2018/12/08 20:09:53 UTC

[GitHub] jamesbognar closed pull request #28: Adding more examples on core module

jamesbognar closed pull request #28: Adding more examples on core module
URL: https://github.com/apache/juneau/pull/28
 
 
   

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/json/JsonComplexExample.java b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
new file mode 100644
index 000000000..1a4c17e7b
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonComplexExample.java
@@ -0,0 +1,56 @@
+package org.apache.juneau.examples.core.json;
+
+import org.apache.juneau.examples.core.pojo.*;
+import org.apache.juneau.json.JsonParser;
+import org.apache.juneau.json.JsonSerializer;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * TODO
+ */
+public class JsonComplexExample {
+    /**
+     * TODO
+     *
+     * @param args
+     * @throws Exception
+     */
+    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;
+
+        // 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);
+
+        String flat = jsonSerializer.serialize(pojoc);
+
+        // Print out the created POJO in JSON format.
+        System.out.println(flat);
+
+        PojoComplex parse = jsonParser.parse(flat, PojoComplex.class);
+
+        assert parse.getId().equals(pojoc.getId());
+        assert parse.getInnerPojo().getName().equals(pojoc.getInnerPojo().getName());
+        assert parse.getInnerPojo().getId().equals(pojoc.getInnerPojo().getId());
+
+        // 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/RdfComplexExample.java b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfComplexExample.java
new file mode 100644
index 000000000..3cfb0923f
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfComplexExample.java
@@ -0,0 +1,39 @@
+package org.apache.juneau.examples.core.rdf;
+
+import org.apache.juneau.examples.core.pojo.*;
+import org.apache.juneau.jena.RdfSerializer;
+import org.apache.juneau.jena.RdfParser;
+import org.apache.juneau.jena.RdfXmlSerializer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+public class RdfComplexExample {
+
+    /**
+     * TODO
+     *
+     * @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
+        RdfSerializer rdfSerializer = RdfXmlSerializer.DEFAULT;
+        // This will show the final output from the bean
+        System.out.println(rdfSerializer.serialize(pojoc));
+    }
+}
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
new file mode 100644
index 000000000..cb2df87e0
--- /dev/null
+++ b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/xml/XmlSimpleExample.java
@@ -0,0 +1,44 @@
+package org.apache.juneau.examples.core.xml;
+
+import org.apache.juneau.examples.core.pojo.Pojo;
+import org.apache.juneau.parser.ParseException;
+import org.apache.juneau.serializer.SerializeException;
+import org.apache.juneau.xml.XmlParser;
+import org.apache.juneau.xml.XmlSerializer;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+
+/**
+ * TODO
+ */
+public class XmlSimpleExample {
+    /**
+     * TODO
+     *
+     * @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");
+
+        // Serialize to human readable XML and print
+        String serial = XmlSerializer.DEFAULT_SQ_READABLE.serialize(pojo);
+        System.out.println(serial);
+
+        // Deserialize back to PojoComplex instance
+        Pojo obj = XmlParser.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.
+
+    }
+}


 

----------------------------------------------------------------
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