forked from commons-app/apps-android-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
maskara
committed
Jan 21, 2018
1 parent
d78c7be
commit 28a6a3b
Showing
32 changed files
with
1,013 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/fr/free/nrw/commons/json/GsonMarshaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fr.free.nrw.commons.json; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import fr.free.nrw.commons.network.GsonUtil; | ||
|
||
public final class GsonMarshaller { | ||
public static String marshal(@Nullable Object object) { | ||
return marshal(GsonUtil.getDefaultGson(), object); | ||
} | ||
|
||
public static String marshal(@NonNull Gson gson, @Nullable Object object) { | ||
return gson.toJson(object); | ||
} | ||
|
||
private GsonMarshaller() { } | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/fr/free/nrw/commons/json/GsonUnmarshaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package fr.free.nrw.commons.json; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import fr.free.nrw.commons.network.GsonUtil; | ||
|
||
public final class GsonUnmarshaller { | ||
/** @return Unmarshalled object. */ | ||
public static <T> T unmarshal(Class<T> clazz, @Nullable String json) { | ||
return unmarshal(GsonUtil.getDefaultGson(), clazz, json); | ||
} | ||
|
||
/** @return Unmarshalled collection of objects. */ | ||
public static <T> T unmarshal(TypeToken<T> typeToken, @Nullable String json) { | ||
return unmarshal(GsonUtil.getDefaultGson(), typeToken, json); | ||
} | ||
|
||
/** @return Unmarshalled object. */ | ||
public static <T> T unmarshal(@NonNull Gson gson, Class<T> clazz, @Nullable String json) { | ||
return gson.fromJson(json, clazz); | ||
} | ||
|
||
/** @return Unmarshalled collection of objects. */ | ||
public static <T> T unmarshal(@NonNull Gson gson, TypeToken<T> typeToken, @Nullable String json) { | ||
// From the manual: "Fairly hideous... Unfortunately, no way to get around this in Java". | ||
return gson.fromJson(json, typeToken.getType()); | ||
} | ||
|
||
private GsonUnmarshaller() { } | ||
} | ||
|
||
|
95 changes: 95 additions & 0 deletions
95
app/src/main/java/fr/free/nrw/commons/json/RequiredFieldsCheckOnReadTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package fr.free.nrw.commons.json; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.util.ArraySet; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonParseException; | ||
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 java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import fr.free.nrw.commons.json.annotations.Required; | ||
|
||
/** | ||
* TypeAdapterFactory that provides TypeAdapters that return null values for objects that are | ||
* missing fields annotated with @Required. | ||
* | ||
* BEWARE: This means that a List or other Collection of objects that have @Required fields can | ||
* contain null elements after deserialization! | ||
* | ||
* TODO: Handle null values in lists during deserialization, perhaps with a new @RequiredElements | ||
* annotation and another corresponding TypeAdapter(Factory). | ||
*/ | ||
class RequiredFieldsCheckOnReadTypeAdapterFactory implements TypeAdapterFactory { | ||
@Nullable @Override public final <T> TypeAdapter<T> create(@NonNull Gson gson, @NonNull TypeToken<T> typeToken) { | ||
Class<?> rawType = typeToken.getRawType(); | ||
Set<Field> requiredFields = collectRequiredFields(rawType); | ||
|
||
if (requiredFields.isEmpty()) { | ||
return null; | ||
} | ||
|
||
setFieldsAccessible(requiredFields, true); | ||
return new Adapter<>(gson.getDelegateAdapter(this, typeToken), requiredFields); | ||
} | ||
|
||
@NonNull private Set<Field> collectRequiredFields(@NonNull Class<?> clazz) { | ||
Field[] fields = clazz.getDeclaredFields(); | ||
Set<Field> required = new ArraySet<>(); | ||
for (Field field : fields) { | ||
if (field.isAnnotationPresent(Required.class)) { | ||
required.add(field); | ||
} | ||
} | ||
return Collections.unmodifiableSet(required); | ||
} | ||
|
||
private void setFieldsAccessible(Iterable<Field> fields, boolean accessible) { | ||
for (Field field : fields) { | ||
field.setAccessible(accessible); | ||
} | ||
} | ||
|
||
private static final class Adapter<T> extends TypeAdapter<T> { | ||
@NonNull private final TypeAdapter<T> delegate; | ||
@NonNull private final Set<Field> requiredFields; | ||
|
||
private Adapter(@NonNull TypeAdapter<T> delegate, @NonNull final Set<Field> requiredFields) { | ||
this.delegate = delegate; | ||
this.requiredFields = requiredFields; | ||
} | ||
|
||
@Override public void write(JsonWriter out, T value) throws IOException { | ||
delegate.write(out, value); | ||
} | ||
|
||
@Override @Nullable public T read(JsonReader in) throws IOException { | ||
T deserialized = delegate.read(in); | ||
return allRequiredFieldsPresent(deserialized, requiredFields) ? deserialized : null; | ||
} | ||
|
||
private boolean allRequiredFieldsPresent(@NonNull T deserialized, | ||
@NonNull Set<Field> required) { | ||
for (Field field : required) { | ||
try { | ||
if (field.get(deserialized) == null) { | ||
return false; | ||
} | ||
} catch (IllegalArgumentException | IllegalAccessException e) { | ||
throw new JsonParseException(e); | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/fr/free/nrw/commons/json/UriTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package fr.free.nrw.commons.json; | ||
|
||
import android.net.Uri; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
|
||
public class UriTypeAdapter extends TypeAdapter<Uri> { | ||
@Override | ||
public void write(JsonWriter out, Uri value) throws IOException { | ||
out.value(value.toString()); | ||
} | ||
|
||
@Override | ||
public Uri read(JsonReader in) throws IOException { | ||
String url = in.nextString(); | ||
return Uri.parse(url); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/fr/free/nrw/commons/json/annotations/Required.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package fr.free.nrw.commons.json.annotations; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
|
||
/** | ||
* Annotate fields in Retrofit POJO classes with this to enforce their presence in order to return | ||
* an instantiated object. | ||
* | ||
* E.g.: @NonNull @Required private String title; | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(FIELD) | ||
public @interface Required { | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
app/src/main/java/fr/free/nrw/commons/mwapi/BaseModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package fr.free.nrw.commons.mwapi; | ||
|
||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
public abstract class BaseModel { | ||
@Override public String toString() { | ||
return ToStringBuilder.reflectionToString(this); | ||
} | ||
|
||
@Override public int hashCode() { | ||
return HashCodeBuilder.reflectionHashCode(this); | ||
} | ||
|
||
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass") | ||
@Override public boolean equals(Object other) { | ||
return EqualsBuilder.reflectionEquals(this, other); | ||
} | ||
} |
Oops, something went wrong.