I am trying to convert my POJO into 2 different CSV representations.
My POJO:
#NoArgsConstructor
#AllArgsConstructor
public static class Example {
#JsonView(View.Public.class)
private String a;
#JsonView(View.Public.class)
private String b;
#JsonView(View.Internal.class)
private String c;
#JsonView(View.Internal.class)
private String d;
public static final class View {
interface Public {}
interface Internal extends Public {}
}
}
Public view exposed fields a and b, and Internal view exposes all fields.
The problem is that if I construct the ObjectWriter with .writerWithSchemaFor(Example.class) all my fields are included but ignored as defined by the view. ObjectWriter will create the schema as defined by the Example.class but if I apply .withView it will only hide the fields, not ignore them.
This means that I must construct the schema manually.
Tests:
#Test
public void testJson() throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
final Example example = new Example("1", "2", "3", "4");
final String result = mapper.writerWithView(Example.View.Public.class).writeValueAsString(example);
System.out.println(result); // {"a":"1","b":"2"}
}
#Test
public void testCsv() throws JsonProcessingException {
final CsvMapper mapper = new CsvMapper();
final Example example = new Example("1", "2", "3", "4");
final String result = mapper.writerWithSchemaFor(Example.class).withView(Example.View.Public.class).writeValueAsString(example);
System.out.println(result); // 1,2,,
}
#Test
public void testCsvWithCustomSchema() throws JsonProcessingException {
final CsvMapper mapper = new CsvMapper();
CsvSchema schema = CsvSchema.builder()
.addColumn("a")
.addColumn("b")
.build();
final Example example = new Example("1", "2", "3", "4");
final String result = mapper.writer().with(schema).withView(Example.View.Public.class).writeValueAsString(example);
System.out.println(result); // 1,2
}
testCsv test has 4 fields, but 2 are excluded. testCsvWithCustomSchema test has only the fields I want.
Is there a way to get CsvSchema that will match my #JsonView without having to construct it myself?
Here is a solution I did with reflection, I am not really happy with it since it is still "manually" building the schema.
This solution is also bad since it ignores mapper configuration like MapperFeature.DEFAULT_VIEW_INCLUSION.
This seems like doing something that should be already available from the library.
#AllArgsConstructor
public class GenericPojoCsvSchemaBuilder {
public CsvSchema build(final Class<?> type) {
return build(type, null);
}
public CsvSchema build(final Class<?> type, final Class<?> view) {
return build(CsvSchema.builder(), type, view);
}
public CsvSchema build(final CsvSchema.Builder builder, final Class<?> type) {
return build(builder, type, null);
}
public CsvSchema build(final CsvSchema.Builder builder, final Class<?> type, final Class<?> view) {
final JsonPropertyOrder propertyOrder = type.getAnnotation(JsonPropertyOrder.class);
final List<Field> fieldsForView;
// DO NOT use Arrays.asList because it uses an internal fixed length implementation which cannot use .removeAll (throws UnsupportedOperationException)
final List<Field> unorderedFields = Arrays.stream(type.getDeclaredFields()).collect(Collectors.toList());
if (propertyOrder != null && propertyOrder.value().length > 0) {
final List<Field> orderedFields = Arrays.stream(propertyOrder.value()).map(s -> {
try {
return type.getDeclaredField(s);
} catch (final NoSuchFieldException e) {
throw new IllegalArgumentException(e);
}
}).collect(Collectors.toList());
if (propertyOrder.value().length < type.getDeclaredFields().length) {
unorderedFields.removeAll(orderedFields);
orderedFields.addAll(unorderedFields);
}
fieldsForView = getJsonViewFields(orderedFields, view);
} else {
fieldsForView = getJsonViewFields(unorderedFields ,view);
}
final JsonIgnoreFieldFilter ignoreFieldFilter = new JsonIgnoreFieldFilter(type.getDeclaredAnnotation(JsonIgnoreProperties.class));
fieldsForView.forEach(field -> {
if (ignoreFieldFilter.matches(field)) {
builder.addColumn(field.getName());
}
});
return builder.build();
}
private List<Field> getJsonViewFields(final List<Field> fields, final Class<?> view) {
if (view == null) {
return fields;
}
return fields.stream()
.filter(field -> {
final JsonView jsonView = field.getAnnotation(JsonView.class);
return jsonView != null && Arrays.stream(jsonView.value()).anyMatch(candidate -> candidate.isAssignableFrom(view));
})
.collect(Collectors.toList());
}
private class JsonIgnoreFieldFilter implements ReflectionUtils.FieldFilter {
private final List<String> fieldNames;
public JsonIgnoreFieldFilter(final JsonIgnoreProperties jsonIgnoreProperties) {
if (jsonIgnoreProperties != null) {
fieldNames = Arrays.asList(jsonIgnoreProperties.value());
} else {
fieldNames = null;
}
}
#Override
public boolean matches(final Field field) {
if (fieldNames != null && fieldNames.contains(field.getName())) {
return false;
}
final JsonIgnore jsonIgnore = field.getDeclaredAnnotation(JsonIgnore.class);
return jsonIgnore == null || !jsonIgnore.value();
}
}
}
Related
I have a message in JSON format that I converted to a JSONObject, and I have around 30 mandatory fields that I have to check for whether they're null or not. If one of these mandatory fields are null, I will discard the message, however other fields can be null without needing to discard the message. Is there any efficient way I can do this without going through each and every field and using isNull() ?
Also, the JSON objects are nested, so a simple anyNull() function would not work since it would only return if the object itself is null and not if the variables themselves are null.
I tried using gson to convert the message to a POJO, and created classes for 10 objects
Gson gson = new Gson();
Message message = gson.fromJson(msg, Message.class);
but since many classes are nested (and one of which is an array of objects) using simple null checkers don't work.
Actually speaking your question is not very clear because you're using a word of "message" that refers your particular class, but can also be more generic referring sent/received messages.
So something like for JSON elements in memory:
public static void failOnNullRecursively(final JsonElement jsonElement) {
if ( jsonElement.isJsonNull() ) {
throw new IllegalArgumentException("null!");
}
if ( jsonElement.isJsonPrimitive() ) {
return;
}
if ( jsonElement.isJsonArray() ) {
for ( final JsonElement element : jsonElement.getAsJsonArray() ) {
failOnNullRecursively(element);
}
return;
}
if ( jsonElement.isJsonObject() ) {
for ( final Map.Entry<String, JsonElement> e : jsonElement.getAsJsonObject().entrySet() ) {
failOnNullRecursively(e.getValue());
}
return;
}
throw new AssertionError(jsonElement);
}
or JSON documents in streams:
public final class FailOnNullJsonReader
extends JsonReader {
private FailOnNullJsonReader(final Reader reader) {
super(reader);
}
public static JsonReader create(final Reader reader) {
return new FailOnNullJsonReader(reader);
}
#Override
public void nextNull() {
throw new IllegalStateException(String.format("null at %#!", getPath()));
}
}
Both of them will throw on null. But it also seems that you want to validate Message instances:
If one of these mandatory fields are null, I will discard the message, however other fields can be null without needing to discard the message.
So this tells why the above null-checks won't fit your needs. What you're looking for is JSR-303. It won't be that efficient as you might want to want it to be (message instances are deserialized, validation takes time and resources too), but it might be efficient from the coding perspective:
final Set<ConstraintViolation<V>> violations = validator.validate(message);
if ( !violations.isEmpty() ) {
throw new ConstraintViolationException(violations);
}
or even integrate it right into Gson so that it serves middleware:
public final class PostReadTypeAdapterFactory<V>
implements TypeAdapterFactory {
private final Predicate<? super TypeToken<?>> supports;
private final BiConsumer<? super TypeToken<V>, ? super V> onRead;
private PostReadTypeAdapterFactory(final Predicate<? super TypeToken<?>> supports, final BiConsumer<? super TypeToken<V>, ? super V> onRead) {
this.supports = supports;
this.onRead = onRead;
}
public static <V> TypeAdapterFactory create(final Predicate<? super TypeToken<?>> supports, final BiConsumer<? super TypeToken<V>, ? super V> onRead) {
return new PostReadTypeAdapterFactory<>(supports, onRead);
}
#Override
#Nullable
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
if ( !supports.test(typeToken) ) {
return null;
}
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, typeToken);
return new TypeAdapter<T>() {
#Override
public void write(final JsonWriter out, final T value)
throws IOException {
delegate.write(out, value);
}
#Override
public T read(final JsonReader in)
throws IOException {
final T readValue = delegate.read(in);
#SuppressWarnings("unchecked")
final V value = (V) readValue;
#SuppressWarnings("unchecked")
final TypeToken<V> valueTypeToken = (TypeToken<V>) typeToken;
onRead.accept(valueTypeToken, value);
return readValue;
}
};
}
}
public final class Jsr303Support {
private Jsr303Support() {
}
public static <V> TypeAdapterFactory createTypeAdapterFactory(final Validator validator) {
return PostReadTypeAdapterFactory.<V>create(
typeToken -> typeToken.getRawType().isAnnotationPresent(Validate.class),
(typeToken, value) -> {
final Set<ConstraintViolation<V>> violations = validator.validate(value);
if ( !violations.isEmpty() ) {
throw new ConstraintViolationException(violations);
}
}
);
}
}
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface Validate {
}
And the test (using Lombok for brevity):
#Validate
#AllArgsConstructor
#EqualsAndHashCode
#ToString
final class Message {
#NotNull
final String foo;
#NotNull
final String bar;
#NotNull
final String baz;
}
public final class Jsr303SupportTest {
private static final Validator validator;
static {
try ( final ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory() ) {
validator = validatorFactory.getValidator();
}
}
public static final Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.disableInnerClassSerialization()
.registerTypeAdapterFactory(Jsr303Support.createTypeAdapterFactory(validator))
.create();
#Test
public void test() {
Assertions.assertEquals(new Message("1", "2", "3"), gson.fromJson("{\"foo\":\"1\",\"bar\":\"2\",\"baz\":\"3\"}", Message.class));
final ConstraintViolationException ex = Assertions.assertThrows(ConstraintViolationException.class, () -> gson.fromJson("{\"foo\":\"1\",\"bar\":null,\"baz\":\"3\"}", Message.class));
Assertions.assertEquals(1, ex.getConstraintViolations().size());
}
}
And finally, probably the most efficient (in terms of reading JSON stream), but very limited whencompared to JSR-303 (and NOT working in Gson because Gson does not propagate null-checking to downstream (de)serializers), way that could replace #NotNull with a similar "functional" annotation:
public final class NotNullTypeAdapterFactory
implements TypeAdapterFactory {
// note no external access
private NotNullTypeAdapterFactory() {
}
#Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
final TypeAdapter<T> delegate = gson.getAdapter(typeToken);
return new TypeAdapter<T>() {
#Override
public void write(final JsonWriter out, #Nullable final T value)
throws IOException {
if ( value == null ) {
throw new IllegalArgumentException(typeToken + " with null");
}
delegate.write(out, value);
}
#Override
public T read(final JsonReader in)
throws IOException {
#Nullable
final T value = delegate.read(in);
if ( value == null ) {
throw new IllegalArgumentException(typeToken + " with null at " + in.getPath());
}
return value;
}
};
}
}
#AllArgsConstructor
#EqualsAndHashCode
#ToString
final class Message {
#JsonAdapter(NotNullTypeAdapterFactory.class)
final String foo;
#JsonAdapter(NotNullTypeAdapterFactory.class)
final String bar;
#JsonAdapter(NotNullTypeAdapterFactory.class)
final String baz;
}
public final class NotNullTypeAdapterFactoryTest {
public static final Gson gson = new GsonBuilder()
.disableHtmlEscaping()
.disableInnerClassSerialization()
.create();
#Test
public void test() {
Assertions.assertEquals(new Message("1", "2", "3"), gson.fromJson("{\"foo\":\"1\",\"bar\":\"2\",\"baz\":\"3\"}", Message.class));
final IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class, () -> gson.fromJson("{\"foo\":\"1\",\"bar\":null,\"baz\":\"3\"}", Message.class));
Assertions.assertEquals("whatever here, the above does not work anyway", ex.getMessage());
}
}
The third, JSR-303, looks like the best for you.
I want to keep a part of a JSON as String value.
As far as i know, there is no way with Annotations, but i could not find a way how to get the full Object/Array value as String.
There is a Workaround, which works, by reading it as an Object and instantly write it back as an String by using the ObjectMapper of Jackson.
You can imagine, this is a horrible solution for very big JSONs.
public class DeserializeTest {
private static ObjectMapper mapper;
public static void main(String[] args) throws IOException {
mapper = Jackson2ObjectMapperBuilder.json().build();
mapper.findAndRegisterModules();
SimpleModule module = new SimpleModule();
module.addDeserializer(TestClassWrapper.class, new TestDeserializer());
mapper.registerModule(module);
String json = "{\"name\":\"testprop\", \"data\":[{\"prop\":\"test\"},{\"prop\":\"test1\"},{\"prop\":\"test2\"}]}";
TestClassWrapper t = mapper.readValue(json, TestClassWrapper.class);
// later in program, when i know the expected class
TestClass o = unwrap(t, new TypeReference<ArrayList<Test2>>() {});
}
public static class TestClassWrapper {
String name;
String data;
// removed getter and setter
}
public static class TestClass {
String name;
List<Test2> data;
// removed getter and setter
}
public static class Test2 {
String prop;
// removed getter and setter
}
public static class TestDeserializer extends JsonDeserializer<TestClassWrapper> {
#Override
public TestClassWrapper deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
TestClassWrapper t = new TestClassWrapper();
String key = p.getCurrentName();
if (key == null) {
p.nextToken();
key = p.getCurrentName();
}
for (; key != null; key = p.nextFieldName()) {
p.nextToken();
switch (key) {
case "name":
t.name = p.getValueAsString();
break;
case "data":
// what i tried:
System.out.println(p.getText()); // [
System.out.println(p.getValueAsString()); // NULL
System.out.println(p.getCurrentValue()); //NULL
System.out.println(p.getCurrentToken()); // [ TOKEN
System.out.println(p.getParsingContext().getCurrentValue()); // NULL
System.out.println(p.getParsingContext().toString()); // [0]
System.out.println(p.getEmbeddedObject()); // NULL
System.out.println(p.getTextCharacters()); // [
try {
System.out.println(ctxt.readValue(p, String.class)); // MismatchedInputException
} catch (MismatchedInputException e){}
// The only way i could make it work.
// Parse to a object and write it back as string.
StringBuilder sb = new StringBuilder();
Iterator<Object> it = p.readValuesAs(Object.class);
while (it.hasNext()) {
sb.append(mapper.writeValueAsString(it.next()));
sb.append(it.hasNext() ? "," : "");
}
t.data = p.getCurrentToken() == JsonToken.END_ARRAY ? "[" + sb.toString() + "]" : sb.toString();
break;
}
}
return t;
}
}
public static TestClass unwrap(TestClassWrapper t, TypeReference targetClass) throws IOException {
TestClass o = new TestClass();
o.name = t.name;
o.data = mapper.readValue(t.data, targetClass);
return o;
}
}
How can i tell the JsonParser object, to just give me the String of the current value?
(For data this would be: "[{"prop":"test"}, {"prop":"test1"}, {"prop":"test2"}]")
I've been using Gson for a few weeks and I've discovered the Runtime Type Adapter Factory class that allows to "adapt values whose runtime type may differ from their declaration type".
Here's my current code using Gson:
public class Database {
private final Gson gson;
private Database() {
// Initialize Gson
RuntimeTypeAdapterFactory<Base> runtimeTypeAdapterFactory = RuntimeTypeAdapterFactory
.of(Base.class, "table")
.registerSubtype(AdminsTbl.class, "admins");
this.gson = new GsonBuilder().registerTypeAdapterFactory(runtimeTypeAdapterFactory).create();
}
}
I have a "table" field in my JSON that tells Gson which class to use (in this case: "admins" -> AdminsTbl.class). Is there something like this in Moshi?
In fact, Gson does not provide RuntimeTypeAdapterFactory per se in its standard bundle. Quick googling for a Moshi implementation brings your question at the top of the search results, at least for me. :) I also couldn't find anything like that in the com.squareup.moshi.recipes package. But you can easily implement it yourself:
public final class MoshiRuntimeTypeJsonAdapterFactory
implements JsonAdapter.Factory {
private static final String DEFAULT_CLASS_NAME_PROPERTY = "type";
private final Class<?> baseClass;
private final String classNameProperty;
private final Map<String, Class<?>> classNameToClass = new HashMap<>();
private final Map<Class<?>, String> classToClassName = new HashMap<>();
private MoshiRuntimeTypeJsonAdapterFactory(final Class<?> baseClass, final String classNameProperty) {
this.baseClass = baseClass;
this.classNameProperty = classNameProperty;
}
public static MoshiRuntimeTypeJsonAdapterFactory of(final Class<?> expectedClass) {
return new MoshiRuntimeTypeJsonAdapterFactory(expectedClass, DEFAULT_CLASS_NAME_PROPERTY);
}
public static MoshiRuntimeTypeJsonAdapterFactory of(final Class<?> expectedClass, final String classNameProperty) {
return new MoshiRuntimeTypeJsonAdapterFactory(expectedClass, classNameProperty);
}
public MoshiRuntimeTypeJsonAdapterFactory with(final Class<?> concreteClass) {
return with(concreteClass, concreteClass.getSimpleName());
}
public MoshiRuntimeTypeJsonAdapterFactory with(final Class<?> concreteClass, final String className)
throws IllegalArgumentException {
if ( classNameToClass.containsKey(className) ) {
throw new IllegalArgumentException(className + " is already registered for " + concreteClass);
}
if ( classToClassName.containsKey(concreteClass) ) {
throw new IllegalArgumentException(concreteClass + " is already registered for " + className);
}
classNameToClass.put(className, concreteClass);
classToClassName.put(concreteClass, className);
return this;
}
#Nullable
#Override
public JsonAdapter<?> create(final Type type, final Set<? extends Annotation> annotations, final Moshi moshi) {
if ( !(type instanceof Class) ) {
return null;
}
final Class<?> typeAsClass = (Class<?>) type;
if ( !baseClass.isAssignableFrom(typeAsClass) ) {
return null;
}
final JsonAdapter<Object> jsonObjectJsonAdapter = moshi.nextAdapter(this, Map.class, ImmutableSet.of());
final LoadingCache<Class<?>, JsonAdapter<Object>> jsonAdaptersCache = CacheBuilder.newBuilder()
.build(new CacheLoader<Class<?>, JsonAdapter<Object>>() {
#Override
public JsonAdapter<Object> load(final Class<?> clazz) {
return moshi.nextAdapter(MoshiRuntimeTypeJsonAdapterFactory.this, clazz, ImmutableSet.copyOf(clazz.getAnnotations()));
}
});
return new JsonAdapter<Object>() {
#Nullable
#Override
public Object fromJson(final JsonReader jsonReader)
throws IOException {
try {
#SuppressWarnings("unchecked")
final Map<String, Object> jsonObject = (Map<String, Object>) jsonReader.readJsonValue();
assert jsonObject != null;
final Object rawClassName = jsonObject.get(classNameProperty);
if ( !(rawClassName instanceof String) ) {
throw new IOException("Type name: expected a string in " + classNameProperty + ", but got " + rawClassName);
}
final String className = (String) rawClassName;
final Class<?> concreteClass = classNameToClass.get(className);
if ( concreteClass == null ) {
throw new IOException("No mapping registered for " + className);
}
final JsonAdapter<Object> jsonAdapter = jsonAdaptersCache.get(concreteClass);
return jsonAdapter.fromJsonValue(jsonObject);
} catch ( final ExecutionException ex ) {
throw new RuntimeException(ex);
}
}
#Override
public void toJson(final JsonWriter jsonWriter, #Nullable final Object value)
throws IOException {
try {
assert value != null;
final Class<?> concreteClass = value.getClass();
final String className = classToClassName.get(concreteClass);
if ( className == null ) {
throw new IOException("No mapping registered for " + concreteClass);
}
final JsonAdapter<Object> valueJsonAdapter = jsonAdaptersCache.get(concreteClass);
#SuppressWarnings("unchecked")
final Map<String, Object> jsonObject = (Map<String, Object>) valueJsonAdapter.toJsonValue(value);
assert jsonObject != null;
jsonObject.put(classNameProperty, className);
jsonObjectJsonAdapter.toJson(jsonWriter, jsonObject);
} catch ( final ExecutionException ex ) {
throw new RuntimeException(ex);
}
}
};
}
}
This implementation makes some use of Google Guava for immutable collections (ImmutableSet) and caching (LoadingCache), but you can easily replace them on your own. I also believe this implementation can be improved for potential Moshi-related performance issues as well.
The trivial example from RuntimeTypeAdapterFactory as seen here adaptation:
private static final Moshi moshi = new Moshi.Builder()
.add(MoshiRuntimeTypeJsonAdapterFactory.of(Shape.class)
.with(Shape.Circle.class)
.with(Shape.Diamond.class)
.with(Shape.Rectangle.class)
)
.build();
Is there a way to setup a Moshi adapter to automatically create a single Object or List<Object> based on the JSON response? Currently, I can do this explicitly. For example, I can receive the following responses:
{
"userId": "1",
"id": "2",
"body": "body...",
"title": "title..."
}
Or
[
{
"userId": "1",
"id": "2",
"body": "body...",
"title": "title..."
}
]
And I would like to create Object or List<Object> without having to explicitly specify which one to use.
You can use a JsonQualifier to generalize this.
From your example, you might use it like
final class Foo {
#SingleToArray final List<User> users;
}
Here's the code with a test to demonstrate more thouroughly.
#Retention(RUNTIME)
#JsonQualifier public #interface SingleToArray {
final class Adapter extends JsonAdapter<List<Object>> {
final JsonAdapter<List<Object>> delegateAdapter;
final JsonAdapter<Object> elementAdapter;
public static final Factory FACTORY = new Factory() {
#Nullable #Override
public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations,
Moshi moshi) {
Set<? extends Annotation> delegateAnnotations =
Types.nextAnnotations(annotations, SingleToArray.class);
if (delegateAnnotations == null) {
return null;
}
if (Types.getRawType(type) != List.class) {
throw new IllegalArgumentException(
"Only lists may be annotated with #SingleToArray. Found: " + type);
}
Type elementType = Types.collectionElementType(type, List.class);
JsonAdapter<List<Object>> delegateAdapter = moshi.adapter(type, delegateAnnotations);
JsonAdapter<Object> elementAdapter = moshi.adapter(elementType);
return new Adapter(delegateAdapter, elementAdapter);
}
};
Adapter(JsonAdapter<List<Object>> delegateAdapter, JsonAdapter<Object> elementAdapter) {
this.delegateAdapter = delegateAdapter;
this.elementAdapter = elementAdapter;
}
#Nullable #Override public List<Object> fromJson(JsonReader reader) throws IOException {
if (reader.peek() != JsonReader.Token.BEGIN_ARRAY) {
return Collections.singletonList(elementAdapter.fromJson(reader));
}
return delegateAdapter.fromJson(reader);
}
#Override public void toJson(JsonWriter writer, #Nullable List<Object> value)
throws IOException {
if (value.size() == 1) {
elementAdapter.toJson(writer, value.get(0));
} else {
delegateAdapter.toJson(writer, value);
}
}
}
}
#Test public void singleToArray() throws Exception {
Moshi moshi = new Moshi.Builder().add(SingleToArray.Adapter.FACTORY).build();
JsonAdapter<List<String>> adapter =
moshi.adapter(Types.newParameterizedType(List.class, String.class), SingleToArray.class);
assertThat(adapter.fromJson("[\"Tom\",\"Huck\"]")).isEqualTo(Arrays.asList("Tom", "Huck"));
assertThat(adapter.toJson(Arrays.asList("Tom", "Huck"))).isEqualTo("[\"Tom\",\"Huck\"]");
assertThat(adapter.fromJson("\"Jim\"")).isEqualTo(Collections.singletonList("Jim"));
assertThat(adapter.toJson(Collections.singletonList("Jim"))).isEqualTo("\"Jim\"");
assertThat(adapter.fromJson("[]")).isEqualTo(Collections.emptyList());
assertThat(adapter.toJson(Collections.<String>emptyList())).isEqualTo("[]");
}
Using #Eric's comment, I came up with the correct code below:
public static <T> List<T> loadFakeData(String url, Class<T> cls){
List<T> list = new ArrayList<>();
Moshi moshi = new Moshi.Builder().build();
try {
JsonReader reader = JsonReader.of(runHttpClient(url));
JsonReader.Token token = reader.peek();
if (token.equals(JsonReader.Token.BEGIN_ARRAY)) {
Type type = Types.newParameterizedType(List.class, cls);
JsonAdapter<List<T>> adapter = moshi.adapter(type);
list = adapter.fromJson(reader);
} else if (token.equals(JsonReader.Token.BEGIN_OBJECT)){
JsonAdapter<T> adapter = moshi.adapter(cls);
T t = adapter.fromJson(reader);
list.add(t);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
I'm trying to create an abstract class for defining configuration classes. I wish to export and import these classes from and to JSON whenever I want to. I'm trying to achieve this using Gson.
I'm getting an error when writing to JSON that states it:
can't serialize java.lang.Class - Forgot to register a type adapter?
My main class: https://hastebin.com/pogohodovi.scala
Abstract config class: https://hastebin.com/adeyawubuy.cs
An example of a child class:
public class DyescapeCOREConfiguration extends DyescapeConfiguration {
private static transient DyescapeCOREConfiguration i = new DyescapeCOREConfiguration();
public static DyescapeCOREConfiguration get() { return i; }
#Expose public static String ServerID = UUID.randomUUID().toString();
}
Please note: I need to keep the variables in the child configuration classes static. I tried to create some adapters/serializers, but they don't seem to work.
You're probably doing:
gson.toJson(DyescapeCOREConfiguration.class)
In order to serialize this class, you still must create an instance of DyescapeCOREConfiguration. Since statics are not (de)serialized by default, you have to enable them (IMHO, enabling such modifier is really not a good idea):
final Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.excludeFieldsWithModifiers(TRANSIENT) // STATIC|TRANSIENT in the default configuration
.create();
final String json = gson.toJson(new DyescapeCOREConfiguration());
System.out.println(json);
The output:
{"ServerID":"37145480-64b9-4beb-b031-2d619f14a44b"}
Update
If obtaining an instance is not possible for whatever reason, write a custom Class<?> type adapter (I would never use it in practice):
StaticTypeAdapterFactory.java
final class StaticTypeAdapterFactory
implements TypeAdapterFactory {
private static final TypeAdapterFactory staticTypeAdapterFactory = new StaticTypeAdapterFactory();
private StaticTypeAdapterFactory() {
}
static TypeAdapterFactory getStaticTypeAdapterFactory() {
return staticTypeAdapterFactory;
}
#Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
final Type type = typeToken.getType();
if ( type.equals(Class.class) ) {
#SuppressWarnings("unchecked")
final TypeAdapter<T> castStaticTypeAdapter = (TypeAdapter<T>) getStaticTypeAdapter(gson);
return castStaticTypeAdapter;
}
return null;
}
}
StaticTypeAdapter.java
final class StaticTypeAdapter<T>
extends TypeAdapter<Class<T>> {
private static final String TARGET_CLASS_PROPERTY = "___class";
private final Gson gson;
private StaticTypeAdapter(final Gson gson) {
this.gson = gson;
}
static <T> TypeAdapter<Class<T>> getStaticTypeAdapter(final Gson gson) {
return new StaticTypeAdapter<>(gson);
}
#Override
#SuppressWarnings("resource")
public void write(final JsonWriter out, final Class<T> value)
throws IOException {
try {
final Iterator<Field> iterator = Stream.of(value.getFields())
.filter(f -> isStatic(f.getModifiers()))
.iterator();
out.beginObject();
while ( iterator.hasNext() ) {
final Field field = iterator.next();
out.name(field.getName());
field.setAccessible(true);
final Object fieldValue = field.get(null);
#SuppressWarnings({ "unchecked", "rawtypes" })
final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType());
adapter.write(out, fieldValue);
}
out.name(TARGET_CLASS_PROPERTY);
out.value(value.getName());
out.endObject();
} catch ( final IllegalAccessException ex ) {
throw new IOException(ex);
}
}
#Override
public Class<T> read(final JsonReader in)
throws IOException {
try {
Class<?> type = null;
in.beginObject();
final Map<String, JsonElement> buffer = new HashMap<>();
while ( in.peek() != END_OBJECT ) {
final String property = in.nextName();
switch ( property ) {
case TARGET_CLASS_PROPERTY:
type = Class.forName(in.nextString());
break;
default:
// buffer until the target class name is known
if ( type == null ) {
final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class);
final JsonElement jsonElement = adapter.read(in);
buffer.put(property, jsonElement);
} else {
// flush the buffer
if ( !buffer.isEmpty() ) {
for ( final Entry<String, JsonElement> e : buffer.entrySet() ) {
final Field field = type.getField(e.getKey());
final Object value = gson.getAdapter(field.getType()).read(in);
field.set(null, value);
}
buffer.clear();
}
final Field field = type.getField(property);
if ( isStatic(field.getModifiers()) ) {
final TypeAdapter<?> adapter = gson.getAdapter(field.getType());
final Object value = adapter.read(in);
field.set(null, value);
}
}
break;
}
}
in.endObject();
// flush the buffer
if ( type != null && !buffer.isEmpty() ) {
for ( final Entry<String, JsonElement> e : buffer.entrySet() ) {
final Field field = type.getField(e.getKey());
final Object value = gson.fromJson(e.getValue(), field.getType());
field.set(null, value);
}
buffer.clear();
}
#SuppressWarnings({ "unchecked", "rawtypes" })
final Class<T> castType = (Class) type;
return castType;
} catch ( final ClassNotFoundException | NoSuchFieldException | IllegalAccessException ex ) {
throw new IOException(ex);
}
}
}
Example use:
final Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(getStaticTypeAdapterFactory())
.create();
final String json = gson.toJson(DyescapeCOREConfiguration.class);
out.println("DyescapeCOREConfiguration.ServerID=" + DyescapeCOREConfiguration.ServerID);
// ---
DyescapeCOREConfiguration.ServerID = "whatever";
out.println("DyescapeCOREConfiguration.ServerID=" + DyescapeCOREConfiguration.ServerID);
// ---
#SuppressWarnings("unchecked")
final Class<DyescapeCOREConfiguration> configurationClass = gson.fromJson(json, Class.class);
// ^--- this is awful, omitting a useless assignment is even worse
out.println("DyescapeCOREConfiguration.ServerID=" + DyescapeCOREConfiguration.ServerID);
Output:
DyescapeCOREConfiguration.ServerID=012fa795-abd8-4b91-b6f5-bab67f73ae17
DyescapeCOREConfiguration.ServerID=whatever
DyescapeCOREConfiguration.ServerID=012fa795-abd8-4b91-b6f5-bab67f73ae17
However, I still recommend you to avoid the idea of static fields (de)serialization.