Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix encoding of BigDecimal keys of maps (#111)

  • Loading branch information...
commit a76475a080b42c84e12c0e534d7c5959b42b86ba 1 parent c679281
Christian Meier mkristian authored
19 restygwt/src/main/java/org/fusesource/restygwt/client/AbstractJsonEncoderDecoder.java
View
@@ -645,8 +645,16 @@ static public int getSize(JSONValue value) {
case DEFAULT:
case SIMPLE: {
JSONObject rc = new JSONObject();
+
for (Entry<KeyType, ValueType> t : value.entrySet()) {
- rc.put(keyEncoder.encode(t.getKey()).toString(), valueEncoder.encode(t.getValue()));
+ //TODO find a way to check only once
+ JSONValue k = keyEncoder.encode(t.getKey());
+ if (k.isString() != null) {
+ rc.put(k.isString().stringValue(), valueEncoder.encode(t.getValue()));
+ }
+ else {
+ rc.put(k.toString(), valueEncoder.encode(t.getValue()));
+ }
}
return rc;
}
@@ -656,7 +664,14 @@ static public int getSize(JSONValue value) {
int i = 0;
for (Entry<KeyType, ValueType> t : value.entrySet()) {
JSONObject entry = new JSONObject();
- entry.put("key", new JSONString(keyEncoder.encode(t.getKey()).toString()));
+ //TODO find a way to check only once
+ JSONValue k = keyEncoder.encode(t.getKey());
+ if (k.isString() != null) {
+ entry.put("key", k);
+ }
+ else {
+ entry.put("key", new JSONString(k.toString()));
+ }
entry.put("value", valueEncoder.encode(t.getValue()));
entries.set(i++, entry);
}
18 restygwt/src/test/java/org/fusesource/restygwt/client/codec/EncoderDecoderTestGwt.java
View
@@ -18,6 +18,7 @@
package org.fusesource.restygwt.client.codec;
+import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
@@ -424,6 +425,23 @@ public void testTypeMapWithIntegerDecode() {
Json.Style.JETTISON_NATURAL).toString());
}
+ public void testTypeMapWithBigDecimalDecode() {
+ Map<BigDecimal, String> map = new HashMap<BigDecimal, String>();
+ map.put(BigDecimal.valueOf(123), "321");
+ AbstractJsonEncoderDecoder<BigDecimal> keyEncoder = AbstractJsonEncoderDecoder.BIG_DECIMAL;
+ AbstractJsonEncoderDecoder<String> valueEncoder = AbstractJsonEncoderDecoder.STRING;
+ assertEquals(map.toString(),
+ AbstractJsonEncoderDecoder.toMap(AbstractJsonEncoderDecoder.toJSON(map, keyEncoder, valueEncoder, Json.Style.DEFAULT),
+ keyEncoder,
+ valueEncoder,
+ Json.Style.DEFAULT).toString());
+ assertEquals(map.toString(),
+ AbstractJsonEncoderDecoder.toMap(AbstractJsonEncoderDecoder.toJSON(map, keyEncoder, valueEncoder, Json.Style.JETTISON_NATURAL),
+ keyEncoder,
+ valueEncoder,
+ Json.Style.JETTISON_NATURAL).toString());
+ }
+
static class Email {
String name, email;
public String toString(){
Please sign in to comment.
Something went wrong with that request. Please try again.