com.github.karsaig.approvalcrest.matcher.GsonProvider Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2013 Shazam Entertainment Limited
*
* 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.github.karsaig.approvalcrest.matcher;
import static com.github.karsaig.approvalcrest.FieldsIgnorer.MARKER;
import static com.google.common.collect.Sets.newTreeSet;
import static org.apache.commons.lang3.ClassUtils.isPrimitiveOrWrapper;
import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.hamcrest.Matcher;
import com.github.karsaig.approvalcrest.MatcherConfiguration;
import com.github.karsaig.approvalcrest.matcher.typeadapters.ClassAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.DateAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.InstantAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.LocalDateAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.LocalDateTimeAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.LocalTimeAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.OffsetDateTimeAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.OffsetTimeAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.PathTypeAdapter;
import com.github.karsaig.approvalcrest.matcher.typeadapters.ThrowableTypeAdapterFactory;
import com.github.karsaig.approvalcrest.matcher.typeadapters.ZonedDateTimeAdapter;
import com.google.common.base.Optional;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.graph.GraphAdapterBuilder;
/**
* Provides an instance of {@link Gson}. If any class type has been ignored on the matcher, the {@link Gson} provided
* will include an {@link ExclusionStrategy} which will skip the serialisation of fields for that type.
*/
@SuppressWarnings("rawtypes")
class GsonProvider {
/**
* Returns a {@link Gson} instance containing {@link ExclusionStrategy} based on the object types to ignore during
* serialisation.
*
* @param matcherConfiguration
* @param circularReferenceTypes cater for circular referenced objects
* @return an instance of {@link Gson}
*/
public static Gson gson(MatcherConfiguration matcherConfiguration, Set> circularReferenceTypes) {
return gson(matcherConfiguration, circularReferenceTypes, null);
}
/**
* Returns a {@link Gson} instance containing {@link ExclusionStrategy} based on the object types to ignore during
* serialisation.
*
* @param matcherConfiguration
* @param circularReferenceTypes cater for circular referenced objects
* @param additionalConfig provides additional gson configuration
* @return an instance of {@link Gson}
*/
public static Gson gson(MatcherConfiguration matcherConfiguration, Set> circularReferenceTypes, GsonConfiguration additionalConfig) {
GsonBuilder gsonBuilder = initGson();
defaultGsonConfiguration(gsonBuilder, matcherConfiguration, circularReferenceTypes);
if (additionalConfig != null) {
additionalConfiguration(additionalConfig, gsonBuilder);
}
return gsonBuilder.create();
}
private static void defaultGsonConfiguration(GsonBuilder gsonBuilder, MatcherConfiguration matcherConfiguration, Set> circularReferenceTypes) {
if (!circularReferenceTypes.isEmpty()) {
registerCircularReferenceTypes(circularReferenceTypes, gsonBuilder);
}
gsonBuilder.registerTypeAdapterFactory(new ThrowableTypeAdapterFactory());
gsonBuilder.registerTypeAdapter(Optional.class, new OptionalSerializer());
gsonBuilder.registerTypeAdapterFactory(DateAdapter.FACTORY);
gsonBuilder.registerTypeAdapterFactory(ClassAdapter.FACTORY);
gsonBuilder.registerTypeAdapter(InstantAdapter.INSTANT_TYPE, new InstantAdapter());
gsonBuilder.registerTypeAdapter(LocalDateAdapter.LOCAL_DATE_TYPE, new LocalDateAdapter());
gsonBuilder.registerTypeAdapter(LocalDateTimeAdapter.LOCAL_DATE_TIME_TYPE, new LocalDateTimeAdapter());
gsonBuilder.registerTypeAdapter(LocalTimeAdapter.LOCAL_TIME_TYPE, new LocalTimeAdapter());
gsonBuilder.registerTypeAdapter(OffsetDateTimeAdapter.OFFSET_DATE_TIME_TYPE, new OffsetDateTimeAdapter());
gsonBuilder.registerTypeAdapter(OffsetTimeAdapter.OFFSET_TIME_TYPE, new OffsetTimeAdapter());
gsonBuilder.registerTypeAdapter(ZonedDateTimeAdapter.ZONED_DATE_TIME_TYPE, new ZonedDateTimeAdapter());
gsonBuilder.registerTypeAdapterFactory(PathTypeAdapter.FACTORY);
registerSetSerialisation(gsonBuilder);
registerMapSerialisation(gsonBuilder);
markSetAndMapFields(gsonBuilder);
registerExclusionStrategies(gsonBuilder, matcherConfiguration);
}
private static void additionalConfiguration(GsonConfiguration additionalConfig, GsonBuilder gsonBuilder) {
for (TypeAdapterFactory factory : additionalConfig.getTypeAdapterFactories()) {
gsonBuilder.registerTypeAdapterFactory(factory);
}
Map> typeAdapterMap = additionalConfig.getTypeAdapters();
for (Type type : typeAdapterMap.keySet()) {
if (typeAdapterMap.get(type) != null) {
for (Object o : typeAdapterMap.get(type)) {
gsonBuilder.registerTypeAdapter(type, o);
}
}
}
Map, List