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 294a0f775..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 @@ -58,31 +58,31 @@ public interface IntegerValue * * @throws MessageIntegerOverflowException If the value does not fit in the range of {@code byte} type. */ - byte getByte(); + byte asByte(); /** * Returns the value as a {@code short}, otherwise throws an exception. * * @throws MessageIntegerOverflowException If the value does not fit in the range of {@code short} type. */ - short getShort(); + short asShort(); /** * Returns the value as an {@code int}, otherwise throws an exception. * * @throws MessageIntegerOverflowException If the value does not fit in the range of {@code int} type. */ - int getInt(); + int asInt(); /** * Returns the value as a {@code long}, otherwise throws an exception. * * @throws MessageIntegerOverflowException If the value does not fit in the range of {@code long} type. */ - long getLong(); + long asLong(); /** * Returns the value as a {@code BigInteger}. */ - BigInteger getBigInteger(); + BigInteger asBigInteger(); } 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/NumberValue.java b/msgpack-core/src/main/java/org/msgpack/value/NumberValue.java
index 0a43973ab..3e5bd75e0 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/NumberValue.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/NumberValue.java
@@ -18,7 +18,7 @@
import java.math.BigInteger;
/**
- * The base interface {@code NumberValue} of {@code IntegerValue} and {@code FloatValue}.
+ * The base interface {@code NumberValue} of {@code IntegerValue} and {@code FloatValue}. To extract primitive type values, call toXXX methods, which may lose some information by rounding or truncation.
*
* @see org.msgpack.value.IntegerValue
* @see org.msgpack.value.FloatValue
@@ -30,36 +30,36 @@ public interface NumberValue
* Represent this value as a byte value, which may involve rounding or truncation of the original value.
* the value.
*/
- byte castAsByte();
+ byte toByte();
/**
* Represent this value as a short value, which may involve rounding or truncation of the original value.
*/
- short castAsShort();
+ short toShort();
/**
* Represent this value as an int value, which may involve rounding or truncation of the original value.
* value.
*/
- int castAsInt();
+ int toInt();
/**
* Represent this value as a long value, which may involve rounding or truncation of the original value.
*/
- long castAsLong();
+ long toLong();
/**
* Represent this value as a BigInteger, which may involve rounding or truncation of the original value.
*/
- BigInteger castAsBigInteger();
+ BigInteger toBigInteger();
/**
* Represent this value as a 32-bit float value, which may involve rounding or truncation of the original value.
*/
- float castAsFloat();
+ float toFloat();
/**
* Represent this value as a 64-bit double value, which may involve rounding or truncation of the original value.
*/
- double castAsDouble();
+ double toDouble();
}
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 6d51d1b51..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,32 +30,32 @@ public interface RawValue
{
/**
* Returns the value as {@code byte[]}.
- *
+ *
* This method copies the byte array.
*/
- byte[] getByteArray();
+ 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.
*/
- ByteBuffer getByteBuffer();
+ ByteBuffer asByteBuffer();
/**
* 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 getString();
+ String asString();
/**
* Returns the value as {@code String}.
- *
+ *
* This method replaces an invalid UTF-8 byte sequence with U+FFFD replacement character.
*/
- String stringValue();
+ 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 da49cd8f6..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,10 +17,10 @@
/**
* 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 getString()} method throws {@code MessageTypeStringCodingException} if the value includes invalid byte sequence. {@code stringValue()} method replaces an invalid byte sequence with U+FFFD replacement character.
+ *
+ * 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 d10d7ba48..2fae838b4 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/Value.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/Value.java
@@ -20,27 +20,31 @@
import java.io.IOException;
/**
- * Value is an implementation of MessagePack type system.
+ * Value is an implementation of MessagePack type system. To retrieve values from a Value object,
+ * You need to check its {@link ValueType} then call an appropriate asXXXValue method.
+ *
+ *
+ *
*/
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.
*/
@@ -48,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.
*/
@@ -56,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.
*/
@@ -64,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.
*/
@@ -72,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.
*/
@@ -80,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.
*/
@@ -88,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.
*/
@@ -96,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.
*/
@@ -104,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.
*/
@@ -112,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.
*/
@@ -120,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.
@@ -129,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.
@@ -138,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.
@@ -147,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.
@@ -156,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.
@@ -165,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.
@@ -174,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.
@@ -183,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.
@@ -192,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.
@@ -201,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.
@@ -210,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.
@@ -219,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.
*
@@ -237,9 +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.
+ *
+ * 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/ValueFactory.java b/msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java
index d229e2659..b0ffc932a 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java
@@ -191,28 +191,32 @@ public MapValue build()
return newMap(map);
}
- public void put(Map.Entry extends Value, ? extends Value> pair)
+ public MapBuilder put(Map.Entry extends Value, ? extends Value> pair)
{
put(pair.getKey(), pair.getValue());
+ return this;
}
- public void put(Value key, Value value)
+ public MapBuilder put(Value key, Value value)
{
map.put(key, value);
+ return this;
}
- public void putAll(Iterable extends Map.Entry extends Value, ? extends Value>> entries)
+ public MapBuilder putAll(Iterable extends Map.Entry extends Value, ? extends Value>> entries)
{
for (Map.Entry extends Value, ? extends Value> entry : entries) {
put(entry.getKey(), entry.getValue());
}
+ return this;
}
- public void putAll(Map extends Value, ? extends Value> map)
+ public MapBuilder putAll(Map extends Value, ? extends Value> map)
{
for (Map.Entry extends Value, ? extends Value> entry : map.entrySet()) {
put(entry);
}
+ return this;
}
}
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 67a5325fe..59e6930cb 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/Variable.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/Variable.java
@@ -186,6 +186,12 @@ public int hashCode()
return Variable.this.hashCode();
}
+ @Override
+ public String toJson()
+ {
+ return Variable.this.toJson();
+ }
+
@Override
public String toString()
{
@@ -357,7 +363,7 @@ public NumberValue asNumberValue()
}
@Override
- public byte castAsByte()
+ public byte toByte()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).byteValue();
@@ -366,7 +372,7 @@ public byte castAsByte()
}
@Override
- public short castAsShort()
+ public short toShort()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).shortValue();
@@ -375,7 +381,7 @@ public short castAsShort()
}
@Override
- public int castAsInt()
+ public int toInt()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).intValue();
@@ -384,7 +390,7 @@ public int castAsInt()
}
@Override
- public long castAsLong()
+ public long toLong()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).longValue();
@@ -393,7 +399,7 @@ public long castAsLong()
}
@Override
- public BigInteger castAsBigInteger()
+ public BigInteger toBigInteger()
{
if (type == Type.BIG_INTEGER) {
return (BigInteger) objectValue;
@@ -405,7 +411,7 @@ else if (type == Type.DOUBLE) {
}
@Override
- public float castAsFloat()
+ public float toFloat()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).floatValue();
@@ -417,7 +423,7 @@ else if (type == Type.DOUBLE) {
}
@Override
- public double castAsDouble()
+ public double toDouble()
{
if (type == Type.BIG_INTEGER) {
return ((BigInteger) objectValue).doubleValue();
@@ -524,7 +530,7 @@ public MessageFormat mostSuccinctMessageFormat()
}
@Override
- public byte getByte()
+ public byte asByte()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(longValue);
@@ -533,7 +539,7 @@ public byte getByte()
}
@Override
- public short getShort()
+ public short asShort()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(longValue);
@@ -542,7 +548,7 @@ public short getShort()
}
@Override
- public int getInt()
+ public int asInt()
{
if (!isInIntRange()) {
throw new MessageIntegerOverflowException(longValue);
@@ -551,7 +557,7 @@ public int getInt()
}
@Override
- public long getLong()
+ public long asLong()
{
if (!isInLongRange()) {
throw new MessageIntegerOverflowException(longValue);
@@ -560,7 +566,7 @@ public long getLong()
}
@Override
- public BigInteger getBigInteger()
+ public BigInteger asBigInteger()
{
if (type == Type.BIG_INTEGER) {
return (BigInteger) objectValue;
@@ -592,7 +598,7 @@ public Variable setFloatValue(double v)
this.type = Type.DOUBLE;
this.accessor = floatAccessor;
this.doubleValue = v;
- this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
+ this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
return this;
}
@@ -600,7 +606,7 @@ public Variable setFloatValue(float v)
{
this.type = Type.DOUBLE;
this.accessor = floatAccessor;
- this.longValue = (long) v; // AbstractNumberValueAccessor uses castAsLong
+ this.longValue = (long) v; // AbstractNumberValueAccessor uses toLong
return this;
}
@@ -651,19 +657,19 @@ public RawValue asRawValue()
}
@Override
- public byte[] getByteArray()
+ public byte[] asByteArray()
{
return (byte[]) objectValue;
}
@Override
- public ByteBuffer getByteBuffer()
+ public ByteBuffer asByteBuffer()
{
- return ByteBuffer.wrap(getByteArray());
+ return ByteBuffer.wrap(asByteArray());
}
@Override
- public String getString()
+ public String asString()
{
byte[] raw = (byte[]) objectValue;
try {
@@ -677,8 +683,9 @@ public String getString()
}
}
+ // override for performance optimization
@Override
- public String stringValue()
+ public String toString()
{
byte[] raw = (byte[]) objectValue;
try {
@@ -724,7 +731,7 @@ public BinaryValue asBinaryValue()
@Override
public ImmutableBinaryValue immutableValue()
{
- return ValueFactory.newBinary(getByteArray());
+ return ValueFactory.newBinary(asByteArray());
}
@Override
@@ -1043,6 +1050,12 @@ public boolean equals(Object o)
return immutableValue().equals(o); // TODO optimize
}
+ @Override
+ public String toJson()
+ {
+ return immutableValue().toJson(); // TODO optimize
+ }
+
@Override
public String toString()
{
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 d558234cf..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
@@ -51,19 +51,19 @@ public ImmutableRawValue asRawValue()
}
@Override
- public byte[] getByteArray()
+ public byte[] asByteArray()
{
return Arrays.copyOf(data, data.length);
}
@Override
- public ByteBuffer getByteBuffer()
+ public ByteBuffer asByteBuffer()
{
return ByteBuffer.wrap(data).asReadOnlyBuffer();
}
@Override
- public String getString()
+ public String asString()
{
if (decodedStringCache == null) {
decodeString();
@@ -77,12 +77,11 @@ public String getString()
}
@Override
- public String stringValue()
+ public String toJson()
{
- if (decodedStringCache == null) {
- decodeString();
- }
- return decodedStringCache;
+ StringBuilder sb = new StringBuilder();
+ appendJsonString(sb, toString());
+ return sb.toString();
}
private void decodeString()
@@ -95,14 +94,14 @@ private void decodeString()
CharsetDecoder reportDecoder = MessagePack.UTF8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
- this.decodedStringCache = reportDecoder.decode(getByteBuffer()).toString();
+ this.decodedStringCache = reportDecoder.decode(asByteBuffer()).toString();
}
catch (CharacterCodingException ex) {
try {
CharsetDecoder replaceDecoder = MessagePack.UTF8.newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE);
- this.decodedStringCache = replaceDecoder.decode(getByteBuffer()).toString();
+ this.decodedStringCache = replaceDecoder.decode(asByteBuffer()).toString();
}
catch (CharacterCodingException neverThrown) {
throw new MessageStringCodingException(neverThrown);
@@ -115,15 +114,17 @@ private void decodeString()
@Override
public String toString()
{
- return toString(new StringBuilder()).toString();
+ if (decodedStringCache == null) {
+ decodeString();
+ }
+ return decodedStringCache;
}
- private StringBuilder toString(StringBuilder sb)
+ static void appendJsonString(StringBuilder sb, String string)
{
- String s = stringValue();
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 a62e7cc64..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
@@ -159,24 +159,46 @@ public int hashCode()
}
@Override
- public String toString()
+ public String toJson()
{
- return toString(new StringBuilder()).toString();
+ if (array.length == 0) {
+ return "[]";
+ }
+ StringBuilder sb = new StringBuilder();
+ sb.append("[");
+ sb.append(array[0].toJson());
+ for (int i = 1; i < array.length; i++) {
+ sb.append(",");
+ sb.append(array[i].toJson());
+ }
+ sb.append("]");
+ return sb.toString();
}
- private StringBuilder toString(StringBuilder sb)
+ @Override
+ public String toString()
{
if (array.length == 0) {
- return sb.append("[]");
+ return "[]";
}
+ StringBuilder sb = new StringBuilder();
sb.append("[");
- sb.append(array[0]);
+ appendString(sb, array[0]);
for (int i = 1; i < array.length; i++) {
sb.append(",");
- sb.append(array[i].toString());
+ appendString(sb, array[i]);
}
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 ImmutableArrayValueList
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java
index ef1de0966..b1c7c0b10 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java
@@ -97,43 +97,43 @@ public ImmutableIntegerValue asIntegerValue()
}
@Override
- public byte castAsByte()
+ public byte toByte()
{
return value.byteValue();
}
@Override
- public short castAsShort()
+ public short toShort()
{
return value.shortValue();
}
@Override
- public int castAsInt()
+ public int toInt()
{
return value.intValue();
}
@Override
- public long castAsLong()
+ public long toLong()
{
return value.longValue();
}
@Override
- public BigInteger castAsBigInteger()
+ public BigInteger toBigInteger()
{
return value;
}
@Override
- public float castAsFloat()
+ public float toFloat()
{
return value.floatValue();
}
@Override
- public double castAsDouble()
+ public double toDouble()
{
return value.doubleValue();
}
@@ -169,7 +169,7 @@ public MessageFormat mostSuccinctMessageFormat()
}
@Override
- public byte getByte()
+ public byte asByte()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(value);
@@ -178,7 +178,7 @@ public byte getByte()
}
@Override
- public short getShort()
+ public short asShort()
{
if (!isInShortRange()) {
throw new MessageIntegerOverflowException(value);
@@ -187,7 +187,7 @@ public short getShort()
}
@Override
- public int getInt()
+ public int asInt()
{
if (!isInIntRange()) {
throw new MessageIntegerOverflowException(value);
@@ -196,7 +196,7 @@ public int getInt()
}
@Override
- public long getLong()
+ public long asLong()
{
if (!isInLongRange()) {
throw new MessageIntegerOverflowException(value);
@@ -205,7 +205,7 @@ public long getLong()
}
@Override
- public BigInteger getBigInteger()
+ public BigInteger asBigInteger()
{
return value;
}
@@ -232,7 +232,7 @@ public boolean equals(Object o)
return false;
}
IntegerValue iv = v.asIntegerValue();
- return value.equals(iv.castAsBigInteger());
+ return value.equals(iv.toBigInteger());
}
@Override
@@ -250,8 +250,14 @@ else if (LONG_MIN.compareTo(value) <= 0
}
@Override
- public String toString()
+ public String toJson()
{
return value.toString();
}
+
+ @Override
+ public String toString()
+ {
+ return toJson();
+ }
}
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 41bbb8659..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 stringValue()} and {@code getString()} 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
*/
@@ -83,7 +83,7 @@ public boolean equals(Object o)
return Arrays.equals(data, bv.data);
}
else {
- return Arrays.equals(data, v.asBinaryValue().getByteArray());
+ return Arrays.equals(data, v.asBinaryValue().asByteArray());
}
}
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 9bd11bdf9..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
@@ -97,8 +97,14 @@ public int hashCode()
}
@Override
- public String toString()
+ public String toJson()
{
return Boolean.toString(value);
}
+
+ @Override
+ public String toString()
+ {
+ return toJson();
+ }
}
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 a69afe242..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
@@ -53,43 +53,43 @@ public ImmutableDoubleValueImpl immutableValue()
}
@Override
- public byte castAsByte()
+ public byte toByte()
{
return (byte) value;
}
@Override
- public short castAsShort()
+ public short toShort()
{
return (short) value;
}
@Override
- public int castAsInt()
+ public int toInt()
{
return (int) value;
}
@Override
- public long castAsLong()
+ public long toLong()
{
return (long) value;
}
@Override
- public BigInteger castAsBigInteger()
+ public BigInteger toBigInteger()
{
return new BigDecimal(value).toBigInteger();
}
@Override
- public float castAsFloat()
+ public float toFloat()
{
return (float) value;
}
@Override
- public double castAsDouble()
+ public double toDouble()
{
return value;
}
@@ -115,7 +115,7 @@ public boolean equals(Object o)
if (!v.isFloatValue()) {
return false;
}
- return value == v.asFloatValue().castAsDouble();
+ return value == v.asFloatValue().toDouble();
}
@Override
@@ -125,6 +125,16 @@ public int hashCode()
return (int) (v ^ (v >>> 32));
}
+ @Override
+ public String toJson()
+ {
+ if (Double.isNaN(value) || Double.isInfinite(value)) {
+ return "null";
+ } else {
+ return Double.toString(value);
+ }
+ }
+
@Override
public String toString()
{
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 b37de8139..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
@@ -108,6 +108,20 @@ public int hashCode()
return hash;
}
+ @Override
+ public String toJson()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append('[');
+ sb.append(Byte.toString(type));
+ sb.append(",\"");
+ for (byte e : data) {
+ sb.append(Integer.toString((int) e, 16));
+ }
+ sb.append("\"]");
+ return sb.toString();
+ }
+
@Override
public String toString()
{
diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java
index 350e01cc5..c83bda332 100644
--- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java
+++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableLongValueImpl.java
@@ -75,43 +75,43 @@ public ImmutableIntegerValue asIntegerValue()
}
@Override
- public byte castAsByte()
+ public byte toByte()
{
return (byte) value;
}
@Override
- public short castAsShort()
+ public short toShort()
{
return (short) value;
}
@Override
- public int castAsInt()
+ public int toInt()
{
return (int) value;
}
@Override
- public long castAsLong()
+ public long toLong()
{
return value;
}
@Override
- public BigInteger castAsBigInteger()
+ public BigInteger toBigInteger()
{
return BigInteger.valueOf(value);
}
@Override
- public float castAsFloat()
+ public float toFloat()
{
return (float) value;
}
@Override
- public double castAsDouble()
+ public double toDouble()
{
return (double) value;
}
@@ -147,7 +147,7 @@ public MessageFormat mostSuccinctMessageFormat()
}
@Override
- public byte getByte()
+ public byte asByte()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(value);
@@ -156,7 +156,7 @@ public byte getByte()
}
@Override
- public short getShort()
+ public short asShort()
{
if (!isInByteRange()) {
throw new MessageIntegerOverflowException(value);
@@ -165,7 +165,7 @@ public short getShort()
}
@Override
- public int getInt()
+ public int asInt()
{
if (!isInIntRange()) {
throw new MessageIntegerOverflowException(value);
@@ -174,13 +174,13 @@ public int getInt()
}
@Override
- public long getLong()
+ public long asLong()
{
return value;
}
@Override
- public BigInteger getBigInteger()
+ public BigInteger asBigInteger()
{
return BigInteger.valueOf((long) value);
}
@@ -210,7 +210,7 @@ public boolean equals(Object o)
if (!iv.isInLongRange()) {
return false;
}
- return value == iv.castAsLong();
+ return value == iv.toLong();
}
@Override
@@ -225,8 +225,14 @@ public int hashCode()
}
@Override
- public String toString()
+ public String toJson()
{
return Long.toString(value);
}
+
+ @Override
+ public String toString()
+ {
+ return toJson();
+ }
}
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 b9a0a6f5c..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,28 +148,63 @@ public int hashCode()
}
@Override
- public String toString()
+ public String 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();
+ }
+
+ private static void appendJsonKey(StringBuilder sb, Value key)
{
- return toString(new StringBuilder()).toString();
+ if (key.isRawValue()) {
+ sb.append(key.toJson());
+ } else {
+ ImmutableStringValueImpl.appendJsonString(sb, key.toString());
+ }
}
- private StringBuilder toString(StringBuilder sb)
+ @Override
+ public String toString()
{
if (kvs.length == 0) {
- return sb.append("{}");
+ return "{}";
}
+ StringBuilder sb = new StringBuilder();
sb.append("{");
- sb.append(kvs[0]);
+ appendString(sb, kvs[0]);
sb.append(":");
- sb.append(kvs[1]);
+ appendString(sb, kvs[1]);
for (int i = 2; i < kvs.length; i += 2) {
sb.append(",");
- sb.append(kvs[i].toString());
+ appendString(sb, kvs[i]);
sb.append(":");
- sb.append(kvs[i + 1].toString());
+ 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 dab10b54f..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
@@ -89,6 +89,12 @@ public int hashCode()
@Override
public String toString()
+ {
+ return toJson();
+ }
+
+ @Override
+ public String toJson()
{
return "null";
}
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 e40139979..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 stringValue()} and {@code getString()} 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
*/
@@ -88,7 +88,7 @@ public boolean equals(Object o)
return Arrays.equals(data, bv.data);
}
else {
- return Arrays.equals(data, v.asStringValue().getByteArray());
+ return Arrays.equals(data, v.asStringValue().asByteArray());
}
}
diff --git a/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java b/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java
index e0ea34679..a74c5cd18 100644
--- a/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java
+++ b/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java
@@ -201,30 +201,30 @@ public static void readAndWriteFile()
case INTEGER:
IntegerValue iv = v.asIntegerValue();
if (iv.isInIntRange()) {
- int i = iv.castAsInt();
+ int i = iv.toInt();
System.out.println("read int: " + i);
}
else if (iv.isInLongRange()) {
- long l = iv.castAsLong();
+ long l = iv.toLong();
System.out.println("read long: " + l);
}
else {
- BigInteger i = iv.castAsBigInteger();
+ BigInteger i = iv.toBigInteger();
System.out.println("read long: " + i);
}
break;
case FLOAT:
FloatValue fv = v.asFloatValue();
- float f = fv.castAsFloat(); // use as float
- double d = fv.castAsDouble(); // use as double
+ float f = fv.toFloat(); // use as float
+ double d = fv.toDouble(); // use as double
System.out.println("read float: " + d);
break;
case STRING:
- String s = v.asStringValue().getString();
+ String s = v.asStringValue().asString();
System.out.println("read string: " + s);
break;
case BINARY:
- byte[] mb = v.asBinaryValue().getByteArray();
+ byte[] mb = v.asBinaryValue().asByteArray();
System.out.println("read binary: size=" + mb.length);
break;
case ARRAY:
diff --git a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala
index 7d87b02a7..62bf8ae82 100644
--- a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala
+++ b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala
@@ -462,7 +462,7 @@ class MessagePackTest extends MessagePackSpec {
val k = kvp(0)
val v = kvp(1)
- (k.asStringValue().getString, v.asStringValue().getString)
+ (k.asStringValue().asString, v.asStringValue().asString)
}).toMap
}.toList
})
diff --git a/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala b/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala
index a89dcb880..e21c387fb 100644
--- a/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala
+++ b/msgpack-core/src/test/scala/org/msgpack/value/ValueTest.scala
@@ -19,6 +19,8 @@ import java.math.BigInteger
import org.msgpack.core._
+import scala.util.parsing.json.JSON
+
class ValueTest extends MessagePackSpec
{
def checkSuccinctType(pack:MessagePacker => Unit, expectedAtMost:MessageFormat) {
@@ -49,5 +51,51 @@ class ValueTest extends MessagePackSpec
}
}
}
+
+ "produce json strings" in {
+
+ import ValueFactory._
+
+ newNil().toJson shouldBe "null"
+ newNil().toString shouldBe "null"
+
+ newBoolean(true).toJson shouldBe "true"
+ newBoolean(false).toJson shouldBe "false"
+ newBoolean(true).toString shouldBe "true"
+ newBoolean(false).toString shouldBe "false"
+
+ newInteger(3).toJson shouldBe "3"
+ newInteger(3).toString shouldBe "3"
+ newInteger(BigInteger.valueOf(1324134134134L)).toJson shouldBe "1324134134134"
+ newInteger(BigInteger.valueOf(1324134134134L)).toString shouldBe "1324134134134"
+
+ newFloat(0.1).toJson shouldBe "0.1"
+ newFloat(0.1).toString shouldBe "0.1"
+
+ newArray(newInteger(0), newString("hello")).toJson shouldBe "[0,\"hello\"]"
+ newArray(newInteger(0), newString("hello")).toString shouldBe "[0,\"hello\"]"
+ newArray(newArray(newString("Apple"), newFloat(0.2)), newNil()).toJson shouldBe """[["Apple",0.2],null]"""
+
+ // Map value
+ val m = newMapBuilder()
+ .put(newString("id"), newInteger(1001))
+ .put(newString("name"), newString("leo"))
+ .put(newString("address"), newArray(newString("xxx-xxxx"), newString("yyy-yyyy")))
+ .put(newString("name"), newString("mitsu"))
+ .build()
+ val i1 = JSON.parseFull(m.toJson)
+ val i2 = JSON.parseFull(m.toString) // expect json value
+ val a1 = JSON.parseFull("""{"id":1001,"name":"mitsu","address":["xxx-xxxx","yyy-yyyy"]}""")
+ // Equals as JSON map
+ i1 shouldBe a1
+ i2 shouldBe a1
+
+ // toJson should quote strings
+ newString("1").toJson shouldBe "\"1\""
+ // toString is for extracting string values
+ newString("1").toString shouldBe "1"
+
+ }
+
}
}
diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java
index 88648c697..1297861f3 100644
--- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java
+++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java
@@ -202,7 +202,7 @@ public JsonToken nextToken()
case STRING:
value = messageUnpacker.unpackValue(var);
if (parsingContext.inObject() && _currToken != JsonToken.FIELD_NAME) {
- parsingContext.setCurrentName(value.asRawValue().stringValue());
+ parsingContext.setCurrentName(value.asRawValue().toString());
nextToken = JsonToken.FIELD_NAME;
}
else {
@@ -212,7 +212,7 @@ public JsonToken nextToken()
case BINARY:
value = messageUnpacker.unpackValue(var);
if (parsingContext.inObject() && _currToken != JsonToken.FIELD_NAME) {
- parsingContext.setCurrentName(value.asRawValue().stringValue());
+ parsingContext.setCurrentName(value.asRawValue().toString());
nextToken = JsonToken.FIELD_NAME;
}
else {
@@ -267,7 +267,7 @@ public String getText()
{
// This method can be called for new BigInteger(text)
if (value.isRawValue()) {
- return value.asRawValue().stringValue();
+ return value.asRawValue().toString();
}
else {
return value.toString();
@@ -305,7 +305,7 @@ public int getTextOffset()
public byte[] getBinaryValue(Base64Variant b64variant)
throws IOException, JsonParseException
{
- return value.asRawValue().getByteArray();
+ return value.asRawValue().asByteArray();
}
@Override
@@ -315,17 +315,17 @@ public Number getNumberValue()
if (value.isIntegerValue()) {
IntegerValue integerValue = value.asIntegerValue();
if (integerValue.isInIntRange()) {
- return integerValue.castAsInt();
+ return integerValue.toInt();
}
else if (integerValue.isInLongRange()) {
- return integerValue.castAsLong();
+ return integerValue.toLong();
}
else {
- return integerValue.castAsBigInteger();
+ return integerValue.toBigInteger();
}
}
else {
- return value.asNumberValue().castAsDouble();
+ return value.asNumberValue().toDouble();
}
}
@@ -333,35 +333,35 @@ else if (integerValue.isInLongRange()) {
public int getIntValue()
throws IOException, JsonParseException
{
- return value.asNumberValue().castAsInt();
+ return value.asNumberValue().toInt();
}
@Override
public long getLongValue()
throws IOException, JsonParseException
{
- return value.asNumberValue().castAsLong();
+ return value.asNumberValue().toLong();
}
@Override
public BigInteger getBigIntegerValue()
throws IOException, JsonParseException
{
- return value.asNumberValue().castAsBigInteger();
+ return value.asNumberValue().toBigInteger();
}
@Override
public float getFloatValue()
throws IOException, JsonParseException
{
- return value.asNumberValue().castAsFloat();
+ return value.asNumberValue().toFloat();
}
@Override
public double getDoubleValue()
throws IOException, JsonParseException
{
- return value.asNumberValue().castAsDouble();
+ return value.asNumberValue().toDouble();
}
@Override
@@ -372,14 +372,14 @@ public BigDecimal getDecimalValue()
IntegerValue number = value.asIntegerValue();
//optimization to not convert the value to BigInteger unnecessarily
if (number.isInLongRange()) {
- return BigDecimal.valueOf(number.castAsLong());
+ return BigDecimal.valueOf(number.toLong());
}
else {
- return new BigDecimal(number.castAsBigInteger());
+ return new BigDecimal(number.toBigInteger());
}
}
else if (value.isFloatValue()) {
- return BigDecimal.valueOf(value.asFloatValue().castAsDouble());
+ return BigDecimal.valueOf(value.asFloatValue().toDouble());
}
else {
throw new UnsupportedOperationException("Couldn't parse value as BigDecimal. " + value);
@@ -391,7 +391,7 @@ public Object getEmbeddedObject()
throws IOException, JsonParseException
{
if (value.isBinaryValue()) {
- return value.asBinaryValue().getByteArray();
+ return value.asBinaryValue().asByteArray();
}
else if (value.isExtensionValue()) {
ExtensionValue extensionValue = value.asExtensionValue();