diff --git a/msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java b/msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java index 53fa77099..4d7a6e8c2 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java @@ -20,7 +20,7 @@ /** * The interface {@code ArrayValue} represents MessagePack's Array type. - *
+ * * MessagePack's Array type can represent sequence of values. */ public interface ArrayValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/BinaryValue.java b/msgpack-core/src/main/java/org/msgpack/value/BinaryValue.java index b0c636d9c..c66f7a67c 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/BinaryValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/BinaryValue.java @@ -17,7 +17,7 @@ /** * The interface {@code BinaryValue} represents MessagePack's Binary type. - * + * * MessagePack's Binary type can represent a byte array at most 264-1 bytes. * * @see org.msgpack.value.RawValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/BooleanValue.java b/msgpack-core/src/main/java/org/msgpack/value/BooleanValue.java index f81353907..6ccf5c9ef 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/BooleanValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/BooleanValue.java @@ -17,7 +17,7 @@ /** * The interface {@code BooleanValue} represents MessagePack's Boolean type. - * + * * MessagePack's Boolean type can represent {@code true} or {@code false}. */ public interface BooleanValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/ExtensionValue.java b/msgpack-core/src/main/java/org/msgpack/value/ExtensionValue.java index 43a5e5c08..5a076ecbc 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/ExtensionValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/ExtensionValue.java @@ -17,10 +17,10 @@ /** * The interface {@code ExtensionValue} represents MessagePack's Extension type. - * + * * MessagePack's Extension type can represent represents a tuple of type information and a byte array where type information is an * integer whose meaning is defined by applications. - * + * * As the type information, applications can use 0 to 127 as the application-specific types. -1 to -128 is reserved for MessagePack's future extension. */ public interface ExtensionValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/FloatValue.java b/msgpack-core/src/main/java/org/msgpack/value/FloatValue.java index 306a609fd..dbaf73a18 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/FloatValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/FloatValue.java @@ -17,7 +17,7 @@ /** * The interface {@code FloatValue} represents MessagePack's Float type. - * + * * MessagePack's Float type can represent IEEE 754 double precision floating point numbers including NaN and infinity. This is same with Java's {@code double} type. * * @see org.msgpack.value.NumberValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java b/msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java index 51446526f..8480751c4 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java @@ -21,7 +21,7 @@ /** * The interface {@code IntegerValue} represents MessagePack's Integer type. - * + * * MessagePack's Integer type can represent from -263 to 264-1. */ public interface IntegerValue diff --git a/msgpack-core/src/main/java/org/msgpack/value/MapValue.java b/msgpack-core/src/main/java/org/msgpack/value/MapValue.java index f34e77c60..fa7f55c8d 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/MapValue.java +++ b/msgpack-core/src/main/java/org/msgpack/value/MapValue.java @@ -21,7 +21,7 @@ /** * The interface {@code ArrayValue} represents MessagePack's Map type. - * + * * MessagePack's Map type can represent sequence of key-value pairs. */ public interface MapValue @@ -45,9 +45,9 @@ public interface MapValue /** * Returns the key-value pairs as an array of {@code Value}. - * + * * Odd elements are keys. Next element of an odd element is a value corresponding to the key. - * + * * For example, if this value represents{"k1": "v1", "k2": "v2"}, this method returns ["k1", "v1", "k2", "v2"].
*/
Value[] getKeyValueArray();
diff --git a/msgpack-core/src/main/java/org/msgpack/value/RawValue.java b/msgpack-core/src/main/java/org/msgpack/value/RawValue.java
index da68ad40d..8857b5340 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/RawValue.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/RawValue.java
@@ -30,14 +30,14 @@ public interface RawValue
{
/**
* Returns the value as {@code byte[]}.
- *
+ *
* This method copies the byte array.
*/
byte[] asByteArray();
/**
* Returns the value as {@code ByteBuffer}.
- *
+ *
* Returned ByteBuffer is read-only. See {@code#asReadOnlyBuffer()}.
* This method doesn't copy the byte array as much as possible.
*/
@@ -45,11 +45,17 @@ public interface RawValue
/**
* Returns the value as {@code String}.
- *
+ *
* This method throws an exception if the value includes invalid UTF-8 byte sequence.
*
* @throws MessageStringCodingException If this value includes invalid UTF-8 byte sequence.
*/
String asString();
+ /**
+ * Returns the value as {@code String}.
+ *
+ * This method replaces an invalid UTF-8 byte sequence with U+FFFD replacement character.
+ */
+ String toString();
}
diff --git a/msgpack-core/src/main/java/org/msgpack/value/StringValue.java b/msgpack-core/src/main/java/org/msgpack/value/StringValue.java
index 9be80b62d..0c812d58d 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/StringValue.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/StringValue.java
@@ -17,9 +17,9 @@
/**
* The interface {@code StringValue} represents MessagePack's String type.
- *
+ *
* MessagePack's String type can represent a UTF-8 string at most 264-1 bytes.
- *
+ *
* Note that the value could include invalid byte sequences. {@code asString()} method throws {@code MessageTypeStringCodingException} if the value includes invalid byte sequence. {@code toJson()} method replaces an invalid byte sequence with U+FFFD replacement character.
*
* @see org.msgpack.value.RawValue
diff --git a/msgpack-core/src/main/java/org/msgpack/value/Value.java b/msgpack-core/src/main/java/org/msgpack/value/Value.java
index 7ab4344c8..2fae838b4 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/Value.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/Value.java
@@ -30,21 +30,21 @@ public interface Value
{
/**
* Returns type of this value.
- *
+ *
* Note that you can't use instanceof to check type of a value because type of a mutable value is variable.
*/
ValueType getValueType();
/**
* Returns immutable copy of this value.
- *
+ *
* This method simply returns this without copying the value if this value is already immutable.
*/
ImmutableValue immutableValue();
/**
* Returns true if type of this value is Nil.
- *
+ *
* If this method returns true, {@code asNilValue} never throws exceptions.
* Note that you can't use instanceof or cast ((NilValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -52,7 +52,7 @@ public interface Value
/**
* Returns true if type of this value is Boolean.
- *
+ *
* If this method returns true, {@code asBooleanValue} never throws exceptions.
* Note that you can't use instanceof or cast ((BooleanValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -60,7 +60,7 @@ public interface Value
/**
* Returns true if type of this value is Integer or Float.
- *
+ *
* If this method returns true, {@code asNumberValue} never throws exceptions.
* Note that you can't use instanceof or cast ((NumberValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -68,7 +68,7 @@ public interface Value
/**
* Returns true if type of this value is Integer.
- *
+ *
* If this method returns true, {@code asIntegerValue} never throws exceptions.
* Note that you can't use instanceof or cast ((IntegerValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -76,7 +76,7 @@ public interface Value
/**
* Returns true if type of this value is Float.
- *
+ *
* If this method returns true, {@code asFloatValue} never throws exceptions.
* Note that you can't use instanceof or cast ((FloatValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -84,7 +84,7 @@ public interface Value
/**
* Returns true if type of this value is String or Binary.
- *
+ *
* If this method returns true, {@code asRawValue} never throws exceptions.
* Note that you can't use instanceof or cast ((RawValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -92,7 +92,7 @@ public interface Value
/**
* Returns true if type of this value is Binary.
- *
+ *
* If this method returns true, {@code asBinaryValue} never throws exceptions.
* Note that you can't use instanceof or cast ((BinaryValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -100,7 +100,7 @@ public interface Value
/**
* Returns true if type of this value is String.
- *
+ *
* If this method returns true, {@code asStringValue} never throws exceptions.
* Note that you can't use instanceof or cast ((StringValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -108,7 +108,7 @@ public interface Value
/**
* Returns true if type of this value is Array.
- *
+ *
* If this method returns true, {@code asArrayValue} never throws exceptions.
* Note that you can't use instanceof or cast ((ArrayValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -116,7 +116,7 @@ public interface Value
/**
* Returns true if type of this value is Map.
- *
+ *
* If this method returns true, {@code asMapValue} never throws exceptions.
* Note that you can't use instanceof or cast ((MapValue) thisValue) to check type of a value because type of a mutable value is variable.
*/
@@ -124,7 +124,7 @@ public interface Value
/**
* Returns true if type of this an Extension.
- *
+ *
* If this method returns true, {@code asExtensionValue} never throws exceptions.
* Note that you can't use instanceof or cast ((ExtensionValue) thisValue) to check type of a value because
* type of a mutable value is variable.
@@ -133,7 +133,7 @@ public interface Value
/**
* Returns the value as {@code NilValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((NilValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Nil.
@@ -142,7 +142,7 @@ public interface Value
/**
* Returns the value as {@code BooleanValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((BooleanValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Boolean.
@@ -151,7 +151,7 @@ public interface Value
/**
* Returns the value as {@code NumberValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((NumberValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Integer or Float.
@@ -160,7 +160,7 @@ public interface Value
/**
* Returns the value as {@code IntegerValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((IntegerValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Integer.
@@ -169,7 +169,7 @@ public interface Value
/**
* Returns the value as {@code FloatValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((FloatValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Float.
@@ -178,7 +178,7 @@ public interface Value
/**
* Returns the value as {@code RawValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((RawValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Binary or String.
@@ -187,7 +187,7 @@ public interface Value
/**
* Returns the value as {@code BinaryValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((BinaryValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Binary.
@@ -196,7 +196,7 @@ public interface Value
/**
* Returns the value as {@code StringValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((StringValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not String.
@@ -205,7 +205,7 @@ public interface Value
/**
* Returns the value as {@code ArrayValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((ArrayValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Array.
@@ -214,7 +214,7 @@ public interface Value
/**
* Returns the value as {@code MapValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((MapValue) thisValue) to check type of a value because type of a mutable value is variable.
*
* @throws MessageTypeCastException If type of this value is not Map.
@@ -223,7 +223,7 @@ public interface Value
/**
* Returns the value as {@code ExtensionValue}. Otherwise throws {@code MessageTypeCastException}.
- *
+ *
* Note that you can't use instanceof or cast ((ExtensionValue) thisValue) to check type of a value
* because type of a mutable value is variable.
*
@@ -241,17 +241,22 @@ void writeTo(MessagePacker pk)
/**
* Compares this value to the specified object.
- *
+ *
* This method returns {@code true} if type and value are equivalent.
* If this value is {@code MapValue} or {@code ArrayValue}, this method check equivalence of elements recursively.
*/
boolean equals(Object obj);
/**
- * Returns json representation of this Value for debugging purpose.
- * This output json format is subject to change in future.
- * Do not write code that depends on the resulting json format.
+ * Returns json representation of this Value.
+ *
+ * Following behavior is not configurable at this release and they might be changed at future releases:
+ *
+ * * if a key of MapValue is not string, the key is converted to a string using toString method.
+ * * NaN and Infinity of DoubleValue are converted to null.
+ * * ExtensionValue is converted to a 2-element array where first element is a number and second element is the data encoded in hex.
+ * * BinaryValue is converted to a string using UTF-8 encoding. Invalid byte sequence is replaced with U+FFFD replacement character.
+ * * Invalid UTF-8 byte sequences in StringValue is replaced with U+FFFD replacement character
*/
String toJson();
-
}
diff --git a/msgpack-core/src/main/java/org/msgpack/value/Variable.java b/msgpack-core/src/main/java/org/msgpack/value/Variable.java
index 3782f7dee..59e6930cb 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/Variable.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/Variable.java
@@ -195,7 +195,7 @@ public String toJson()
@Override
public String toString()
{
- return toJson();
+ return Variable.this.toString();
}
}
@@ -683,8 +683,9 @@ public String asString()
}
}
+ // override for performance optimization
@Override
- public String toJson()
+ public String toString()
{
byte[] raw = (byte[]) objectValue;
try {
@@ -1052,13 +1053,13 @@ public boolean equals(Object o)
@Override
public String toJson()
{
- return immutableValue().toString(); // TODO optimize
+ return immutableValue().toJson(); // TODO optimize
}
@Override
public String toString()
{
- return toJson();
+ return immutableValue().toString(); // TODO optimize
}
@Override
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/AbstractImmutableRawValue.java b/msgpack-core/src/main/java/org/msgpack/value/impl/AbstractImmutableRawValue.java
index 13b4764f1..31adb5b1a 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/AbstractImmutableRawValue.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/AbstractImmutableRawValue.java
@@ -79,7 +79,9 @@ public String asString()
@Override
public String toJson()
{
- return toJson(new StringBuilder()).toString();
+ StringBuilder sb = new StringBuilder();
+ appendJsonString(sb, toString());
+ return sb.toString();
}
private void decodeString()
@@ -118,12 +120,11 @@ public String toString()
return decodedStringCache;
}
- private StringBuilder toJson(StringBuilder sb)
+ static void appendJsonString(StringBuilder sb, String string)
{
- String s = toString();
sb.append("\"");
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
+ for (int i = 0; i < string.length(); i++) {
+ char ch = string.charAt(i);
if (ch < 0x20) {
switch (ch) {
case '\n':
@@ -169,13 +170,11 @@ else if (ch >= 0xd800 && ch <= 0xdfff) {
}
}
sb.append("\"");
-
- return sb;
}
private static final char[] HEX_TABLE = "0123456789ABCDEF".toCharArray();
- private void escapeChar(StringBuilder sb, int ch)
+ private static void escapeChar(StringBuilder sb, int ch)
{
sb.append("\\u");
sb.append(HEX_TABLE[(ch >> 12) & 0x0f]);
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java
index 093c5a0f5..09fc18e52 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java
@@ -160,15 +160,11 @@ public int hashCode()
@Override
public String toJson()
- {
- return toJson(new StringBuilder()).toString();
- }
-
- private StringBuilder toJson(StringBuilder sb)
{
if (array.length == 0) {
- return sb.append("[]");
+ return "[]";
}
+ StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(array[0].toJson());
for (int i = 1; i < array.length; i++) {
@@ -176,13 +172,33 @@ private StringBuilder toJson(StringBuilder sb)
sb.append(array[i].toJson());
}
sb.append("]");
- return sb;
+ return sb.toString();
}
@Override
public String toString()
{
- return toJson();
+ if (array.length == 0) {
+ return "[]";
+ }
+ StringBuilder sb = new StringBuilder();
+ sb.append("[");
+ appendString(sb, array[0]);
+ for (int i = 1; i < array.length; i++) {
+ sb.append(",");
+ appendString(sb, array[i]);
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+
+ private static void appendString(StringBuilder sb, Value value)
+ {
+ if (value.isRawValue()) {
+ sb.append(value.toJson());
+ } else {
+ sb.append(value.toString());
+ }
}
private static class ImmutableArrayValueList
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBinaryValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBinaryValueImpl.java
index ca5ccfae0..2d444ae83 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBinaryValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBinaryValueImpl.java
@@ -25,7 +25,7 @@
/**
* {@code ImmutableBinaryValueImpl} Implements {@code ImmutableBinaryValue} using a {@code byte[]} field.
- * This implementation caches result of {@code toJson()} and {@code asString()} using a private {@code String} field.
+ * This implementation caches result of {@code toString()} and {@code asString()} using a private {@code String} field.
*
* @see org.msgpack.value.StringValue
*/
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBooleanValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBooleanValueImpl.java
index 5d15698a7..535e91c61 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBooleanValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBooleanValueImpl.java
@@ -24,7 +24,7 @@
/**
* {@code ImmutableBooleanValueImpl} Implements {@code ImmutableBooleanValue} using a {@code boolean} field.
- *
+ *
* This class is a singleton. {@code ImmutableBooleanValueImpl.trueInstance()} and {@code ImmutableBooleanValueImpl.falseInstance()} are the only instances of this class.
*
* @see org.msgpack.value.BooleanValue
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java
index 5cc9b66f7..b7fa39397 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java
@@ -128,12 +128,16 @@ public int hashCode()
@Override
public String toJson()
{
- return Double.toString(value);
+ if (Double.isNaN(value) || Double.isInfinite(value)) {
+ return "null";
+ } else {
+ return Double.toString(value);
+ }
}
@Override
public String toString()
{
- return toJson();
+ return Double.toString(value);
}
}
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableExtensionValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableExtensionValueImpl.java
index c7659a477..eb14cc767 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableExtensionValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableExtensionValueImpl.java
@@ -112,19 +112,27 @@ public int hashCode()
public String toJson()
{
StringBuilder sb = new StringBuilder();
- sb.append('(');
+ sb.append('[');
sb.append(Byte.toString(type));
- sb.append(",0x");
+ sb.append(",\"");
for (byte e : data) {
sb.append(Integer.toString((int) e, 16));
}
- sb.append(")");
+ sb.append("\"]");
return sb.toString();
}
@Override
public String toString()
{
- return toJson();
+ StringBuilder sb = new StringBuilder();
+ sb.append('(');
+ sb.append(Byte.toString(type));
+ sb.append(",0x");
+ for (byte e : data) {
+ sb.append(Integer.toString((int) e, 16));
+ }
+ sb.append(")");
+ return sb.toString();
}
}
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java
index ede45d170..3df98d619 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java
@@ -148,34 +148,63 @@ public int hashCode()
}
@Override
- public String toString()
+ public String toJson()
{
- return toJson();
+ if (kvs.length == 0) {
+ return "{}";
+ }
+ StringBuilder sb = new StringBuilder();
+ sb.append("{");
+ appendJsonKey(sb, kvs[0]);
+ sb.append(":");
+ sb.append(kvs[1].toJson());
+ for (int i = 2; i < kvs.length; i += 2) {
+ sb.append(",");
+ appendJsonKey(sb, kvs[i]);
+ sb.append(":");
+ sb.append(kvs[i + 1].toJson());
+ }
+ sb.append("}");
+ return sb.toString();
}
- @Override
- public String toJson()
+ private static void appendJsonKey(StringBuilder sb, Value key)
{
- return toJson(new StringBuilder()).toString();
+ if (key.isRawValue()) {
+ sb.append(key.toJson());
+ } else {
+ ImmutableStringValueImpl.appendJsonString(sb, key.toString());
+ }
}
- private StringBuilder toJson(StringBuilder sb)
+ @Override
+ public String toString()
{
if (kvs.length == 0) {
- return sb.append("{}");
+ return "{}";
}
+ StringBuilder sb = new StringBuilder();
sb.append("{");
- sb.append(kvs[0].toJson());
+ appendString(sb, kvs[0]);
sb.append(":");
- sb.append(kvs[1].toJson());
+ appendString(sb, kvs[1]);
for (int i = 2; i < kvs.length; i += 2) {
sb.append(",");
- sb.append(kvs[i].toJson());
+ appendString(sb, kvs[i]);
sb.append(":");
- sb.append(kvs[i + 1].toJson());
+ appendString(sb, kvs[i + 1]);
}
sb.append("}");
- return sb;
+ return sb.toString();
+ }
+
+ private static void appendString(StringBuilder sb, Value value)
+ {
+ if (value.isRawValue()) {
+ sb.append(value.toJson());
+ } else {
+ sb.append(value.toString());
+ }
}
private static class ImmutableMapValueMap
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableNilValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableNilValueImpl.java
index 141e2ce1a..7077d115e 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableNilValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableNilValueImpl.java
@@ -24,7 +24,7 @@
/**
* {@code ImmutableNilValueImpl} Implements {@code ImmutableNilValue}.
- *
+ *
* This class is a singleton. {@code ImmutableNilValueImpl.get()} is the only instances of this class.
*
* @see org.msgpack.value.NilValue
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableStringValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableStringValueImpl.java
index ab26c72d4..dbdb57195 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableStringValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableStringValueImpl.java
@@ -25,7 +25,7 @@
/**
* {@code ImmutableStringValueImpl} Implements {@code ImmutableStringValue} using a {@code byte[]} field.
- * This implementation caches result of {@code toJson()} and {@code asString()} using a private {@code String} field.
+ * This implementation caches result of {@code toString()} and {@code asString()} using a private {@code String} field.
*
* @see org.msgpack.value.StringValue
*/