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

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

objet Tapas entier avec sérialization

File size: 3.3 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.Collection;
16import java.util.Collections;
17import java.util.Map;
18
19@SuppressWarnings("rawtypes")
20public class JSONHelperImpl
21        implements JSONHelper, InitializingBean
22{
23    public JSONHelperImpl()
24    {
25        _jsonConfig = new JsonConfig();
26    }
27
28    public void afterPropertiesSet()
29            throws Exception
30    {
31        for( final Map.Entry<Class, JsonBeanProcessor> mapping : _mapping.entrySet() )
32        {
33            final Class key = mapping.getKey();
34            final JsonBeanProcessor value = mapping.getValue();
35
36            if( LOGGER.isDebugEnabled() )
37                LOGGER.debug( key + " now uses custom JSON Serializer : " + value.getClass().getName() );
38
39            _jsonConfig.registerJsonBeanProcessor( key, value );
40        }
41    }
42
43    @NotNull
44    public JSON toJSON( @NotNull final Object object )
45    {
46        if( object instanceof Throwable )
47        {
48            final Throwable throwable = (Throwable) object;
49            final Throwable throwableWrapper = new Throwable( throwable );
50            return JSONSerializer.toJSON( throwableWrapper, _jsonConfig );
51        }
52        return JSONSerializer.toJSON( object, _jsonConfig );
53    }
54
55    @NotNull
56    public JSONObject toJSONObject( @NotNull final Object object )
57    {
58        return (JSONObject) toJSON( object );
59    }
60
61    @NotNull
62    public JSONArray toJSON( @Nullable final Object[] collection )
63    {
64        return (JSONArray) JSONSerializer.toJSON( collection, _jsonConfig );
65    }
66
67    @NotNull
68    public JSONArray toJSON( @Nullable final Collection collection )
69    {
70        return (JSONArray) JSONSerializer.toJSON( collection, _jsonConfig );
71    }
72
73    @SuppressWarnings({"unchecked"})
74    @Nullable
75    public <T> T fromJSON( @NotNull final JSON json, @NotNull final Class<T> beanClass )
76    {
77        final JSONObject jsonObject = JSONObject.fromObject( json, _jsonConfig );
78        return (T) JSONObject.toBean( jsonObject, beanClass, _mapClassesForDeserialization.get( beanClass ) );
79    }
80
81    public Map<Class, JsonBeanProcessor> getMapping()
82    {
83        return _mapping;
84    }
85
86    public void setMapping( final Map<Class, JsonBeanProcessor> mapping )
87    {
88        _mapping = mapping;
89    }
90
91    public Map<Class, Map<String, Class>> getMapClassesForDeserialization()
92    {
93        return _mapClassesForDeserialization;
94    }
95
96    public void setMapClassesForDeserialization( final Map<Class, Map<String, Class>> mapClassesForDeserialization )
97    {
98        _mapClassesForDeserialization = mapClassesForDeserialization;
99    }
100
101    public void setJsonConfig( final JsonConfig jsonConfig )
102    {
103        _jsonConfig = jsonConfig;
104    }
105
106    private static final Log LOGGER = LogFactory.getLog( JSONHelperImpl.class );
107
108    private Map<Class, JsonBeanProcessor> _mapping = Collections.emptyMap();
109    private JsonConfig _jsonConfig;
110    private Map<Class, Map<String, Class>> _mapClassesForDeserialization;
111}
Note: See TracBrowser for help on using the repository browser.