Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2006-2014 phloc systems http://www.phloc.com office[at]phloc[dot]com 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 com.phloc.json;
 
 import java.util.Map;
 
 
General utility class for JSON string operations like escaping and unescaping

Author(s):
Boris Gregorcic
 
 public final class JSONComplexUtils
 {
   private JSONComplexUtils ()
   {}

  
Converts the passed property map to a new JSON object containing all properties from the map.
ATTENTION: Currently only simple types are supported, no container types or nested objects!

Parameters:
aProperties
Returns:
the created JSON object
 
   @Nonnull
   public static IJSONObject convertToJSON (@Nullable final Map <StringObjectaProperties)
   {
     return convertToJSON (aPropertiesfalse);
   }

  
Converts the passed property map to a new JSON object containing all properties from the map.
ATTENTION: Currently only simple types are supported, no container types or nested objects!

Parameters:
aProperties The source map
bUseTypeConverter If true the type converter registry should be used to convert simple values.
Returns:
the created JSON object
 
   @Nonnull
   public static IJSONObject convertToJSON (@Nullable final Map <StringObjectaProperties,
                                            final boolean bUseTypeConverter)
   {
     final IJSONObject aJSON = new JSONObject ();
     if (aProperties != null)
     {
       // Sort map by key, to create reproducible results
       for (final Map.Entry <StringObjectaEntry : ContainerHelper.getSortedByKey (aProperties).entrySet ())
       {
         final String sPropertyName = aEntry.getKey ();
         final Object aValue = aEntry.getValue ();
         if (aValue == null)
         {
           // Special null keyword
           aJSON.setKeywordProperty (sPropertyName.);
         }
         else
           if (aValue instanceof IJSONConvertible)
           {
             // Object itself can be converted to JSON
             final IJSONObject aNestedValue = ((IJSONConvertibleaValue).getAsJSON ();
             aJSON.setObjectProperty (sPropertyNameaNestedValue);
           }
           else
             if (aValue instanceof Map <?, ?>)
             {
               // Its a nested map so recurse into it
               @SuppressWarnings ("unchecked")
               final IJSONObject aNestedValue = convertToJSON ((Map <StringObject>) aValue);
               aJSON.setObjectProperty (sPropertyNameaNestedValue);
            }
            else
            {
              // Main set property value
              aJSON.setProperty (sPropertyNameaValuebUseTypeConverter);
            }
      }
    }
    return aJSON;
  }
New to GrepCode? Check out our FAQ X