diff --git a/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeDeserializer.java b/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeDeserializer.java new file mode 100644 index 000000000..75f98abc9 --- /dev/null +++ b/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeDeserializer.java @@ -0,0 +1,44 @@ +package com.nutomic.syncthingandroid.model; + +import android.util.Log; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +public class MinDiskFreeDeserializer implements JsonDeserializer { + + private static final String TAG = "MinDiskFreeDeserializer"; + + @Override + public MinDiskFree deserialize(final JsonElement json, final Type typeOfT, + final JsonDeserializationContext context) throws JsonParseException { + JsonObject jsonObject = json.getAsJsonObject(); + MinDiskFree minDiskFree = new MinDiskFree(); + minDiskFree.unit = getFieldAsString(jsonObject, "unit"); + minDiskFree.value = getFieldAsFloat(jsonObject, "value"); + Log.v(TAG, "TEST unit " + minDiskFree.unit); + Log.v(TAG, "TEST value " + minDiskFree.value); + return minDiskFree; + } + + private String getFieldAsString(JsonObject jsonObject, String serializedName) { + if (jsonObject.get(serializedName) == null) { + Log.v(TAG, "getFieldAsString: " + serializedName + " == null"); + return null; + } + return jsonObject.get(serializedName).getAsString(); + } + + private float getFieldAsFloat(JsonObject jsonObject, String serializedName) { + if (jsonObject.get(serializedName) == null) { + Log.v(TAG, "getFieldAsFloat: " + serializedName + " == null"); + return 0; + } + return jsonObject.get(serializedName).getAsFloat(); + } +} diff --git a/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeSerializer.java b/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeSerializer.java new file mode 100644 index 000000000..347fd17f5 --- /dev/null +++ b/app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeSerializer.java @@ -0,0 +1,26 @@ +package com.nutomic.syncthingandroid.model; + +import android.util.Log; + +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.lang.reflect.Type; + +public class MinDiskFreeSerializer implements JsonSerializer { + + private static final String TAG = "MinDiskFreeSerializer"; + + @Override + public JsonElement serialize(final MinDiskFree minDiskFree, Type typeOfSrc, + final JsonSerializationContext context) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("unit", minDiskFree.unit); + jsonObject.addProperty("value", minDiskFree.value); + Log.v(TAG, "TEST SERIALIZE " + minDiskFree.unit); + Log.v(TAG, "TEST SERIALIZE " + minDiskFree.value); + return jsonObject; + } +}