From 01625edeae56ad37178e4ed568b77ae55e9333a2 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 23 Aug 2014 10:56:38 +0300 Subject: [PATCH] initial gson project --- gson/.classpath | 36 +++++ ...e.wst.jsdt.core.javascriptValidator.launch | 7 + gson/.gitignore | 13 ++ gson/.project | 36 +++++ gson/.springBeans | 14 ++ gson/README.md | 12 ++ gson/pom.xml | 151 ++++++++++++++++++ gson/src/main/resources/example1.json | 12 ++ gson/src/main/resources/example2.json | 10 ++ gson/src/main/resources/logback.xml | 20 +++ gson/src/main/webapp/WEB-INF/api-servlet.xml | 6 + gson/src/main/webapp/WEB-INF/web.xml | 42 +++++ .../deserialization/GenericSourceClass.java | 10 ++ .../test/GsonDeserializationUnitTest.java | 33 ++++ gson/src/test/resources/.gitignore | 13 ++ 15 files changed, 415 insertions(+) create mode 100644 gson/.classpath create mode 100644 gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch create mode 100644 gson/.gitignore create mode 100644 gson/.project create mode 100644 gson/.springBeans create mode 100644 gson/README.md create mode 100644 gson/pom.xml create mode 100644 gson/src/main/resources/example1.json create mode 100644 gson/src/main/resources/example2.json create mode 100644 gson/src/main/resources/logback.xml create mode 100644 gson/src/main/webapp/WEB-INF/api-servlet.xml create mode 100644 gson/src/main/webapp/WEB-INF/web.xml create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java create mode 100644 gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java create mode 100644 gson/src/test/resources/.gitignore diff --git a/gson/.classpath b/gson/.classpath new file mode 100644 index 0000000000..0720e4851b --- /dev/null +++ b/gson/.classpath @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch new file mode 100644 index 0000000000..627021fb96 --- /dev/null +++ b/gson/.externalToolBuilders/org.eclipse.wst.jsdt.core.javascriptValidator.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/gson/.gitignore b/gson/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/gson/.project b/gson/.project new file mode 100644 index 0000000000..ea8ae1a67f --- /dev/null +++ b/gson/.project @@ -0,0 +1,36 @@ + + + jackson + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/gson/.springBeans b/gson/.springBeans new file mode 100644 index 0000000000..a79097f40d --- /dev/null +++ b/gson/.springBeans @@ -0,0 +1,14 @@ + + + 1 + + + + + + + src/main/webapp/WEB-INF/api-servlet.xml + + + + diff --git a/gson/README.md b/gson/README.md new file mode 100644 index 0000000000..539cb34761 --- /dev/null +++ b/gson/README.md @@ -0,0 +1,12 @@ +========= + +## Jackson Cookbooks and Examples + +### Relevant Articles: +- [Jackson Ignore Properties on Marshalling](http://www.baeldung.com/jackson-ignore-properties-on-serialization) +- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array) +- [Jackson Unmarshalling json with Unknown Properties](http://www.baeldung.com/jackson-deserialize-json-unknown-properties) +- [Jackson – Custom Serializer](http://www.baeldung.com/jackson-custom-serialization) +- [Jackson – Custom Deserializer](http://www.baeldung.com/jackson-deserialization) + + diff --git a/gson/pom.xml b/gson/pom.xml new file mode 100644 index 0000000000..2a28f4fb36 --- /dev/null +++ b/gson/pom.xml @@ -0,0 +1,151 @@ + + 4.0.0 + org.baeldung + gson + 0.1-SNAPSHOT + + gson + + + + + + + com.google.guava + guava + 17.0 + + + commons-io + commons-io + 2.4 + + + + org.apache.commons + commons-collections4 + 4.0 + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + + + com.google.code.gson + gson + 2.3 + + + + + + junit + junit-dep + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + jackson + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.7 + 1.7 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + 4.0.6.RELEASE + 3.2.4.RELEASE + + + 4.3.6.Final + 5.1.31 + + + 2.4.1 + + + 1.7.7 + 1.1.2 + + + 5.1.2.Final + + + 17.0 + 3.3.2 + + + 1.3 + 4.11 + 1.9.5 + + 4.3.2 + 4.3.5 + + 2.3.2 + + + 3.1 + 2.4 + 2.17 + 2.6 + 1.4.8 + + + + \ No newline at end of file diff --git a/gson/src/main/resources/example1.json b/gson/src/main/resources/example1.json new file mode 100644 index 0000000000..46d2982cec --- /dev/null +++ b/gson/src/main/resources/example1.json @@ -0,0 +1,12 @@ +{ + "collection": [ + { + "name": "Test order1", + "detail": "ahk ks" + }, + { + "name": "Test order2", + "detail": "Fisteku" + } + ] +} \ No newline at end of file diff --git a/gson/src/main/resources/example2.json b/gson/src/main/resources/example2.json new file mode 100644 index 0000000000..f4433731e6 --- /dev/null +++ b/gson/src/main/resources/example2.json @@ -0,0 +1,10 @@ +[ + { + "name": "Test order1", + "detail": "ahk ks" + }, + { + "name": "Test order2", + "detail": "Fisteku" + } +] \ No newline at end of file diff --git a/gson/src/main/resources/logback.xml b/gson/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/gson/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/api-servlet.xml b/gson/src/main/webapp/WEB-INF/api-servlet.xml new file mode 100644 index 0000000000..a675fc6d95 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/api-servlet.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/gson/src/main/webapp/WEB-INF/web.xml b/gson/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..48d4b8fe61 --- /dev/null +++ b/gson/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,42 @@ + + + + Spring MVC Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.config + + + + org.springframework.web.context.ContextLoaderListener + + + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + + + + + + + \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java b/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java new file mode 100644 index 0000000000..590203e622 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/GenericSourceClass.java @@ -0,0 +1,10 @@ +package org.baeldung.gson.deserialization; + +public class GenericSourceClass { + int intField; + + public GenericSourceClass(final int i) { + intField = i; + } + +} \ No newline at end of file diff --git a/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java new file mode 100644 index 0000000000..583aa16ff7 --- /dev/null +++ b/gson/src/test/java/org/baeldung/gson/deserialization/test/GsonDeserializationUnitTest.java @@ -0,0 +1,33 @@ +package org.baeldung.gson.deserialization.test; + +import org.baeldung.gson.deserialization.GenericSourceClass; +import org.junit.Before; +import org.junit.Test; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class GsonDeserializationUnitTest { + + private Gson gson; + + @Before + public final void before() { + gson = new Gson(); + } + + // tests + + @Test + public void givenUsingGson_whenDeserializingGeneric_thenCorrect() { + final java.lang.reflect.Type genericSourceClassType = new TypeToken() { + }.getType(); + final GenericSourceClass sourceObject = new GenericSourceClass(1); + final String serializedSourceObject = gson.toJson(sourceObject, genericSourceClassType); + + final GenericSourceClass targetObject = gson.fromJson(serializedSourceObject, genericSourceClassType); + + System.out.println(targetObject); + } + +} diff --git a/gson/src/test/resources/.gitignore b/gson/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/gson/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file