Skip to content

Commit

Permalink
Add de/serializer for MinDiskFree
Browse files Browse the repository at this point in the history
  • Loading branch information
Catfriend1 committed Jan 26, 2019
1 parent bdef820 commit 5827426
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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();
}
}
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;
}
}

0 comments on commit 5827426

Please sign in to comment.