1 /*
2 * Copyright 2002-2009 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.json;
18
19 import net.sf.json.util.JSONTokener;
20 import net.sf.json.util.JSONUtils;
21
22 /**
23 * Transforms java objects into JSON and back.<br>
24 * Transformation from java to JSON is pretty straightforward, but the other way
25 * around needs certain configuration, otherwise the java objects produced will
26 * be DynaBeans and Lists, because the JSON notation does not carry any
27 * information on java classes.<br>
28 *
29 * @author Andres Almiray <aalmiray@users.sourceforge.net>
30 */
31 public class JSONSerializer {
32 /**
33 * Transform a JSON value to a java object.<br>
34 * Depending on the configured values for conversion this will return a
35 * DynaBean, a bean, a List, or and array.
36 *
37 * @param json a JSON value
38 * @return depends on the nature of the source object (JSONObject, JSONArray,
39 * JSONNull).
40 */
41 public static Object toJava( JSON json ) {
42 return toJava( json, new JsonConfig() );
43 }
44
45 /**
46 * Transform a JSON value to a java object.<br>
47 * Depending on the configured values for conversion this will return a
48 * DynaBean, a bean, a List, or and array.
49 *
50 * @param json a JSON value
51 * @param jsonConfig additional configuration
52 * @return depends on the nature of the source object (JSONObject, JSONArray,
53 * JSONNull) and the configured rootClass, classMap and arrayMode
54 */
55 public static Object toJava( JSON json, JsonConfig jsonConfig ) {
56 if( JSONUtils.isNull( json ) ){
57 return null;
58 }
59
60 Object object = null;
61
62 if( json instanceof JSONArray ){
63 if( jsonConfig.getArrayMode() == JsonConfig.MODE_OBJECT_ARRAY ){
64 object = JSONArray.toArray( (JSONArray) json, jsonConfig );
65 }else{
66 object = JSONArray.toCollection( (JSONArray) json, jsonConfig );
67 }
68 }else{
69 object = JSONObject.toBean( (JSONObject) json, jsonConfig );
70 }
71
72 return object;
73 }
74
75 /**
76 * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
77 * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
78 * JavaBeans.
79 *
80 * @param object any java Object
81 * @throws JSONException if the object can not be converted
82 */
83 public static JSON toJSON( Object object ) {
84 return toJSON( object, new JsonConfig() );
85 }
86
87 /**
88 * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
89 * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
90 * JavaBeans.
91 *
92 * @param object any java Object
93 * @param jsonConfig additional configuration
94 * @throws JSONException if the object can not be converted
95 */
96 public static JSON toJSON( Object object, JsonConfig jsonConfig ) {
97 JSON json = null;
98 if( object == null ){
99 json = JSONNull.getInstance();
100 }else if( object instanceof JSONString ){
101 json = toJSON( (JSONString) object, jsonConfig );
102 }else if( object instanceof String ){
103 json = toJSON( (String) object, jsonConfig );
104 }else if( JSONUtils.isArray( object ) ){
105 json = JSONArray.fromObject( object, jsonConfig );
106 }else{
107 try{
108 json = JSONObject.fromObject( object, jsonConfig );
109 }catch( JSONException e ){
110 if( object instanceof JSONTokener ){
111 ((JSONTokener) object).reset();
112 }
113 json = JSONArray.fromObject( object, jsonConfig );
114 }
115 }
116
117 return json;
118 }
119
120 /**
121 * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
122 *
123 * @throws JSONException if the string is not a valid JSON string
124 */
125 private static JSON toJSON( JSONString string, JsonConfig jsonConfig ) {
126 return toJSON( string.toJSONString(), jsonConfig );
127 }
128
129 /**
130 * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
131 *
132 * @throws JSONException if the string is not a valid JSON string
133 */
134 private static JSON toJSON( String string, JsonConfig jsonConfig ) {
135 JSON json = null;
136 if( string.startsWith( "[" ) ){
137 json = JSONArray.fromObject( string, jsonConfig );
138 }else if( string.startsWith( "{" ) ){
139 json = JSONObject.fromObject( string, jsonConfig );
140 }else if( "null".equals( string ) ){
141 json = JSONNull.getInstance();
142 }else{
143 throw new JSONException( "Invalid JSON String" );
144 }
145
146 return json;
147 }
148 }