Skip to content

Commit

Permalink
Improved convenience implementations of getString() and getInt()
Browse files Browse the repository at this point in the history
  • Loading branch information
jfuerth committed Feb 3, 2013
1 parent 3944999 commit dce612c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
18 changes: 10 additions & 8 deletions api/src/main/java/javax/json/JsonArrayBuilder.java
Expand Up @@ -266,11 +266,11 @@ public String getString(int index) {

@Override
public String getString(int index, String defaultValue) {
try {
return getString(index);
} catch (Exception e) {
return defaultValue;
JsonValue value = get(index);
if (value instanceof JsonString) {
return ((JsonString) value).getValue();
}
return defaultValue;
}

@Override
Expand All @@ -280,11 +280,11 @@ public int getInt(int index) {

@Override
public int getInt(int index, int defaultValue) {
try {
return getInt(index);
} catch (Exception e) {
return defaultValue;
JsonValue value = get(index);
if (value instanceof JsonNumber) {
return ((JsonNumber) value).intValue();
}
return defaultValue;
}

@Override
Expand All @@ -301,6 +301,8 @@ public boolean getBoolean(int index) {

@Override
public boolean getBoolean(int index, boolean defaultValue) {
// XXX this is not ideal for correctness or for speed.
// preferred approach would be to create a JsonBoolean type and test using instanceof
try {
return getBoolean(index);
} catch (Exception e) {
Expand Down
24 changes: 15 additions & 9 deletions api/src/main/java/javax/json/JsonObjectBuilder.java
Expand Up @@ -43,7 +43,11 @@
import java.io.StringWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
* Builds a {@link JsonObject} from scratch. It uses builder pattern to build
Expand Down Expand Up @@ -314,11 +318,11 @@ public String getString(String name) {

@Override
public String getString(String name, String defaultValue) {
try {
return getString(name);
} catch (Exception e) {
return defaultValue;
JsonValue value = get(name);
if (value instanceof JsonString) {
return ((JsonString) value).getValue();
}
return defaultValue;
}

@Override
Expand All @@ -328,11 +332,11 @@ public int getInt(String name) {

@Override
public int getInt(String name, int defaultValue) {
try {
return getInt(name);
} catch (Exception e) {
return defaultValue;
JsonValue value = get(name);
if (value instanceof JsonNumber) {
return ((JsonNumber) value).intValue();
}
return defaultValue;
}

@Override
Expand All @@ -351,6 +355,8 @@ public boolean getBoolean(String name) {

@Override
public boolean getBoolean(String name, boolean defaultValue) {
// XXX this is not ideal for correctness or for speed.
// preferred approach would be to create a JsonBoolean type and test using instanceof
try {
return getBoolean(name);
} catch (Exception e) {
Expand Down

0 comments on commit dce612c

Please sign in to comment.