Repository: incubator-juneau Updated Branches: refs/heads/master e3d952842 -> 5674cb907
JUNEAU-36 Adding core 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/5674cb90 Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/5674cb90 Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/5674cb90 Branch: refs/heads/master Commit: 5674cb907a1853182cbaa16b233893817a4cf3db Parents: e3d9528 Author: John D. Ament <[email protected]> Authored: Sun Jan 8 21:59:30 2017 -0500 Committer: John D. Ament <[email protected]> Committed: Wed Jan 25 21:56:20 2017 -0500 ---------------------------------------------------------------------- juneau-examples/juneau-examples-core/README.md | 50 +++++++++++++++++++ juneau-examples/juneau-examples-core/pom.xml | 51 ++++++++++++++++++++ .../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 | 39 +++++++++++++++ pom.xml | 5 +- 8 files changed, 311 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/5674cb90/juneau-examples/juneau-examples-core/README.md ---------------------------------------------------------------------- diff --git a/juneau-examples/juneau-examples-core/README.md b/juneau-examples/juneau-examples-core/README.md new file mode 100644 index 0000000..27b5799 --- /dev/null +++ b/juneau-examples/juneau-examples-core/README.md @@ -0,0 +1,50 @@ +// *************************************************************************************************************************** +// * 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. * +// *************************************************************************************************************************** + +# 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/5674cb90/juneau-examples/juneau-examples-core/pom.xml ---------------------------------------------------------------------- diff --git a/juneau-examples/juneau-examples-core/pom.xml b/juneau-examples/juneau-examples-core/pom.xml new file mode 100644 index 0000000..d868aba --- /dev/null +++ b/juneau-examples/juneau-examples-core/pom.xml @@ -0,0 +1,51 @@ +<?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> + <name>Apache Juneau - Examples - Core</name> + <description>Apache Juneau Examples for Core parser functionality</description> + <dependencies> + <dependency> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.juneau</groupId> + <artifactId>juneau-core-rdf</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/5674cb90/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonConfigurationExample.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..8adb404 --- /dev/null +++ b/juneau-examples/juneau-examples-core/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/5674cb90/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/json/JsonSimpleExample.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..31df733 --- /dev/null +++ b/juneau-examples/juneau-examples-core/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/5674cb90/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java ---------------------------------------------------------------------- diff --git a/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java b/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/pojo/Pojo.java new file mode 100644 index 0000000..c400899 --- /dev/null +++ b/juneau-examples/juneau-examples-core/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/5674cb90/juneau-examples/juneau-examples-core/src/main/java/org/apache/juneau/examples/core/rdf/RdfExample.java ---------------------------------------------------------------------- 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 new file mode 100644 index 0000000..5b21ecf --- /dev/null +++ b/juneau-examples/juneau-examples-core/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/5674cb90/juneau-examples/pom.xml ---------------------------------------------------------------------- diff --git a/juneau-examples/pom.xml b/juneau-examples/pom.xml new file mode 100644 index 0000000..a12710d --- /dev/null +++ b/juneau-examples/pom.xml @@ -0,0 +1,39 @@ +<?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> + <name>Apache Juneau - Examples</name> + <description>Apache Juneau Examples</description> + <modules> + <module>juneau-examples-core</module> + </modules> + +</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/5674cb90/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 09cb854..bd6bd12 100644 --- a/pom.xml +++ b/pom.xml @@ -101,8 +101,9 @@ <module>juneau-microservice</module> <module>juneau-examples-rest</module> <module>juneau-all</module> + <module>juneau-examples</module> <module>juneau-distrib</module> - </modules> + </modules> <distributionManagement> <!-- Uncomment to generate Maven site in /tmp/site --> @@ -123,6 +124,8 @@ <configuration> <excludes> <exclude>**/*.log</exclude> + <exclude>**/.idea</exclude> + <exclude>**/*.iml</exclude> <exclude>**/*.classpath</exclude> <exclude>**/*.log.*</exclude> <exclude>**/target/**</exclude>
