Skip to content

Commit 5a91feb

Browse files
committed
Fix #21 JsonConfig.IncludeNullValues for response
Add tests. Up version to 1.0.26 ready for release.
1 parent c85d1ad commit 5a91feb

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/AndroidClient/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22
apply plugin: 'com.github.dcendents.android-maven'
33
apply plugin: 'com.jfrog.bintray'
44

5-
version = "1.0.25"
5+
version = "1.0.26"
66

77
android {
88
compileSdkVersion 23

src/AndroidClient/client/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111
testCompile 'org.mockito:mockito-core:1.9.5'
1212
}
1313

14-
version = "1.0.25"
14+
version = "1.0.26"
1515
group = "net.servicestack" // Maven Group ID for the artifact
1616
String packageId = "client"
1717
String groupID = group

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,11 @@ public static ResponseStatus createResponseStatus(JsonObject obj) {
548548
Object field = obj.get(key);
549549

550550
if (varName.toLowerCase().equals("errorcode")) {
551-
status.setErrorCode(jsonElementEntry.getValue().getAsString());
551+
status.setErrorCode(getAsStringOrNull(jsonElementEntry.getValue()));
552552
} else if (varName.toLowerCase().equals("message")) {
553-
status.setMessage(jsonElementEntry.getValue().getAsString());
553+
status.setMessage(getAsStringOrNull(jsonElementEntry.getValue()));
554554
} else if (varName.toLowerCase().equals("stacktrace")) {
555-
status.setStackTrace(jsonElementEntry.getValue().getAsString());
555+
status.setStackTrace(getAsStringOrNull(jsonElementEntry.getValue()));
556556
} else if (varName.toLowerCase().equals("errors")) {
557557

558558
if (field instanceof JsonArray){
@@ -567,11 +567,11 @@ public static ResponseStatus createResponseStatus(JsonObject obj) {
567567
String fieldName = Utils.sanitizeVarName(fieldKey);
568568

569569
if (fieldName.toLowerCase().equals("errorcode")) {
570-
fieldError.setErrorCode(entry.getValue().getAsString());
570+
fieldError.setErrorCode(getAsStringOrNull(entry.getValue()));
571571
} else if (fieldName.toLowerCase().equals("message")) {
572-
fieldError.setMessage(entry.getValue().getAsString());
572+
fieldError.setMessage(getAsStringOrNull(entry.getValue()));
573573
} else if (fieldName.toLowerCase().equals("fieldname")) {
574-
fieldError.setFieldName(entry.getValue().getAsString());
574+
fieldError.setFieldName(getAsStringOrNull(entry.getValue()));
575575
}
576576

577577
}
@@ -729,6 +729,10 @@ public static byte[] toBase64Bytes(byte[] source, int off, int len) {
729729
}
730730
}
731731

732+
private static String getAsStringOrNull(JsonElement jsonElement) {
733+
return jsonElement.isJsonNull() ? null : jsonElement.getAsString();
734+
}
735+
732736
private final static byte EQUALS_SIGN = (byte)'=';
733737

734738
private final static byte[] _STANDARD_ALPHABET = {

src/AndroidClient/client/src/test/java/net/servicestack/client/tests/UtilsTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.servicestack.client.tests;
22

3+
import com.google.gson.*;
34
import junit.framework.TestCase;
5+
import net.servicestack.client.ResponseStatus;
46
import net.servicestack.client.Utils;
57

68
import java.text.SimpleDateFormat;
@@ -42,4 +44,20 @@ public void test_Can_parse_pre_UnixTime(){
4244
Date date = Utils.parseDate("\\/Date(-30610224000)\\/");
4345
assertEquals(new Date(-30610224000L), date);
4446
}
47+
48+
public void test_Can_parse_response_with_server_enabled_nulls() {
49+
JsonObject jsonObject = new JsonParser().parse("{\n" +
50+
" \"ResponseStatus\": {\n" +
51+
" \"ErrorCode\": \"ModelNotFoundException\",\n" +
52+
" \"Message\": \"The specified model was not found\",\n" +
53+
" \"StackTrace\": null,\n" +
54+
" \"Errors\": [],\n" +
55+
" \"Meta\": null\n" +
56+
" }\n" +
57+
"}").getAsJsonObject();
58+
ResponseStatus responseStatus = Utils.createResponseStatus(jsonObject.get("ResponseStatus"));
59+
assertEquals(responseStatus.getErrorCode(),"ModelNotFoundException");
60+
assertEquals(responseStatus.getMessage(),"The specified model was not found");
61+
assertEquals(responseStatus.getErrors().size(),0);
62+
}
4563
}

0 commit comments

Comments
 (0)