-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/improve decoder bunq/sdk java#35 #52
Changes from 10 commits
43dcb31
2f05f7f
17f4996
025e89c
9aa9650
bae3983
c57eea2
0eabdb1
9fe88a0
a8c0640
8ef3d92
336c7f9
29b3ae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.bunq.sdk.json; | ||
|
||
import com.bunq.sdk.exception.BunqException; | ||
import com.bunq.sdk.model.core.AnchorObjectInterface; | ||
import com.bunq.sdk.model.core.BunqModel; | ||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Type; | ||
|
||
public class AnchorObjectAdapter implements JsonDeserializer<AnchorObjectInterface> { | ||
@Override | ||
public AnchorObjectInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving throws to next line makes it nicer readable |
||
JsonParseException { | ||
AnchorObjectInterface model = new Gson().fromJson(json, typeOfT); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 newlines |
||
|
||
if (model.isAllFieldNull()) { | ||
Field[] allFields = model.getClass().getDeclaredFields(); | ||
|
||
for(Field field : allFields) { | ||
if (!BunqModel.class.isAssignableFrom(field.getType())) { | ||
continue; | ||
} | ||
|
||
BunqModel content = new Gson().fromJson(json, (Type) field.getType()); | ||
field.setAccessible(true); | ||
|
||
try { | ||
if (content.isAllFieldNull()) { | ||
field.set(model, null); | ||
} else { | ||
field.set(model, content); | ||
} | ||
} catch (IllegalAccessException e) { | ||
throw new BunqException(e.getMessage()); | ||
} | ||
|
||
field.setAccessible(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line before this :) |
||
} | ||
} | ||
|
||
return model; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.bunq.sdk.model.core; | ||
|
||
public interface AnchorObjectInterface { | ||
/** | ||
*/ | ||
boolean isAllFieldNull(); | ||
|
||
/** | ||
*/ | ||
BunqModel getReferencedObject(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.stream.JsonReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
@@ -34,6 +35,13 @@ abstract public class BunqModel { | |
protected BunqModel() { | ||
} | ||
|
||
public static <T> T fromJsonReader(Class<T> tClass, JsonReader reader) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No empty docblock? |
||
return gson.fromJson( | ||
reader, | ||
tClass | ||
); | ||
} | ||
|
||
/** | ||
* De-serializes an object from a JSON format specific to Installation and SessionServer. | ||
*/ | ||
|
@@ -147,4 +155,8 @@ public String toString() { | |
return gson.toJson(this); | ||
} | ||
|
||
/** | ||
*/ | ||
abstract public boolean isAllFieldNull(); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wildcard in import :'(