Created
May 7, 2015 15:27
-
-
Save ajcrites/d5eeb2284e8a706ae337 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Capabilities { | |
private int alloy; | |
private Range gauge; | |
private Range width; | |
} |
This file contains hidden or 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 Range { | |
private int value; | |
private String range; | |
private boolean isRange; | |
public Range(int value) { | |
this.value = value; | |
this.isRange = false; | |
} | |
public Range(String value) { | |
this.range = range; | |
this.isRange = true; | |
} | |
public boolean isRange() { | |
return this.isRange; | |
} | |
public int getValue() { | |
return this.value; | |
} | |
public String getRange() { | |
return this.range; | |
} | |
} |
This file contains hidden or 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
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class RangeParse { | |
public static void main(String [] args) { | |
Gson gson = new GsonBuilder().create(); | |
Capabilities capabilities = gson.fromJson("{\"alloy\":1100,\"gauge\":\"1.0-2.0\",\"width\":1.0}", Capabilities.class); | |
gson.toJson(capabilities, System.out); | |
} | |
} |
This file contains hidden or 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
import java.lang.reflect.Type; | |
import com.google.gson.JsonSerializer; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonPrimitive; | |
import com.google.gson.JsonParseException; | |
public class RangeTypeAdapter implements JsonDeserializer<Range>, JsonSerializer<Range> { | |
public Range deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) throws JsonParseException { | |
String range; | |
Integer value; | |
try { | |
value = json.getAsInt(); | |
return new Range(value); | |
} | |
catch (NumberFormatException e) { | |
return new Range(json.getAsString()); | |
} | |
} | |
public JsonElement serialize(Range range, Type typeOfSrc, JsonSerializationContext ctx) { | |
if (range.isRange()) { | |
return new JsonPrimitive(range.getRange()); | |
} | |
return new JsonPrimitive(range.getValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment