Coverage Report - org.apache.shindig.social.core.util.JsonLibConverterUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JsonLibConverterUtils
77%
23/30
62%
10/16
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 
 23  
 import java.util.Map.Entry;
 24  
 
 25  
 import net.sf.json.JSONArray;
 26  
 import net.sf.json.JSONObject;
 27  
 
 28  
 /**
 29  
  * Some utility functions to simpilfy handling SF json-lib objects.
 30  
  */
 31  0
 public final class JsonLibConverterUtils {
 32  
 
 33  
   /**
 34  
    * The Logger.
 35  
    */
 36  1
   protected static final Log LOG = LogFactory.getLog(JsonLibConverterUtils.class);
 37  
 
 38  
   /**
 39  
    * Dumps a JSON Object out to the log at info level.
 40  
    *
 41  
    * @param jsonObject
 42  
    *                The object to dump
 43  
    * @param indent
 44  
    *                the indent to be used per object nesting.
 45  
    */
 46  
   public static final void dumpJsonObject(JSONObject jsonObject, String indent) {
 47  6
     for (Object o : jsonObject.entrySet()) {
 48  9
       Entry<?, ?> entry = (Entry<?, ?>) o;
 49  9
       Object key = entry.getKey();
 50  9
       Object value = entry.getValue();
 51  9
       if (value instanceof JSONObject) {
 52  3
         LOG.info(indent + key + ":JSONObject");
 53  3
         dumpJsonObject((JSONObject) value, indent + "  ");
 54  3
       } else if (value instanceof JSONArray) {
 55  3
         LOG.info(indent + key + ":JSONArray " + ((JSONArray) value).size());
 56  3
         dumpJsonArray((JSONArray) value, indent + "  ");
 57  3
       } else {
 58  3
         if (value == null) {
 59  0
           LOG.info(indent + key + ":null:na");
 60  0
         } else {
 61  3
           LOG.info(indent + key + ":" + value + ":" + value.getClass());
 62  
         }
 63  
       }
 64  9
     }
 65  6
   }
 66  
 
 67  
   /**
 68  
    * Dump a json object to the log.
 69  
    * @param array a json array to dump
 70  
    * @param indent the indent for each level of nesting
 71  
    */
 72  
   public static void dumpJsonArray(JSONArray array, String indent) {
 73  5
     for (Object value : array) {
 74  2
       if (value instanceof JSONObject) {
 75  2
         LOG.info(indent + ":JSONObject");
 76  2
         dumpJsonObject((JSONObject) value, indent + "  ");
 77  2
       } else if (value instanceof JSONArray) {
 78  0
         LOG.info(indent + ":JSONArray " + ((JSONArray) value).size());
 79  0
         dumpJsonArray((JSONArray) value, indent + "  ");
 80  0
       } else {
 81  0
         LOG.info(indent + ":" + value + ":" + (value == null ? "na" : value.getClass()));
 82  
       }
 83  2
     }
 84  5
   }
 85  
 }