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.
/*
* Copyright 2011 the original author or authors.
* 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.jayway.jsonpath.spi.mapper;
import java.lang.reflect.Constructor;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.Set;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.TypeRef;
import jakarta.json.JsonArray;
import jakarta.json.JsonArrayBuilder;
import jakarta.json.JsonException;
import jakarta.json.JsonNumber;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.JsonbException;
import jakarta.json.stream.JsonLocation;
import jakarta.json.stream.JsonParser;
public class JakartaMappingProvider implements MappingProvider {
private final Jsonb jsonb;
private Method jsonToClassMethod, jsonToTypeMethod;
public JakartaMappingProvider() {
this.jsonb = JsonbBuilder.create();
this.jsonToClassMethod = findMethod(jsonb.getClass(), "fromJson", JsonParser.class, Class.class);
this.jsonToTypeMethod = findMethod(jsonb.getClass(), "fromJson", JsonParser.class, Type.class);
}
public JakartaMappingProvider(JsonbConfig jsonbConfiguration) {
this.jsonb = JsonbBuilder.create(jsonbConfiguration);
this.jsonToClassMethod = findMethod(jsonb.getClass(), "fromJson", JsonParser.class, Class.class);
this.jsonToTypeMethod = findMethod(jsonb.getClass(), "fromJson", JsonParser.class, Type.class);
}
/**
* Maps supplied JSON source {@code Object} to a given target class or collection.
* This implementation ignores the JsonPath's {@link Configuration} argument.
*/
@Override
public T map(Object source, Class targetType, Configuration configuration) {
@SuppressWarnings("unchecked")
T result = (T) mapImpl(source, targetType);
return result;
}
/**
* Maps supplied JSON source {@code Object} to a given target type or collection.
* This implementation ignores the JsonPath's {@link Configuration} argument.
*
* Method may produce a {@code ClassCastException} on an attempt to cast
* the result of JSON mapping operation to a requested target type, especially if
* a parameterized generic type is used.
*/
@Override
public T map(Object source, final TypeRef targetType, Configuration configuration) {
@SuppressWarnings("unchecked")
T result = (T) mapImpl(source, targetType.getType());
return result;
}
private Object mapImpl(Object source, final Type targetType) {
if (source == null || source == JsonValue.NULL) {
return null;
}
if (source == JsonValue.TRUE) {
if (Boolean.class.equals(targetType)) {
return Boolean.TRUE;
} else {
String className = targetType.toString();
throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
}
}
if (source == JsonValue.FALSE) {
if (Boolean.class.equals(targetType)) {
return Boolean.FALSE;
} else {
String className = targetType.toString();
throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
}
} else if (source instanceof JsonString) {
if (String.class.equals(targetType)) {
return ((JsonString) source).getChars();
} else {
String className = targetType.toString();
throw new MappingException("JSON string cannot be mapped to " + className);
}
} else if (source instanceof JsonNumber) {
JsonNumber jsonNumber = (JsonNumber) source;
if (jsonNumber.isIntegral()) {
return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));
} else {
return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));
}
}
if (source instanceof JsonArrayBuilder) {
source = ((JsonArrayBuilder) source).build();
} else if (source instanceof JsonObjectBuilder) {
source = ((JsonObjectBuilder) source).build();
}
if (source instanceof Collection) {
// this covers both List and JsonArray from JSON-P spec
Class> rawTargetType = getRawClass(targetType);
Type targetTypeArg = getFirstTypeArgument(targetType);
Collection