Skip to content
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

Merged
merged 13 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/com/bunq/sdk/json/AnchorObjectAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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.*;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wildcard in import :'(


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

Choose a reason for hiding this comment

The 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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 newlines


if (model.isAllFieldNull()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove line

Field[] allFields = model.getClass().getDeclaredFields();

for(Field field : allFields) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can also go as well

if (!BunqModel.class.isAssignableFrom(field.getType())) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bruv ?

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line before this :)

}
}

return model;
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/bunq/sdk/json/BunqGsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bunq.sdk.context.InstallationContext;
import com.bunq.sdk.http.Pagination;
import com.bunq.sdk.model.core.AnchorObjectInterface;
import com.bunq.sdk.model.core.Installation;
import com.bunq.sdk.model.core.MonetaryAccountReference;
import com.bunq.sdk.model.core.SessionServer;
Expand Down Expand Up @@ -33,7 +34,8 @@ public static GsonBuilder buildDefault() {
new MonetaryAccountReferenceTypeAdapter()
)
.registerTypeAdapter(InstallationContext.class, new InstallationContextAdapter())
.registerTypeAdapter(Pagination.class, new PaginationAdapter());
.registerTypeAdapter(Pagination.class, new PaginationAdapter())
.registerTypeHierarchyAdapter(AnchorObjectInterface.class, new AnchorObjectAdapter());
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/AnchorObjectInterface.java
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();
}
12 changes: 12 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/BunqModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -34,6 +35,13 @@ abstract public class BunqModel {
protected BunqModel() {
}

public static <T> T fromJsonReader(Class<T> tClass, JsonReader reader) {

Choose a reason for hiding this comment

The 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.
*/
Expand Down Expand Up @@ -147,4 +155,8 @@ public String toString() {
return gson.toJson(this);
}

/**
*/
abstract public boolean isAllFieldNull();

}
16 changes: 16 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/Installation.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,20 @@ public String getPublicKeyServer() {
return publicKeyServer.getPublicKeyServer();
}

@Override
public boolean isAllFieldNull() {
if (this.id == null) {
return false;
}

if (this.sessionToken == null) {
return false;
}

if (this.publicKeyServer == null) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@ public LabelMonetaryAccount getLabelMonetaryAccount() {
return labelMonetaryAccount;
}

@Override
public boolean isAllFieldNull() {
if (this.pointer == null) {
return false;
}

if (this.labelMonetaryAccount == null) {
return false;
}

return true;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/SessionServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,24 @@ public UserPerson getUserPerson() {
return userPerson;
}

@Override
public boolean isAllFieldNull() {
if (this.id == null) {
return false;
}

if (this.sessionToken == null) {
return false;
}

if (this.userCompany == null) {
return false;
}

if (this.userPerson == null) {
return false;
}

return true;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/bunq/sdk/model/core/SessionToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ public class SessionToken extends BunqModel {
protected SessionToken() {
}

@Override
public boolean isAllFieldNull() {
if (this.token == null) {
return false;
}

return true;
}

public SessionToken(String token) {
this.token = token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.bunq.sdk.model.core.MonetaryAccountReference;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,4 +46,16 @@ public static BunqResponse<byte[]> list(ApiContext apiContext, Integer userId, I
return new BunqResponse<>(responseRaw.getBodyBytes(), responseRaw.getHeaders());
}

/**
*/
public boolean isAllFieldNull() {
return true;
}

/**
*/
public static AttachmentConversationContent fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentConversationContent.class, reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.bunq.sdk.model.generated.object.Attachment;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -85,4 +86,24 @@ public void setId(Integer id) {
this.id = id;
}

/**
*/
public boolean isAllFieldNull() {
if (this.attachment != null) {
return false;
}

if (this.id != null) {
return false;
}

return true;
}

/**
*/
public static AttachmentMonetaryAccount fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentMonetaryAccount.class, reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.bunq.sdk.model.generated.object.Attachment;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -137,4 +138,32 @@ public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}

/**
*/
public boolean isAllFieldNull() {
if (this.uuid != null) {
return false;
}

if (this.created != null) {
return false;
}

if (this.updated != null) {
return false;
}

if (this.attachment != null) {
return false;
}

return true;
}

/**
*/
public static AttachmentPublic fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentPublic.class, reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.bunq.sdk.model.core.MonetaryAccountReference;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,4 +46,16 @@ public static BunqResponse<byte[]> list(ApiContext apiContext, String attachment
return new BunqResponse<>(responseRaw.getBodyBytes(), responseRaw.getHeaders());
}

/**
*/
public boolean isAllFieldNull() {
return true;
}

/**
*/
public static AttachmentPublicContent fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentPublicContent.class, reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.bunq.sdk.model.generated.object.Attachment;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -139,4 +140,32 @@ public void setAttachment(Attachment attachment) {
this.attachment = attachment;
}

/**
*/
public boolean isAllFieldNull() {
if (this.id != null) {
return false;
}

if (this.created != null) {
return false;
}

if (this.updated != null) {
return false;
}

if (this.attachment != null) {
return false;
}

return true;
}

/**
*/
public static AttachmentTab fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentTab.class, reader);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.bunq.sdk.model.core.MonetaryAccountReference;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.stream.JsonReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,4 +46,16 @@ public static BunqResponse<byte[]> list(ApiContext apiContext, Integer userId, I
return new BunqResponse<>(responseRaw.getBodyBytes(), responseRaw.getHeaders());
}

/**
*/
public boolean isAllFieldNull() {
return true;
}

/**
*/
public static AttachmentTabContent fromJsonReader(JsonReader reader) {
return fromJsonReader(AttachmentTabContent.class, reader);
}

}
Loading