Open Source Repository

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



net/sf/json/util/WebUtils.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.util;

import java.util.Iterator;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

/**
 * Provides useful methods for working with JSON and web.
 *
 @author Andres Almiray <[email protected]>
 */
public class WebUtils {
   private static final WebHijackPreventionStrategy DEFAULT_WEB_HIJACK_PREVENTION_STRATEGY = WebHijackPreventionStrategy.INFINITE_LOOP;
   private static WebHijackPreventionStrategy webHijackPreventionStrategy = DEFAULT_WEB_HIJACK_PREVENTION_STRATEGY;

   /**
    * Returns the configured WebHijackPreventionStrategy.
    */
   public static WebHijackPreventionStrategy getWebHijackPreventionStrategy() {
      return webHijackPreventionStrategy;
   }

   /**
    * Transforms the input Json string using the configured
    * WebHijackPreventionStrategy.<br>
    *
    @param json the input string
    @return String a transformed json string
    */
   public static String protectJSON json ) {
      return protectjson, false );
   }

   /**
    * Transforms the input Json string using the configured
    * WebHijackPreventionStrategy.<br>
    *
    @param json the input string
    @param shrink if redundant key quotes may be eliminated.
    @return String a transformed json string
    */
   public static String protectJSON json, boolean shrink ) {
      String output = !shrink ? json.toString: toStringjson );
      return webHijackPreventionStrategy.protectoutput );
   }

   /**
    * Sets a WebHijackPreventionStrategy.<br>
    * Will use default value (WebHijackPreventionStrategy.INFINITE_LOOP) if
    * null.
    */
   public static void setWebHijackPreventionStrategyWebHijackPreventionStrategy strategy ) {
      webHijackPreventionStrategy = strategy == null ? DEFAULT_WEB_HIJACK_PREVENTION_STRATEGY
            : strategy;
   }

   /**
    * Returns a string represenation of a JSON value.<br>
    * When an object property name does not contain a space (' ') or a colon
    * (':'), the quotes are omitted. This is done to reduce the amount of bytes
    * sent to a web browser.<br/>USE WITH CAUTION.
    */
   public static String toStringJSON json ) {
      ifjson instanceof JSONObject ){
         return toString( (JSONObjectjson );
      }else ifjson instanceof JSONArray ){
         return toString( (JSONArrayjson );
      }else{
         return toString( (JSONNulljson );
      }
   }

   private static String joinJSONArray jsonArray ) {
      int len = jsonArray.size();
      StringBuffer sb = new StringBuffer();

      forint i = 0; i < len; i += ){
         ifi > ){
            sb.append"," );
         }
         Object value = jsonArray.get);
         sb.appendtoStringvalue ) );

      }
      return sb.toString();
   }

   private static String quoteString str ) {
      ifstr.indexOf" " > -|| str.indexOf":" > -){
         return JSONUtils.quotestr );
      }else{
         return str;
      }
   }

   private static String toStringJSONArray jsonArray ) {
      try{
         return '[' + joinjsonArray ']';
      }catchException e ){
         return null;
      }
   }

   private static String toStringJSONNull jsonNull ) {
      return jsonNull.toString();
   }

   private static String toStringJSONObject jsonObject ) {
      ifjsonObject.isNullObject() ){
         return JSONNull.getInstance()
               .toString();
      }
      Iterator keys = jsonObject.keys();
      StringBuffer sb = new StringBuffer"{" );

      whilekeys.hasNext() ){
         ifsb.length() ){
            sb.append',' );
         }
         Object o = keys.next();
         sb.appendquoteo.toString() ) );
         sb.append':' );
         sb.appendtoStringjsonObject.getString.valueOf) ) ) );
      }
      sb.append'}' );
      return sb.toString();
   }

   private static String toStringObject object ) {
      ifobject instanceof JSON ){
         return toString( (JSONobject );
      }else{
         return JSONUtils.valueToStringobject );
      }
   }

   private WebUtils() {
   }
}