Open Source Repository

Home /json/json-lib-2.4-jdk15 | Repository Home



net/sf/json/JSONSerializer.java
/*
 * Copyright 2002-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.json;

import net.sf.json.util.JSONTokener;
import net.sf.json.util.JSONUtils;

/**
 * Transforms java objects into JSON and back.<br>
 * Transformation from java to JSON is pretty straightforward, but the other way
 * around needs certain configuration, otherwise the java objects produced will
 * be DynaBeans and Lists, because the JSON notation does not carry any
 * information on java classes.<br>
 *
 @author Andres Almiray <[email protected]>
 */
public class JSONSerializer {
   /**
    * Transform a JSON value to a java object.<br>
    * Depending on the configured values for conversion this will return a
    * DynaBean, a bean, a List, or and array.
    *
    @param json a JSON value
    @return depends on the nature of the source object (JSONObject, JSONArray,
    *         JSONNull).
    */
   public static Object toJavaJSON json ) {
      return toJavajson, new JsonConfig() );
   }

   /**
    * Transform a JSON value to a java object.<br>
    * Depending on the configured values for conversion this will return a
    * DynaBean, a bean, a List, or and array.
    *
    @param json a JSON value
    @param jsonConfig additional configuration
    @return depends on the nature of the source object (JSONObject, JSONArray,
    *         JSONNull) and the configured rootClass, classMap and arrayMode
    */
   public static Object toJavaJSON json, JsonConfig jsonConfig ) {
      ifJSONUtils.isNulljson ) ){
         return null;
      }

      Object object = null;

      ifjson instanceof JSONArray ){
         ifjsonConfig.getArrayMode() == JsonConfig.MODE_OBJECT_ARRAY ){
            object = JSONArray.toArray( (JSONArrayjson, jsonConfig );
         }else{
            object = JSONArray.toCollection( (JSONArrayjson, jsonConfig );
         }
      }else{
         object = JSONObject.toBean( (JSONObjectjson, jsonConfig );
      }

      return object;
   }

   /**
    * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
    * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
    * JavaBeans.
    *
    @param object any java Object
    @throws JSONException if the object can not be converted
    */
   public static JSON toJSONObject object ) {
      return toJSONobject, new JsonConfig() );
   }

   /**
    * Creates a JSONObject, JSONArray or a JSONNull from object.<br>
    * Accepts JSON formatted strings, Maps, arrays, Collections, DynaBeans and
    * JavaBeans.
    *
    @param object any java Object
    @param jsonConfig additional configuration
    @throws JSONException if the object can not be converted
    */
   public static JSON toJSONObject object, JsonConfig jsonConfig ) {
      JSON json = null;
      ifobject == null ){
         json = JSONNull.getInstance();
      }else ifobject instanceof JSONString ){
         json = toJSON( (JSONStringobject, jsonConfig );
      }else ifobject instanceof String ){
         json = toJSON( (Stringobject, jsonConfig );
      }else ifJSONUtils.isArrayobject ) ){
         json = JSONArray.fromObjectobject, jsonConfig );
      }else{
         try{
            json = JSONObject.fromObjectobject, jsonConfig );
         }catchJSONException e ){
            ifobject instanceof JSONTokener ){
               ((JSONTokenerobject).reset();
            }
            json = JSONArray.fromObjectobject, jsonConfig );
         }
      }

      return json;
   }

   /**
    * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
    *
    @throws JSONException if the string is not a valid JSON string
    */
   private static JSON toJSONJSONString string, JsonConfig jsonConfig ) {
      return toJSONstring.toJSONString(), jsonConfig );
   }

   /**
    * Creates a JSONObject, JSONArray or a JSONNull from a JSONString.
    *
    @throws JSONException if the string is not a valid JSON string
    */
   private static JSON toJSONString string, JsonConfig jsonConfig ) {
      JSON json = null;
      ifstring.startsWith"[" ) ){
         json = JSONArray.fromObjectstring, jsonConfig );
      }else ifstring.startsWith"{" ) ){
         json = JSONObject.fromObjectstring, jsonConfig );
      }else if"null".equalsstring ) ){
         json = JSONNull.getInstance();
      }else{
         throw new JSONException"Invalid JSON String" );
      }

      return json;
   }
}