com.bazaarvoice.jolt.JsonUtils Maven / Gradle / Ivy
/*
* Copyright 2013 Bazaarvoice, Inc.
*
* 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.bazaarvoice.jolt;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Static method convenience wrappers for a JsonUtil configured with a minimal ObjectMapper.
*
* The ObjectMapper use is configured to :
* Allow comments in the JSON strings,
* Hydrates all JSON Maps into LinkedHashMaps.
*/
public class JsonUtils {
private static final JsonUtil util = new JsonUtilImpl();
/**
* Construct a JsonUtil with a Jackson ObjectMapper that has been preconfigured with custom
* Modules or Mixins.
*/
public static JsonUtil customJsonUtil( ObjectMapper mapper ) {
return new JsonUtilImpl( mapper );
}
/**
* Removes a key recursively from anywhere in a JSON document.
* NOTE: mutates its input.
*
* Deprecated: use JoltUtils instead
*
* @param json the Jackson Object version of the JSON document
* (contents changed by this call)
* @param keyToRemove the key to remove from the document
*/
@Deprecated
public static void removeRecursive( Object json, String keyToRemove ) {
if ( ( json == null ) || ( keyToRemove == null ) ) {
return;
}
if ( json instanceof Map ) {
@SuppressWarnings("unchecked")
Map jsonMap = (Map) json;
// If this level of the tree has the key we are looking for, remove it
// Do the lookup instead of just the remove to avoid un-necessarily
// dying on ImmutableMaps.
if ( jsonMap.containsKey( keyToRemove ) ) {
jsonMap.remove( keyToRemove );
}
// regardless, recurse down the tree
for ( Object value : jsonMap.values() ) {
removeRecursive( value, keyToRemove );
}
}
if ( json instanceof List ) {
for ( Object value : (List) json ) {
removeRecursive( value, keyToRemove );
}
}
}
/**
* Utility for test classes, so that they can inline json in a test class.
* Does a character level replacement of apostrophe (') with double quote (").
*
* This means you can express a snippit of JSON without having to forward
* slash escape everything.
*
* This is character based, so don't have any apostrophes (') in your test
* data.
*
* @param javason JSON-ish string you want to turn into Maps-of-Maps
* @return Maps-of-Maps
*/
public static Map javason( String javason ) {
String json = javason.replace( '\'', '"' );
return jsonToMap( new ByteArrayInputStream( json.getBytes() ) );
}
public static JsonUtil getDefaultJsonUtil() {
return util;
}
//// All the methods listed below are static passthrus to the JsonUtil interface
public static Object jsonToObject( String json ) {
return util.jsonToObject( json );
}
public static Object jsonToObject( String json, String charset ) {
return util.jsonToObject( json, charset );
}
public static Object jsonToObject( InputStream in ) {
return util.jsonToObject( in );
}
public static Map jsonToMap( String json ) {
return util.jsonToMap( json );
}
public static Map jsonToMap( String json, String charset ) {
return util.jsonToMap( json, charset );
}
public static Map jsonToMap( InputStream in ) {
return util.jsonToMap( in );
}
public static List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy