Forked from dustin-graham/CustomizedTypeAdapterFactory.java
Created
August 2, 2017 17:49
-
-
Save Hazer/1b1df43ae876a06e0b77b7040d4b4547 to your computer and use it in GitHub Desktop.
How to manipulate Gson parsing the awesome way. Based on the SO thread: http://stackoverflow.com/questions/11271375/gson-custom-seralizer-for-one-variable-of-many-in-an-object-using-typeadapter
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
public abstract class CustomizedTypeAdapterFactory<C> | |
implements TypeAdapterFactory { | |
private final Class<C> customizedClass; | |
public CustomizedTypeAdapterFactory(Class<C> customizedClass) { | |
this.customizedClass = customizedClass; | |
} | |
@SuppressWarnings("unchecked") // we use a runtime check to guarantee that 'C' and 'T' are equal | |
public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | |
return type.getRawType() == customizedClass | |
? (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<C>) type) | |
: null; | |
} | |
private TypeAdapter<C> customizeMyClassAdapter(Gson gson, TypeToken<C> type) { | |
final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type); | |
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); | |
return new TypeAdapter<C>() { | |
@Override public void write(JsonWriter out, C value) throws IOException { | |
JsonElement tree = delegate.toJsonTree(value); | |
beforeWrite(value, tree); | |
elementAdapter.write(out, tree); | |
} | |
@Override public C read(JsonReader in) throws IOException { | |
JsonElement tree = elementAdapter.read(in); | |
afterRead(tree); | |
return delegate.fromJsonTree(tree); | |
} | |
}; | |
} | |
/** | |
* Override this to muck with {@code toSerialize} before it is written to | |
* the outgoing JSON stream. | |
*/ | |
protected void beforeWrite(C source, JsonElement toSerialize) { | |
} | |
/** | |
* Override this to muck with {@code deserialized} before it parsed into | |
* the application type. | |
*/ | |
protected void afterRead(JsonElement deserialized) { | |
} | |
} |
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
public class DeviceInfo { | |
@SerializedName("values") | |
private Values mValues; | |
private static class Values { | |
@SerializedName("Websocket Version") | |
private String mWebsocketVersion; | |
@SerializedName("Model") | |
private String mModel; | |
@SerializedName("Machine Type") | |
private String mMachineType; | |
@SerializedName("Equipment Key") | |
private String mEquipmentKey; | |
@SerializedName("Part Number") | |
private String mPartNumber; | |
@SerializedName("Version") | |
private String mVersion; | |
@SerializedName("Machine UUID") | |
private String mMachineUUID; | |
@SerializedName("Features") | |
private String mFeatures; | |
private List<String> mFeatureList; | |
} | |
//getters and setters ... | |
public static class DeviceInfoDeserializerFactory extends CustomizedTypeAdapterFactory<Values> { | |
public DeviceInfoDeserializerFactory() { | |
super(Values.class); | |
} | |
@Override | |
protected void afterRead(JsonElement deserialized) { | |
JsonObject machineInfoObject = deserialized.getAsJsonObject(); | |
String featuresString = machineInfoObject.get("Features").getAsString(); | |
List<String> features = new ArrayList<String>(Arrays.asList(featuresString.split(", "))); | |
JsonArray featureArray = new JsonArray(); | |
for (String feature : features) { | |
featureArray.add(new JsonPrimitive(feature)); | |
} | |
machineInfoObject.add("mFeatureList", featureArray); | |
} | |
} | |
} |
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
{ | |
"values": { | |
"Websocket Version": "2.1", | |
"Model": "ETEP14112", | |
"Machine Type": , | |
"Equipment Key": "T7", | |
"Part Number": "324974", | |
"Version": "3.1.1102.0", | |
"Machine UUID": "7001004d-4d43-3034-4744-1408e584be00", | |
"Features": "Feature 1, Feature 2, Feature 3, Feature 4, Feature 5, Feature 6" | |
} | |
} |
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
public class MachineInfoParseTest extends TestCase { | |
private String mManifest_1 = "{ \"values\":{ \"Websocket Version\":\"2.1\", \"Model\":\"ETEP14112\", \"Machine Type\":\"SpecialDevice\", \"Equipment Key\":\"T7\", \"Part Number\":\"324974\", \"Version\":\"3.1.1102.0\", \"Machine UUID\":\"7001004d-4d43-3034-4744-1408e584be00\", \"Features\":\"Feature 1, Feature 2, Feature 3, Feature 4, Feature 5, Feature 6\"}}"; | |
public void testManifestParse() { | |
Gson gson = new GsonBuilder().registerTypeAdapterFactory(new MachineInfo.MachineInfoDeserializerFactory()).create(); | |
MachineInfo machineInfo = gson.fromJson(mManifest_1, MachineInfo.class); | |
List<String> featureList = machineInfo.getFeatureList(); | |
assertNotNull(featureList); | |
assertEquals(6, featureList.size()); | |
assertEquals("Feature 1", featureList.get(0)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment