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.
package com.stripe.net;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import com.stripe.Stripe;
import com.stripe.param.common.EmptyParam;
import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;
/**
* Converter to map an api request object to an untyped map. It is not called a *Serializer because
* the outcome is not a JSON data. It is not called *UntypedMapDeserializer because it is not
* converting from JSON.
*/
class ApiRequestParamsConverter {
private static final Gson GSON =
new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapterFactory(new HasEmptyEnumTypeAdapterFactory())
.registerTypeAdapterFactory(new NullValuesInMapsTypeAdapterFactory())
.create();
private static final UntypedMapDeserializer FLATTENING_EXTRA_PARAMS_DESERIALIZER =
new UntypedMapDeserializer(new ExtraParamsFlatteningStrategy());
/** Strategy to flatten extra params in the API request parameters. */
private static class ExtraParamsFlatteningStrategy implements UntypedMapDeserializer.Strategy {
@Override
public void deserializeAndTransform(
Map outerMap,
Map.Entry jsonEntry,
UntypedMapDeserializer untypedMapDeserializer) {
String key = jsonEntry.getKey();
JsonElement jsonValue = jsonEntry.getValue();
if (ApiRequestParams.EXTRA_PARAMS_KEY.equals(key)) {
if (!jsonValue.isJsonObject()) {
throw new IllegalStateException(
String.format(
"Unexpected schema for extra params. JSON object is expected at key `%s`, but found"
+ " `%s`. This is likely a problem with this current library version `%s`. "
+ "Please contact [email protected] for assistance.",
ApiRequestParams.EXTRA_PARAMS_KEY, jsonValue, Stripe.VERSION));
}
// JSON value now corresponds to the extra params map, and is also deserialized as a map.
// Instead of putting this result map under the original key, flatten the map
// by adding all its key/value pairs to the outer map instead.
Map extraParamsMap =
untypedMapDeserializer.deserialize(jsonValue.getAsJsonObject());
for (Map.Entry entry : extraParamsMap.entrySet()) {
validateDuplicateKey(outerMap, entry.getKey(), entry.getValue());
outerMap.put(entry.getKey(), entry.getValue());
}
} else {
Object value = untypedMapDeserializer.deserializeJsonElement(jsonValue);
validateDuplicateKey(outerMap, key, value);
// Normal deserialization where output map has the same structure as the given JSON content.
// The deserialized content is an untyped `Object` and added to the outer map at the
// original key.
outerMap.put(key, value);
}
}
}
private static void validateDuplicateKey(
Map outerMap, String paramKey, Object paramValue) {
if (outerMap.containsKey(paramKey)) {
throw new IllegalArgumentException(
String.format(
"Found multiple param values for the same param key. This can happen because you passed "
+ "additional parameters via `putExtraParam` that conflict with the existing params. "
+ "Found param key `%s` with values `%s` and `%s`. "
+ "If you wish to pass additional params for nested parameters, you "
+ "should add extra params at the nested params themselves, not from the "
+ "top-level param.",
paramKey, outerMap.get(paramKey), paramValue));
}
}
private static class NullValuesInMapsTypeAdapterFactory implements TypeAdapterFactory {
TypeAdapter> getValueAdapter(Gson gson, TypeToken> type) {
Type valueType;
if (type.getType() instanceof ParameterizedType) {
ParameterizedType mapParameterizedType = (ParameterizedType) type.getType();
valueType = mapParameterizedType.getActualTypeArguments()[1];
} else {
valueType = Object.class;
}
return gson.getAdapter(TypeToken.get(valueType));
}
@Override
public TypeAdapter create(Gson gson, TypeToken type) {
if (!Map.class.isAssignableFrom(type.getRawType())) {
return null;
}
final TypeAdapter> valueAdapter = getValueAdapter(gson, type);
final TypeAdapter delegate = gson.getDelegateAdapter(this, type);
@SuppressWarnings({"unchecked", "rawtypes"})
final TypeAdapter typeAdapter = new MapAdapter(valueAdapter, delegate);
return typeAdapter.nullSafe();
}
}
private static class MapAdapter extends TypeAdapter