001 /*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2006-2020 the original author or authors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.kordamp.json;
019
020 import org.kordamp.json.util.JSONTokener;
021 import org.kordamp.json.util.JSONUtils;
022
023 /**
024 * Transforms java objects into JSON and back.<br>
025 * Transformation from java to JSON is pretty straightforward, but the other way
026 * around needs certain configuration, otherwise the java objects produced will
027 * be DynaBeans and Lists, because the JSON notation does not carry any
028 * information on java classes.<br>
029 *
030 * @author Andres Almiray
031 */
032 public class JSONSerializer {
033 /**
034 * Transform a JSON value to a java object.<br>
035 * Depending on the configured values for conversion this will return a
036 * DynaBean, a bean, a List, or and array.
037 *
038 * @param json a JSON value
039 *
040 * @return depends on the nature of the source object (JSONObject, JSONArray,
041 * JSONNull).
042 */
043 public static Object toJava(JSON json) {
044 return toJava(json, new JsonConfig());
045 }
046
047 /**
048 * Transform a JSON value to a java object.<br>
049 * Depending on the configured values for conversion this will return a
050 * DynaBean, a bean, a List, or and array.
051 *
052 * @param json a JSON value
053 * @param jsonConfig additional configuration
054 *
055 * @return depends on the nature of the source object (JSONObject, JSONArray,
056 * JSONNull) and the configured rootClass, classMap and arrayMode
057 */
058 public static Object toJava(JSON json, JsonConfig jsonConfig) {
059 if (JSONUtils.isNull(json)) {
060 return null;
061 }
062
063 Object object = null;
064
065 if (json instanceof JSONArray) {
066 if (jsonConfig.getArrayMode() == JsonConfig.MODE_OBJECT_ARRAY) {
067 object = JSONArray.toArray((JSONArray) json, jsonConfig);
068 } else {
069 object = JSONArray.toCollection((JSONArray) json, jsonConfig);
070 }
071 } else {
072 object = JSONObject.toBean((JSONObject) json, jsonConfig);
073 }
074
075 return object;
076 }
077
078 /**
079 * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
080 * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
081 * JavaBeans.
082 *
083 * @param object any java Object
084 *
085 * @throws JSONException if the object can not be converted
086 */
087 public static JSON toJSON(Object object) {
088 return toJSON(object, new JsonConfig());
089 }
090
091 /**
092 * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
093 * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
094 * JavaBeans.
095 *
096 * @param object any java Object
097 * @param jsonConfig additional configuration
098 *
099 * @throws JSONException if the object can not be converted
100 */
101 public static JSON toJSON(Object object, JsonConfig jsonConfig) {
102 JSON json = null;
103 if (object == null) {
104 json = JSONNull.getInstance();
105 } else if (object instanceof JSONString) {
106 json = toJSON((JSONString) object, jsonConfig);
107 } else if (object instanceof String) {
108 json = toJSON((String) object, jsonConfig);
109 } else if (JSONUtils.isArray(object)) {
110 json = JSONArray.fromObject(object, jsonConfig);
111 } else {
112 try {
113 json = JSONObject.fromObject(object, jsonConfig);
114 } catch (JSONException e) {
115 if (object instanceof JSONTokener) {
116 ((JSONTokener) object).reset();
117 }
118 json = JSONArray.fromObject(object, jsonConfig);
119 }
120 }
121
122 return json;
123 }
124
125 /**
126 * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
127 *
128 * @throws JSONException if the string is not a valid JSON string
129 */
130 private static JSON toJSON(JSONString string, JsonConfig jsonConfig) {
131 return toJSON(string.toJSONString(), jsonConfig);
132 }
133
134 /**
135 * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
136 *
137 * @throws JSONException if the string is not a valid JSON string
138 */
139 private static JSON toJSON(String string, JsonConfig jsonConfig) {
140 JSON json = null;
141 if (string.startsWith("[")) {
142 json = JSONArray.fromObject(string, jsonConfig);
143 } else if (string.startsWith("{")) {
144 json = JSONObject.fromObject(string, jsonConfig);
145 } else if ("null".equals(string)) {
146 json = JSONNull.getInstance();
147 } else {
148 throw new JSONException("Invalid JSON String");
149 }
150
151 return json;
152 }
153 }
|