source: geisa/common/implementation/com/ether/JSONHelperImpl.java @ 390

Last change on this file since 390 was 390, checked in by npipsl, 12 years ago

Création du projet GEISA

File size: 5.2 KB
Line 
1package com.ether;
2
3import java.lang.reflect.TypeVariable;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.Map;
7
8import net.sf.json.JSON;
9import net.sf.json.JSONArray;
10import net.sf.json.JSONObject;
11import net.sf.json.JSONSerializer;
12import net.sf.json.JsonConfig;
13import net.sf.json.processors.JsonBeanProcessor;
14
15import org.apache.commons.logging.Log;
16import org.apache.commons.logging.LogFactory;
17import org.jetbrains.annotations.NotNull;
18import org.jetbrains.annotations.Nullable;
19import org.springframework.beans.factory.InitializingBean;
20
21@SuppressWarnings("rawtypes")
22public class JSONHelperImpl
23        implements JSONHelper, InitializingBean
24{
25    public JSONHelperImpl()
26    {
27        _jsonConfig = new JsonConfig();
28    }
29
30        public void afterPropertiesSet()
31            throws Exception
32    {
33        for( final Map.Entry<Class, JsonBeanProcessor> mapping : _mapping.entrySet() )
34        {
35            final Class key = mapping.getKey();
36            final JsonBeanProcessor value = mapping.getValue();
37
38            if( LOGGER.isDebugEnabled() )
39                LOGGER.debug( key + " now uses custom JSON Serializer : " + value.getClass().getName() );
40
41            _jsonConfig.registerJsonBeanProcessor( key, value );
42        }
43    }
44
45    @NotNull
46    public JSON toJSON( @NotNull final Object object )
47    {
48        if( object instanceof Throwable )
49        {
50            final Throwable throwable = (Throwable) object;
51            final Throwable throwableWrapper = new Throwable( throwable );
52            return JSONSerializer.toJSON( throwableWrapper, _jsonConfig );
53        }
54        return JSONSerializer.toJSON( object, _jsonConfig );
55    }
56
57    @NotNull
58    public JSONObject toJSONObject( @NotNull final Object object )
59    {
60        return (JSONObject) toJSON( object );
61    }
62
63    @NotNull
64    public JSONArray toJSON( @Nullable final Object[] collection )
65    {
66        return (JSONArray) JSONSerializer.toJSON( collection, _jsonConfig );
67    }
68
69    @NotNull
70    public JSONArray toJSON( @Nullable final Collection collection )
71    {
72        return (JSONArray) JSONSerializer.toJSON( collection, _jsonConfig );
73    }
74
75    @SuppressWarnings({"unchecked"})
76    @Nullable
77    public <T> T fromJSON( @NotNull final JSON json, @NotNull final Class<T> beanClass )
78    {
79        if( json.isArray() )
80        {
81            final JSONArray jsonArray = JSONArray.fromObject( json, _jsonConfig );
82            if( beanClass.isArray() )
83            {
84                final Class<?> componentType = beanClass.getComponentType();
85                //json.sf.net does not support Long[] or long[]
86                if( Long.class.equals( componentType ) || Long.TYPE.equals( componentType ) )
87                {
88                    final Integer[] integers = (Integer[]) JSONArray.toArray( jsonArray, Integer.class );
89                    if( Long.class.equals( componentType ) )
90                    {
91                        final Long[] resultArray = new Long[integers.length];
92                        for( int i = 0; i < integers.length; ++i )
93                            resultArray[i] = integers[i].longValue();
94                        return (T) resultArray;
95                    }
96                    if( Long.TYPE.equals( componentType ) )
97                    {
98                        final long[] resultArray = new long[integers.length];
99                        for( int i = 0; i < integers.length; ++i )
100                            resultArray[i] = integers[i].longValue();
101                        return (T) resultArray;
102                    }
103                }
104                return (T) JSONArray.toArray( jsonArray, componentType );
105            }
106            if( beanClass.isAssignableFrom( Collection.class ) )
107            {
108                final TypeVariable<Class<T>>[] typeParameters = beanClass.getTypeParameters();
109                if( null == typeParameters || typeParameters.length != 1 || null == typeParameters[0] )
110                    return (T) JSONArray.toCollection( jsonArray );
111                return (T) JSONArray.toCollection( jsonArray, typeParameters[0].getGenericDeclaration() );
112            }
113        }
114        final JSONObject jsonObject = JSONObject.fromObject( json, _jsonConfig );
115        return (T) JSONObject.toBean( jsonObject, beanClass, _mapClassesForDeserialization.get( beanClass ) );
116    }
117
118    public Map<Class, JsonBeanProcessor> getMapping()
119    {
120        return _mapping;
121    }
122
123    public void setMapping( final Map<Class, JsonBeanProcessor> mapping )
124    {
125        _mapping = mapping;
126    }
127
128    public Map<Class, Map<String, Class>> getMapClassesForDeserialization()
129    {
130        return _mapClassesForDeserialization;
131    }
132
133    public void setMapClassesForDeserialization( final Map<Class, Map<String, Class>> mapClassesForDeserialization )
134    {
135        _mapClassesForDeserialization = mapClassesForDeserialization;
136    }
137
138    public void setJsonConfig( final JsonConfig jsonConfig )
139    {
140        _jsonConfig = jsonConfig;
141    }
142
143    private static final Log LOGGER = LogFactory.getLog(JSONHelperImpl.class);   
144
145    private Map<Class, JsonBeanProcessor> _mapping = Collections.emptyMap();
146    private JsonConfig _jsonConfig;
147    private Map<Class, Map<String, Class>> _mapClassesForDeserialization;
148}
Note: See TracBrowser for help on using the repository browser.