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;
}
Related
gson null when not deserialize
public class Mode {
#Expose(deserialize = false)
public final List<String> list;
public Mode(List<String> list) {
this.list = list;
}
public List<String> getList() {
return list;
}
}
list only serialize not deserialize
public class Entity {
public Mode setting = new Mode(Arrays.asList("1", "2"));
}
add deserialization exclusion strategy:
Gson gson = new GsonBuilder().setPrettyPrinting().addDeserializationExclusionStrategy(new ExclusionStrategy() {
#Override
public boolean shouldSkipField(FieldAttributes f) {
Expose annotation = f.getAnnotation(Expose.class);
if (annotation == null) {
return false;
}
return !annotation.deserialize();
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}).create();
var s = """
{
"setting": {
"list": [
"1",
"2",
"3"
]
}
}
""";
System.out.println(gson.fromJson(s, Entity.class).setting.getList());
The list field of the Mode class is null when deserialize the Entity class
Perfect solution
//Step.1 get json object
JsonObject jsonObject = gson().fromJson("", JsonObject.class);
//Step.2 get all field of the entity
for (Field configField : klass.getDeclaredFields()) {
configField.setAccessible(true);
if (!jsonObject.has(configField.getName())) {
continue;
}
//Step.3 deserialize
Object setting = gson().fromJson(jsonObject.get(configField.getName()).toString(), configField.getType());
//Step.3.1 get all field of the setting
for (Field settingField : setting.getClass().getDeclaredFields()) {
settingField.setAccessible(true);
if (settingField.isAnnotationPresent(Expose.class)) {
if (!settingField.getAnnotation(Expose.class).deserialize()) {
Field declaredField = ReflectUtil.getField(setting.getClass(), settingField.getName());
//Step.3.2 use the value from entity to set the value of setting
declaredField.set(setting, ReflectUtil.getFieldValue(ReflectUtil.getFieldValue(entity, configField.getName()), settingField.getName()));
}
}
}
//Step.4 use the value from step 3 to set the value of entity
configField.set(entity, setting);
}
I am trying to apply Lifecycle Configurations on S3 bucket. Trying to apply using following JSON:
[{
"id": "tmpdelete",
"status": "Enabled",
"filter": {
"predicate": {
"prefix": "tmp"
}
},
"transitions": [{
"days": "1",
"storageClass": "GLACIER"
}],
"noncurrentVersionTransitions": [{
"days": "1",
"storageClass": "GLACIER"
}],
"expirationInDays": "2",
"noncurrentVersionExpirationInDays": "2",
"expiredObjectDeleteMarker": "true"
}]
When i am trying to map it with Rule[].class it is not working. I am using following code:
String json = above_json;
Rule[] rules = null;
Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithModifiers(Modifier.FINAL,
Modifier.TRANSIENT, Modifier.STATIC, Modifier.ABSTRACT).create();
rules = gson.fromJson(json, Rule[].class);
try {
amazonS3.setBucketLifecycleConfiguration(bucketName, new BucketLifecycleConfiguration().withRules(rules));
} catch (Exception e) {
throw e;
}
It throws error saying Failed to invoke public com.amazonaws.services.s3.model.lifecycle.LifecycleFilterPredicate() with no args. LifecycleFilterPredicate is an abstract class which implements Serializable and it doesn't have no-args contructor. How to solve this problem.?
Ok, I think I found your problem: when GSON tries to construct the objects from that json string into an actual object (or, in this case, a list of objects), the process fails because when it gets to the filter.predicate bit, it probably tries to do something like this:
LifecycleFilterPredicate predicate = new LifecycleFilterPredicate();
predicate.setPrefix("tmp");
Which doesn't work because LifecycleFilterPredicate doesn't have a public constructor without any arguments, as you've stated.
I think that, unfortunately, your only solution is to parse the JSON in a different way.
UPDATE
You'll need to make use of a GSON TypeAdapter as follows:
class LifecycleFilterPredicateAdapter extends TypeAdapter<LifecycleFilterPredicate>
{
#Override
public LifecycleFilterPredicate read(JsonReader reader)
throws IOException
{
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.beginObject();
if(!"prefix".equals(reader.nextName()))
{
return null;
}
String prefix = reader.nextString();
LifecyclePrefixPredicate predicate = new LifecyclePrefixPredicate(prefix);
reader.endObject();
return predicate;
}
#Override
public void write(JsonWriter writer, LifecycleFilterPredicate predicate)
throws IOException
{
//nothing here
}
}
...
Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithModifiers(Modifier.FINAL,
Modifier.TRANSIENT, Modifier.STATIC, Modifier.ABSTRACT)
.registerTypeAdapter(LifecycleFilterPredicate.class, new LifecycleFilterPredicateAdapter()).create();
I've tried it locally and don't get the exception anymore :)
I tried this and it worked for me
public class RuleInstanceCreator implements InstanceCreator<LifecycleFilterPredicate> {
#Override
public LifecycleFilterPredicate createInstance(Type type) {
return new LifecycleFilterPredicate() {
private static final long serialVersionUID = 1L;
#Override
public void accept(LifecyclePredicateVisitor lifecyclePredicateVisitor) {
}
};
}
}
Gson gson = new GsonBuilder()
.registerTypeAdapter(LifecycleFilterPredicate.class, new LifecycleFilterPredicateAdapter()).create();
rules = gson.fromJson(json, Rule[].class);
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();
}
}
}
I have some odd JSON like:
[
{
"type":"0",
"value":"my string"
},
{
"type":"1",
"value":42
},
{
"type":"2",
"value": {
}
}
]
Based on some field, the object in the array is a certain type.
Using Gson, my thought is to have a TypeAdapterFactory that sends delegate adapters for those certain types to a TypeAdapter, but I'm hung up on understanding a good way of reading that "type" field to know which type to create.
In the TypeAdapter,
Object read(JsonReader in) throws IOException {
String type = in.nextString();
switch (type) {
// delegate to creating certain types.
}
}
would assume the "type" field comes first in my JSON. Is there a decent way to remove that assumption?
Here is some code I wrote to handle an array of NewsFeedArticle and NewsFeedAd items in Json. Both items implement a marker interface NewsFeedItem to allow me to easily check if the TypeAdater should be used for a particular field.
public class NewsFeedItemTypeAdapterFactory implements TypeAdapterFactory {
#Override
#SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!NewsFeedItem.class.isAssignableFrom(type.getRawType())) {
return null;
}
TypeAdapter<JsonElement> jsonElementAdapter = gson.getAdapter(JsonElement.class);
TypeAdapter<NewsFeedArticle> newsFeedArticleAdapter = gson.getDelegateAdapter(this, TypeToken.get(NewsFeedArticle.class));
TypeAdapter<NewsFeedAd> newsFeedAdAdapter = gson.getDelegateAdapter(this, TypeToken.get(NewsFeedAd.class));
return (TypeAdapter<T>) new NewsFeedItemTypeAdapter(jsonElementAdapter, newsFeedArticleAdapter, newsFeedAdAdapter).nullSafe();
}
private static class NewsFeedItemTypeAdapter extends TypeAdapter<NewsFeedItem> {
private final TypeAdapter<JsonElement> jsonElementAdapter;
private final TypeAdapter<NewsFeedArticle> newsFeedArticleAdapter;
private final TypeAdapter<NewsFeedAd> newsFeedAdAdapter;
NewsFeedItemTypeAdapter(TypeAdapter<JsonElement> jsonElementAdapter,
TypeAdapter<NewsFeedArticle> newsFeedArticleAdapter,
TypeAdapter<NewsFeedAd> newsFeedAdAdapter) {
this.jsonElementAdapter = jsonElementAdapter;
this.newsFeedArticleAdapter = newsFeedArticleAdapter;
this.newsFeedAdAdapter = newsFeedAdAdapter;
}
#Override
public void write(JsonWriter out, NewsFeedItem value) throws IOException {
if (value.getClass().isAssignableFrom(NewsFeedArticle.class)) {
newsFeedArticleAdapter.write(out, (NewsFeedArticle) value);
} else if (value.getClass().isAssignableFrom(NewsFeedAd.class)) {
newsFeedAdAdapter.write(out, (NewsFeedAd) value);
}
}
#Override
public NewsFeedItem read(JsonReader in) throws IOException {
JsonObject objectJson = jsonElementAdapter.read(in).getAsJsonObject();
if (objectJson.has("Title")) {
return newsFeedArticleAdapter.fromJsonTree(objectJson);
} else if (objectJson.has("CampaignName")) {
return newsFeedAdAdapter.fromJsonTree(objectJson);
}
return null;
}
}
}
You can then register this with Gson using the following code.
return new GsonBuilder()
.registerTypeAdapterFactory(new NewsFeedItemTypeAdapterFactory())
.create();
I've got the following classes
public class MyClass {
private List<MyOtherClass> others;
}
public class MyOtherClass {
private String name;
}
And I have JSON that may look like this
{
others: {
name: "val"
}
}
or this
{
others: [
{
name: "val"
},
{
name: "val"
}
]
}
I'd like to be able to use the same MyClass for both of these JSON formats. Is there a way to do this with Gson?
I came up with an answer.
private static class MyOtherClassTypeAdapter implements JsonDeserializer<List<MyOtherClass>> {
public List<MyOtherClass> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
List<MyOtherClass> vals = new ArrayList<MyOtherClass>();
if (json.isJsonArray()) {
for (JsonElement e : json.getAsJsonArray()) {
vals.add((MyOtherClass) ctx.deserialize(e, MyOtherClass.class));
}
} else if (json.isJsonObject()) {
vals.add((MyOtherClass) ctx.deserialize(json, MyOtherClass.class));
} else {
throw new RuntimeException("Unexpected JSON type: " + json.getClass());
}
return vals;
}
}
Instantiate a Gson object like this
Type myOtherClassListType = new TypeToken<List<MyOtherClass>>() {}.getType();
Gson gson = new GsonBuilder()
.registerTypeAdapter(myOtherClassListType, new MyOtherClassTypeAdapter())
.create();
That TypeToken is a com.google.gson.reflect.TypeToken.
You can read about the solution here:
https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Gener
Thank you three-cups for the solution!
The same thing with generic type in case it's needed for multiple types:
public class SingleElementToListDeserializer<T> implements JsonDeserializer<List<T>> {
private final Class<T> clazz;
public SingleElementToListDeserializer(Class<T> clazz) {
this.clazz = clazz;
}
public List<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
List<T> resultList = new ArrayList<>();
if (json.isJsonArray()) {
for (JsonElement e : json.getAsJsonArray()) {
resultList.add(context.<T>deserialize(e, clazz));
}
} else if (json.isJsonObject()) {
resultList.add(context.<T>deserialize(json, clazz));
} else {
throw new RuntimeException("Unexpected JSON type: " + json.getClass());
}
return resultList;
}
}
And configuring Gson:
Type myOtherClassListType = new TypeToken<List<MyOtherClass>>() {}.getType();
SingleElementToListDeserializer<MyOtherClass> adapter = new SingleElementToListDeserializer<>(MyOtherClass.class);
Gson gson = new GsonBuilder()
.registerTypeAdapter(myOtherClassListType, adapter)
.create();
to share code, and to only apply the deserialization logic to specific fields:
JSON model:
public class AdminLoginResponse implements LoginResponse
{
public Login login;
public Customer customer;
#JsonAdapter(MultiOrganizationArrayOrObject.class) // <-------- look here
public RealmList<MultiOrganization> allAccounts;
}
field-specific class:
class MultiOrganizationArrayOrObject
: ArrayOrSingleObjectTypeAdapter<RealmList<MultiOrganization>,MultiOrganization>(kClass()) {
override fun List<MultiOrganization>.toTypedList() = RealmList(*this.toTypedArray())
}
abstract class:
/**
* parsed field can either be a [JSONArray] of type [Element], or an single [Element] [JSONObject].
*/
abstract class ArrayOrSingleObjectTypeAdapter<TypedList: List<Element>, Element : Any>(
private val elementKClass: KClass<Element>
) : JsonDeserializer<TypedList> {
override fun deserialize(
json: JsonElement, typeOfT: Type?, ctx: JsonDeserializationContext
): TypedList = when {
json.isJsonArray -> json.asJsonArray.map { ctx.deserialize<Element>(it, elementKClass.java) }
json.isJsonObject -> listOf(ctx.deserialize<Element>(json, elementKClass.java))
else -> throw RuntimeException("Unexpected JSON type: " + json.javaClass)
}.toTypedList()
abstract fun List<Element>.toTypedList(): TypedList
}
Building off three-cups answer, I have the following which lets the JsonArray be deserialized directly as an array.
static public <T> T[] fromJsonAsArray(Gson gson, JsonElement json, Class<T> tClass, Class<T[]> tArrClass)
throws JsonParseException {
T[] arr;
if(json.isJsonObject()){
//noinspection unchecked
arr = (T[]) Array.newInstance(tClass, 1);
arr[0] = gson.fromJson(json, tClass);
}else if(json.isJsonArray()){
arr = gson.fromJson(json, tArrClass);
}else{
throw new RuntimeException("Unexpected JSON type: " + json.getClass());
}
return arr;
}
Usage:
String response = ".......";
JsonParser p = new JsonParser();
JsonElement json = p.parse(response);
Gson gson = new Gson();
MyQuote[] quotes = GsonUtils.fromJsonAsArray(gson, json, MyQuote.class, MyQuote[].class);