Skip to content
Permalink
Browse files

Merge pull request #17 from idrsolutions/extend-json-values

Allow boolean and int custom values in the JSON response
  • Loading branch information...
leonatherton committed Aug 29, 2019
2 parents 6d0d519 + 3f5b0fd commit 7b0cf21d99b6e18d8569ee5e1552c896cf58ea93
Showing with 60 additions and 5 deletions.
  1. +1 −1 pom.xml
  2. +59 −4 src/main/java/conversion/Individual.java
@@ -4,7 +4,7 @@
<groupId>com.idrsolutions</groupId>
<artifactId>base-microservice-example</artifactId>
<packaging>jar</packaging>
<version>5.0.0-alpha</version>
<version>5.1.0-alpha</version>
<name>IDRsolutions Base Microservice Example</name>
<description>Provides the shared classes used by IDRsolutions microservice examples.</description>
<url>https://github.com/idrsolutions/base-microservice-example</url>
@@ -37,7 +37,7 @@
private String errorCode;
private Object customData;

private final HashMap<String, String> customValues = new HashMap<>();
private final HashMap<String, JsonValue> customValues = new HashMap<>();

/**
* Create individual with a specific UUID.
@@ -72,8 +72,8 @@ String toJsonString() {
json.append("{\"state\":\"").append(state).append("\"")
.append(errorCode != null ? ",\"errorCode\":" + errorCode : "");

for (final Map.Entry<String, String> valuePair : customValues.entrySet()) {
json.append(",\"").append(valuePair.getKey()).append("\":\"").append(valuePair.getValue()).append("\"");
for (final Map.Entry<String, JsonValue> valuePair : customValues.entrySet()) {
json.append(",\"").append(valuePair.getKey()).append("\":").append(valuePair.getValue().toString());
}

json.append("}");
@@ -89,7 +89,29 @@ String toJsonString() {
* @param value the value mapped to the key
*/
public void setValue(final String key, final String value) {
customValues.put(key, value);
customValues.put(key, JsonValue.of(value));
}

/**
* Adds a key value pair to the individual to pass to the client in GET
* requests and callbacks.
*
* @param key the key to be passed to the client
* @param value the value mapped to the key
*/
public void setValue(final String key, final boolean value) {
customValues.put(key, JsonValue.of(value));
}

/**
* Adds a key value pair to the individual to pass to the client in GET
* requests and callbacks.
*
* @param key the key to be passed to the client
* @param value the value mapped to the key
*/
public void setValue(final String key, final int value) {
customValues.put(key, JsonValue.of(value));
}

/**
@@ -178,4 +200,37 @@ public void setCustomData(Object customData) {
public long getTimestamp() {
return timestamp;
}

private static abstract class JsonValue {
private JsonValue() {}

abstract public String toString();

private static JsonValue of(final String value) {
return new JsonValue() {
@Override
public String toString() {
return '"' + value + '"';
}
};
}

private static JsonValue of(final int value) {
return new JsonValue() {
@Override
public String toString() {
return String.valueOf(value);
}
};
}

private static JsonValue of(final boolean value) {
return new JsonValue() {
@Override
public String toString() {
return String.valueOf(value);
}
};
}
}
}

0 comments on commit 7b0cf21

Please sign in to comment.
You can’t perform that action at this time.