org.elasticsearch.hadoop.serialization.json.BackportedObjectReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch-hadoop-mr Show documentation
Show all versions of elasticsearch-hadoop-mr Show documentation
Elasticsearch Hadoop Map/Reduce
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.hadoop.serialization.json;
/*
* This file contains code from org.codehaus.jackson.map.ObjectReader.
* All copyrights apply.
* Persuant to section 4 of the Apache 2.0 License, the license for
* the file follows:
*/
/*
* 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.
*/
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.JsonFactory;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.JsonGenerator;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.JsonParser;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.JsonProcessingException;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.DeserializationConfig;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.DeserializationContext;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.DeserializerProvider;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.JsonDeserializer;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.JsonMappingException;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.ObjectMapper;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.deser.StdDeserializationContext;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.introspect.VisibilityChecker;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.jsontype.TypeResolverBuilder;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.type.ArrayType;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.map.type.TypeFactory;
import org.elasticsearch.hadoop.thirdparty.codehaus.jackson.type.JavaType;
import org.elasticsearch.hadoop.util.Assert;
import org.elasticsearch.hadoop.util.ReflectionUtils;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
/**
* Backported class from Jackson 1.8.8 for Jackson 1.5.2.
* Used only when dealing with Jackson 1.5 otherwise the proper Jackson class is used which
* saves us the hassle of keeping up with the breaking changes in Jackson library.
*
* Builder object that can be used for per-serialization configuration of
* deserialization parameters, such as root type to use or object
* to update (instead of constructing new instance).
* Uses "fluid" (aka builder) pattern so that instances are immutable
* (and thus fully thread-safe with no external synchronization);
* new instances are constructed for different configurations.
* Instances are initially constructed by {@link ObjectMapper} and can be
* reused.
*
* @author tatu
* @since 1.6
*/
public class BackportedObjectReader implements ObjectReader {
final static Field ROOT_DESERIALIZERS;
static {
Field fl = ReflectionUtils.findField(ObjectMapper.class, "_rootDeserializers");
Assert.notNull(fl, "Cannot find root deserializers");
ROOT_DESERIALIZERS = fl;
ReflectionUtils.makeAccessible(fl);
}
/*
/**********************************************************
/* Immutable configuration from ObjectMapper
/**********************************************************
*/
/**
* Root-level cached deserializers
*/
final protected ConcurrentHashMap> _rootDeserializers;
/**
* General serialization configuration settings
*/
protected final DeserializationConfig _config;
protected final DeserializerProvider _provider;
/**
* Factory used for constructing {@link JsonGenerator}s
*/
protected final JsonFactory _jsonFactory;
// Support for polymorphic types:
protected TypeResolverBuilder> _defaultTyper;
// Configurable visibility limits
protected VisibilityChecker> _visibilityChecker;
/*
/**********************************************************
/* Configuration that can be changed during building
/**********************************************************
*/
/**
* Declared type of value to instantiate during deserialization.
* Defines which deserializer to use; as well as base type of instance
* to construct if an updatable value is not configured to be used
* (subject to changes by embedded type information, for polymorphic
* types). If {@link #_valueToUpdate} is non-null, only used for
* locating deserializer.
*/
protected final JavaType _valueType;
/**
* Instance to update with data binding; if any. If null,
* a new instance is created, if non-null, properties of
* this value object will be updated instead.
* Note that value can be of almost any type, except not
* {@link ArrayType}; array
* types can not be modified because array size is immutable.
*/
protected final Object _valueToUpdate;
public static BackportedObjectReader create(ObjectMapper mapper, Class> type) {
return new BackportedObjectReader(mapper, TypeFactory.type(type), null);
}
/**
* Constructor used by {@link ObjectMapper} for initial instantiation
*/
protected BackportedObjectReader(ObjectMapper mapper, JavaType valueType, Object valueToUpdate) {
_rootDeserializers = ReflectionUtils.getField(ROOT_DESERIALIZERS, mapper);
_provider = mapper.getDeserializerProvider();
_jsonFactory = mapper.getJsonFactory();
// must make a copy at this point, to prevent further changes from trickling down
_config = mapper.copyDeserializationConfig();
_valueType = valueType;
_valueToUpdate = valueToUpdate;
if (valueToUpdate != null && valueType.isArrayType()) {
throw new IllegalArgumentException("Can not update an array value");
}
}
public BackportedJacksonMappingIterator readValues(JsonParser jp) throws IOException,
JsonProcessingException {
DeserializationContext ctxt = _createDeserializationContext(jp, _config);
return new BackportedJacksonMappingIterator(_valueType, jp, ctxt, _findRootDeserializer(_config, _valueType));
}
/**
* Method called to locate deserializer for the passed root-level value.
*/
protected JsonDeserializer