1 | package com.ether; |
---|
2 | |
---|
3 | import net.sf.json.JSON; |
---|
4 | import net.sf.json.JSONArray; |
---|
5 | import net.sf.json.JSONObject; |
---|
6 | import net.sf.json.JSONSerializer; |
---|
7 | import net.sf.json.JsonConfig; |
---|
8 | import net.sf.json.processors.JsonBeanProcessor; |
---|
9 | import org.apache.commons.logging.Log; |
---|
10 | import org.apache.commons.logging.LogFactory; |
---|
11 | import org.jetbrains.annotations.NotNull; |
---|
12 | import org.jetbrains.annotations.Nullable; |
---|
13 | import org.springframework.beans.factory.InitializingBean; |
---|
14 | |
---|
15 | import java.util.Collection; |
---|
16 | import java.util.Collections; |
---|
17 | import java.util.Map; |
---|
18 | |
---|
19 | @SuppressWarnings("rawtypes") |
---|
20 | public 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 | } |
---|