org.reflections.serializers.JsonSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swagger-all Show documentation
Show all versions of swagger-all Show documentation
swagger-all is a rebundled verison of Swagger as one OSGi bundle.
The newest version!
package org.reflections.serializers;
import com.google.common.base.Supplier;
import com.google.common.collect.*;
import com.google.common.io.Files;
import com.google.gson.*;
import org.reflections.Reflections;
import org.reflections.util.Utils;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/** serialization of Reflections to json
*
* an example of produced json:
*
* {"store":{"storeMap":
* {"org.reflections.scanners.TypeAnnotationsScanner":{
* "org.reflections.TestModel$AC1":["org.reflections.TestModel$C1"],
* "org.reflections.TestModel$AC2":["org.reflections.TestModel$I3",
* ...
*
* */
public class JsonSerializer implements Serializer {
private Gson gson;
public Reflections read(InputStream inputStream) {
return getGson().fromJson(new InputStreamReader(inputStream), Reflections.class);
}
public File save(Reflections reflections, String filename) {
try {
File file = Utils.prepareFile(filename);
Files.write(toString(reflections), file, Charset.defaultCharset());
return file;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String toString(Reflections reflections) {
return getGson().toJson(reflections);
}
private Gson getGson() {
if (gson == null) {
gson = new GsonBuilder()
.registerTypeAdapter(Multimap.class, new com.google.gson.JsonSerializer() {
public JsonElement serialize(Multimap multimap, Type type, JsonSerializationContext jsonSerializationContext) {
return jsonSerializationContext.serialize(multimap.asMap());
}
})
.registerTypeAdapter(Multimap.class, new JsonDeserializer() {
public Multimap deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
final SetMultimap map = Multimaps.newSetMultimap(new HashMap>(), new Supplier>() {
public Set get() {
return Sets.newHashSet();
}
});
for (Map.Entry entry : ((JsonObject) jsonElement).entrySet()) {
for (JsonElement element : (JsonArray) entry.getValue()) {
map.get(entry.getKey()).add(element.getAsString());
}
}
return map;
}
})
.setPrettyPrinting()
.create();
}
return gson;
}
}