Skip to content

Instantly share code, notes, and snippets.

@gfx
Created June 18, 2014 01:31
Show Gist options
  • Save gfx/9409b05f0e21723cdb7a to your computer and use it in GitHub Desktop.
Save gfx/9409b05f0e21723cdb7a to your computer and use it in GitHub Desktop.
package com..github.gfx.toolbox;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.android.gms.common.annotation.KeepName;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collection;
import java.util.List;
/**
* original is here https://github.com/square/flow/blob/master/flow-sample/src/main/java/com/example/flow/GsonParcer.java
*/
public class Parcels {
private Parcels() {
}
public static <T> Parcelable wrap(T instance) {
try {
String json = encode(instance);
return new Wrapper(json);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> Parcelable wrap(List<T> instance) {
try {
String json = encode(instance);
return new Wrapper(json);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> T unwrap(Parcelable parcelable) {
Wrapper wrapper = (Wrapper) parcelable;
try {
return decode(wrapper.json);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T, U> T unwrap(Parcelable parcelable, TypeToken<U> token) {
Wrapper wrapper = (Wrapper) parcelable;
try {
return decode(wrapper.json, token);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static <T> List<T> unwrapList(Parcelable parcelable) {
TypeToken<Collection<T>> typeToken = new TypeToken<Collection<T>>() {
};
return unwrap(parcelable, typeToken);
}
private static <T> String encode(T instance) throws IOException {
StringWriter stringWriter = new StringWriter();
JsonWriter writer = new JsonWriter(stringWriter);
try {
Class<?> type = instance.getClass();
writer.beginObject();
writer.name(type.getName());
GsonHolder.GSON.toJson(instance, type, writer);
writer.endObject();
return stringWriter.toString();
} finally {
writer.close();
}
}
private static <T> T decode(String json) throws IOException {
JsonReader reader = new JsonReader(new StringReader(json));
try {
reader.beginObject();
Class<?> type = Class.forName(reader.nextName());
return GsonHolder.GSON.fromJson(reader, type);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
reader.close();
}
}
private static <T, U> T decode(String json, TypeToken<U> token) throws IOException {
JsonReader reader = new JsonReader(new StringReader(json));
try {
reader.beginObject();
reader.nextName();
return GsonHolder.GSON.fromJson(reader, token.getType());
} finally {
reader.close();
}
}
private static class Wrapper implements Parcelable {
private final String json;
private Wrapper(String json) {
this.json = json;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(json);
}
@KeepName
public static final Creator<Wrapper> CREATOR = new Creator<Wrapper>() {
@Override
public Wrapper createFromParcel(Parcel in) {
String json = in.readString();
return new Wrapper(json);
}
@Override
public Wrapper[] newArray(int size) {
return new Wrapper[size];
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment