123456789101112131415161718192021222324252627282930313233343536 |
- package TypeAdapter;
- import java.lang.reflect.Type;
- import com.google.gson.JsonDeserializationContext;
- import com.google.gson.JsonDeserializer;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonPrimitive;
- import com.google.gson.JsonSerializationContext;
- import com.google.gson.JsonSerializer;
- import utility.Vector2Int;
- public class PositionAdapter implements JsonSerializer<Vector2Int>, JsonDeserializer<Vector2Int> {
- @Override
- public JsonElement serialize(Vector2Int arg0, Type arg1, JsonSerializationContext arg2) {
- return new JsonPrimitive(arg0.getX() + ":" + arg0.getY());
- }
- @Override
- public Vector2Int deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) {
- try {
- String arg = arg0.getAsString();
- int mid = arg.indexOf(':');
- int x = Integer.parseInt(arg.substring(0, mid));
- int y = Integer.parseInt(arg.substring(mid + 1, arg.length()));
- return new Vector2Int(x, y);
- } catch (NumberFormatException e) {
- System.err.println(e);
- }
- return new Vector2Int(-1, -1);
- }
- }
|