Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.apache.tika.serialization;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.fasterxml.jackson.databind.JsonNode;
/**
* See the notes @link{TikaJsonSerializer}.
*
* This currently requires a setString() option on objects that have enum parameters.
*/
public class TikaJsonDeserializer {
public static Optional deserializeObject(JsonNode root) {
if (!root.isObject()) {
throw new IllegalArgumentException("root needs to be an object");
}
if (!root.has(TikaJsonSerializer.INSTANTIATED_CLASS_KEY)) {
throw new IllegalArgumentException("need to specify: " + TikaJsonSerializer.INSTANTIATED_CLASS_KEY);
}
String className = root
.get(TikaJsonSerializer.INSTANTIATED_CLASS_KEY)
.asText();
try {
return Optional.of(deserialize(Class.forName(className), root));
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static T deserialize(Class extends T> clazz, JsonNode root) throws ReflectiveOperationException {
T obj = clazz
.getDeclaredConstructor()
.newInstance();
Map> setters = getSetters(obj);
if (!root.isObject()) {
throw new IllegalArgumentException("must be object");
}
Iterator> fields = root.fields();
while (fields.hasNext()) {
Map.Entry e = fields.next();
String name = e.getKey();
JsonNode child = e.getValue();
if (TikaJsonSerializer.INSTANTIATED_CLASS_KEY.equals(name)) {
continue;
}
setValue(name, child, obj, setters);
}
return obj;
}
private static Map> getSetters(Object obj) {
Map> setters = new HashMap<>();
for (Method m : obj
.getClass()
.getMethods()) {
String n = m.getName();
if (n.startsWith(TikaJsonSerializer.SET) && n.length() > 3 && Character.isUpperCase(n.charAt(3))) {
if (m.getParameters().length == 1) {
String paramName = TikaJsonSerializer.getParam(TikaJsonSerializer.SET, n);
List methods = setters.get(paramName);
if (methods == null) {
methods = new ArrayList<>();
setters.put(paramName, methods);
}
methods.add(m);
}
}
}
return setters;
}
private static void setValue(String name, JsonNode node, Object obj, Map> setters) throws ReflectiveOperationException {
List mySetters = setters.get(name);
if (mySetters == null || mySetters.size() == 0) {
throw new IllegalArgumentException("can't find any setter for " + name);
}
if (node.isNull()) {
setNull(name, node, obj, mySetters);
} else if (node.isNumber()) {
setNumericValue(name, node, obj, mySetters);
} else if (node.isTextual()) {
setStringValue(name, node.asText(), obj, mySetters);
} else if (node.isArray()) {
setArray(name, node, obj, mySetters);
} else if (node.isObject()) {
setObject(name, node, obj, mySetters);
} else if (node.isBoolean()) {
setBoolean(name, node, obj, mySetters);
}
}
private static void setArray(String name, JsonNode node, Object obj, List mySetters) {
//there's much more to be done here. :(
for (Method setter : mySetters) {
try {
tryArray(name, node, obj, setter);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new IllegalArgumentException("couldn't create array for " + name);
}
}
}
private static void tryArray(String name, JsonNode node, Object obj, Method setter) throws InvocationTargetException, IllegalAccessException {
Class argClass = setter.getParameterTypes()[0];
Class componentType = argClass.getComponentType();
if (argClass.isArray()) {
int len = node.size();
Object arrayObject = Array.newInstance(componentType, len);
for (int i = 0; i < len; i++) {
Array.set(arrayObject, i, getVal(componentType, node.get(i)));
}
setter.invoke(obj, arrayObject);
} else if (List.class.isAssignableFrom(argClass)) {
Type listType = setter.getGenericParameterTypes()[0];
Type elementType = null;
if (listType instanceof ParameterizedType) {
elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];
}
if (elementType == null) {
throw new IllegalArgumentException("Can't infer parameterized type for list in: " + node);
}
int len = node.size();
List