source: tapas/common/implementation/com/ether/JSONHelperImpl.java @ 399

Last change on this file since 399 was 399, checked in by vmipsl, 12 years ago

commit temporaire _ serialization json Tapas
PAS FINI !!

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