Skip to content

Commit

Permalink
add Copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
twest72 committed Mar 7, 2012
1 parent f51f28c commit d3c1cad
Show file tree
Hide file tree
Showing 11 changed files with 630 additions and 339 deletions.
28 changes: 28 additions & 0 deletions build.gradle
@@ -1,4 +1,32 @@

/*
* Copyright (c) 2011, Thomas Westphal
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'eclipse'
Expand Down
9 changes: 8 additions & 1 deletion gson.ipr
Expand Up @@ -15,7 +15,14 @@
</wildcardResourcePatterns>
<annotationProcessing enabled="false" useClasspath="true" />
</component>
<component name="CopyrightManager" default="">
<component name="CopyrightManager" default="BSD">
<copyright>
<option name="notice" value="Copyright (c) 2011, Thomas Westphal&#10;All rights reserved.&#10;&#10;Redistribution and use in source and binary forms, with or without&#10;modification, are permitted provided that the following conditions are&#10;met:&#10;&#10;- Redistributions of source code must retain the above copyright notice,&#10; this list of conditions and the following disclaimer.&#10;&#10;- Redistributions in binary form must reproduce the above copyright&#10; notice, this list of conditions and the following disclaimer in the&#10; documentation and/or other materials provided with the distribution.&#10;&#10;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS&#10;&quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED&#10;TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&#10;PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR&#10;CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,&#10;EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,&#10;PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR&#10;PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF&#10;LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING&#10;NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS&#10;SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." />
<option name="keyword" value="Copyright" />
<option name="allowReplaceKeyword" value="" />
<option name="myName" value="BSD" />
<option name="myLocal" value="true" />
</copyright>
<module2copyright />
</component>
<component name="DependencyValidationManager">
Expand Down
214 changes: 123 additions & 91 deletions src/main/groovy/de/aonnet/json/JsonConverter.groovy
@@ -1,91 +1,123 @@
package de.aonnet.json

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import java.lang.reflect.Field

class JsonConverter {

private final static List<Class> SIMPLE_TYPES = [String.class, Number.class, Boolean.class, Character.class]

private final static List<String> EXCLUDE_FIELD_NAMES = ['class', 'metaClass']

static String toJsonString(def bean) {

return createJsonBuilder(bean).toString()
}

static Map toJsonMap(def bean) {

return createJsonBuilder(bean).content
}

private static JsonBuilder createJsonBuilder(def bean) {

JsonBuilder builder = new JsonBuilder()
builder {
// the first pair is the bean (key -> classname, values -> bean properties)
"${ bean.class.getName() }" buildProperties(bean)
}

return builder
}

private static Map buildProperties(def bean) {

Map properties = [:]
List excludeFieldNames = getExcludeFieldNames(bean)

bean.properties.each {propName, propValue ->

if (!excludeFieldNames.contains(propName)) {

if (propValue == null || SIMPLE_TYPES.contains(propValue.getClass())) {
properties.put propName, propValue
} else {
properties.put propName, buildProperties(propValue)
}
}
}

return properties
}

private static List getExcludeFieldNames(def bean) {

List excludeFieldNames = []
excludeFieldNames.addAll EXCLUDE_FIELD_NAMES

bean.getClass().declaredFields.each { Field field ->

if (!excludeFieldNames.contains(field.name) && field.getAnnotation(JsonExclude) != null) {
excludeFieldNames << field.name
}
}

return excludeFieldNames
}

static def newInstanceFromJsonString(String json) {

Map beanData = new JsonSlurper().parseText(json)
return newInstanceFromJsonMap(beanData)
}

static def newInstanceFromJsonMap(Map beanData) {

String beanClassName
Map beanProperties

beanData.eachWithIndex { key, value, index ->

// the first pair is the bean (key -> classname, values -> bean properties)
if (index == 0) {
beanClassName = key
beanProperties = value
}
}

return Class.forName(beanClassName).newInstance(beanProperties)
}
}
/*
* Copyright (c) 2011, Thomas Westphal
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package de.aonnet.json

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import java.lang.reflect.Field

class JsonConverter {

private final static List<Class> SIMPLE_TYPES = [String.class, Number.class, Boolean.class, Character.class]

private final static List<String> EXCLUDE_FIELD_NAMES = ['class', 'metaClass']

static String toJsonString(def bean) {

return createJsonBuilder(bean).toString()
}

static Map toJsonMap(def bean) {

return createJsonBuilder(bean).content
}

private static JsonBuilder createJsonBuilder(def bean) {

JsonBuilder builder = new JsonBuilder()
builder {
// the first pair is the bean (key -> classname, values -> bean properties)
"${ bean.class.getName() }" buildProperties(bean)
}

return builder
}

private static Map buildProperties(def bean) {

Map properties = [:]
List excludeFieldNames = getExcludeFieldNames(bean)

bean.properties.each {propName, propValue ->

if (!excludeFieldNames.contains(propName)) {

if (propValue == null || SIMPLE_TYPES.contains(propValue.getClass())) {
properties.put propName, propValue
} else {
properties.put propName, buildProperties(propValue)
}
}
}

return properties
}

private static List getExcludeFieldNames(def bean) {

List excludeFieldNames = []
excludeFieldNames.addAll EXCLUDE_FIELD_NAMES

bean.getClass().declaredFields.each { Field field ->

if (!excludeFieldNames.contains(field.name) && field.getAnnotation(JsonExclude) != null) {
excludeFieldNames << field.name
}
}

return excludeFieldNames
}

static def newInstanceFromJsonString(String json) {

Map beanData = new JsonSlurper().parseText(json)
return newInstanceFromJsonMap(beanData)
}

static def newInstanceFromJsonMap(Map beanData) {

if (beanData.size() <= 0) {
return null
}

String beanClassName
Map beanProperties

beanData.eachWithIndex { key, value, index ->

// the first pair is the bean (key -> classname, values -> bean properties)
if (index == 0) {
beanClassName = key
beanProperties = value
}
}

return Class.forName(beanClassName).newInstance(beanProperties)
}
}
54 changes: 41 additions & 13 deletions src/main/groovy/de/aonnet/json/JsonExclude.groovy
@@ -1,13 +1,41 @@
package de.aonnet.json

import java.lang.annotation.Documented
import java.lang.annotation.Retention
import java.lang.annotation.Target
import static java.lang.annotation.ElementType.FIELD
import static java.lang.annotation.RetentionPolicy.RUNTIME

@Target(value = FIELD)
@Retention(value = RUNTIME)
@Documented
@interface JsonExclude {
}
/*
* Copyright (c) 2011, Thomas Westphal
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package de.aonnet.json

import java.lang.annotation.Documented
import java.lang.annotation.Retention
import java.lang.annotation.Target
import static java.lang.annotation.ElementType.FIELD
import static java.lang.annotation.RetentionPolicy.RUNTIME

@Target(value = FIELD)
@Retention(value = RUNTIME)
@Documented
@interface JsonExclude {
}
54 changes: 41 additions & 13 deletions src/main/groovy/de/aonnet/json/JsonSupport.groovy
@@ -1,13 +1,41 @@
package de.aonnet.json

import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import org.codehaus.groovy.transform.GroovyASTTransformationClass

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE])
@GroovyASTTransformationClass("de.aonnet.json.JsonSupportTransformation")
public @interface JsonSupport {
}
/*
* Copyright (c) 2011, Thomas Westphal
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package de.aonnet.json

import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import org.codehaus.groovy.transform.GroovyASTTransformationClass

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE])
@GroovyASTTransformationClass("de.aonnet.json.JsonSupportTransformation")
public @interface JsonSupport {
}

0 comments on commit d3c1cad

Please sign in to comment.