-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdef820
commit 5827426
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeDeserializer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MinDiskFree> { | ||
|
||
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(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/nutomic/syncthingandroid/model/MinDiskFreeSerializer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MinDiskFree> { | ||
|
||
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; | ||
} | ||
} |