Skip to content

Commit 31d2fe8

Browse files
committed
Fix Value#toJson
Value#toJson() is expected to return a valid json since its name. Some valid MessagePack values are invalid in JSON such as ExtensionValue, non-string keys in a MapValue, NaN or Infinite.
1 parent b18aecf commit 31d2fe8

20 files changed

+154
-86
lines changed

msgpack-core/src/main/java/org/msgpack/value/ArrayValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* The interface {@code ArrayValue} represents MessagePack's Array type.
23-
* <p/>
23+
*
2424
* MessagePack's Array type can represent sequence of values.
2525
*/
2626
public interface ArrayValue

msgpack-core/src/main/java/org/msgpack/value/BinaryValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* The interface {@code BinaryValue} represents MessagePack's Binary type.
20-
* <p/>
20+
*
2121
* MessagePack's Binary type can represent a byte array at most 2<sup>64</sup>-1 bytes.
2222
*
2323
* @see org.msgpack.value.RawValue

msgpack-core/src/main/java/org/msgpack/value/BooleanValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* The interface {@code BooleanValue} represents MessagePack's Boolean type.
20-
* <p/>
20+
*
2121
* MessagePack's Boolean type can represent {@code true} or {@code false}.
2222
*/
2323
public interface BooleanValue

msgpack-core/src/main/java/org/msgpack/value/ExtensionValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
/**
1919
* The interface {@code ExtensionValue} represents MessagePack's Extension type.
20-
* <p/>
20+
*
2121
* MessagePack's Extension type can represent represents a tuple of type information and a byte array where type information is an
2222
* integer whose meaning is defined by applications.
23-
* <p/>
23+
*
2424
* 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.
2525
*/
2626
public interface ExtensionValue

msgpack-core/src/main/java/org/msgpack/value/FloatValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
/**
1919
* The interface {@code FloatValue} represents MessagePack's Float type.
20-
* <p/>
20+
*
2121
* 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.
2222
*
2323
* @see org.msgpack.value.NumberValue

msgpack-core/src/main/java/org/msgpack/value/IntegerValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* The interface {@code IntegerValue} represents MessagePack's Integer type.
24-
* <p/>
24+
*
2525
* MessagePack's Integer type can represent from -2<sup>63</sup> to 2<sup>64</sup>-1.
2626
*/
2727
public interface IntegerValue

msgpack-core/src/main/java/org/msgpack/value/MapValue.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* The interface {@code ArrayValue} represents MessagePack's Map type.
24-
* <p/>
24+
*
2525
* MessagePack's Map type can represent sequence of key-value pairs.
2626
*/
2727
public interface MapValue
@@ -45,9 +45,9 @@ public interface MapValue
4545

4646
/**
4747
* Returns the key-value pairs as an array of {@code Value}.
48-
* <p/>
48+
*
4949
* Odd elements are keys. Next element of an odd element is a value corresponding to the key.
50-
* <p/>
50+
*
5151
* For example, if this value represents <code>{"k1": "v1", "k2": "v2"}</code>, this method returns ["k1", "v1", "k2", "v2"].
5252
*/
5353
Value[] getKeyValueArray();

msgpack-core/src/main/java/org/msgpack/value/RawValue.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,32 @@ public interface RawValue
3030
{
3131
/**
3232
* Returns the value as {@code byte[]}.
33-
* <p/>
33+
*
3434
* This method copies the byte array.
3535
*/
3636
byte[] asByteArray();
3737

3838
/**
3939
* Returns the value as {@code ByteBuffer}.
40-
* <p/>
40+
*
4141
* Returned ByteBuffer is read-only. See {@code#asReadOnlyBuffer()}.
4242
* This method doesn't copy the byte array as much as possible.
4343
*/
4444
ByteBuffer asByteBuffer();
4545

4646
/**
4747
* Returns the value as {@code String}.
48-
* <p/>
48+
*
4949
* This method throws an exception if the value includes invalid UTF-8 byte sequence.
5050
*
5151
* @throws MessageStringCodingException If this value includes invalid UTF-8 byte sequence.
5252
*/
5353
String asString();
5454

55+
/**
56+
* Returns the value as {@code String}.
57+
*
58+
* This method replaces an invalid UTF-8 byte sequence with <code>U+FFFD replacement character</code>.
59+
*/
60+
String toString();
5561
}

msgpack-core/src/main/java/org/msgpack/value/StringValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
/**
1919
* The interface {@code StringValue} represents MessagePack's String type.
20-
* <p/>
20+
*
2121
* MessagePack's String type can represent a UTF-8 string at most 2<sup>64</sup>-1 bytes.
22-
* <p/>
22+
*
2323
* 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 <code>U+FFFD replacement character</code>.
2424
*
2525
* @see org.msgpack.value.RawValue

msgpack-core/src/main/java/org/msgpack/value/Value.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,101 +30,101 @@ public interface Value
3030
{
3131
/**
3232
* Returns type of this value.
33-
* <p/>
33+
*
3434
* Note that you can't use <code>instanceof</code> to check type of a value because type of a mutable value is variable.
3535
*/
3636
ValueType getValueType();
3737

3838
/**
3939
* Returns immutable copy of this value.
40-
* <p/>
40+
*
4141
* This method simply returns <code>this</code> without copying the value if this value is already immutable.
4242
*/
4343
ImmutableValue immutableValue();
4444

4545
/**
4646
* Returns true if type of this value is Nil.
47-
* <p/>
47+
*
4848
* If this method returns true, {@code asNilValue} never throws exceptions.
4949
* Note that you can't use <code>instanceof</code> or cast <code>((NilValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
5050
*/
5151
boolean isNilValue();
5252

5353
/**
5454
* Returns true if type of this value is Boolean.
55-
* <p/>
55+
*
5656
* If this method returns true, {@code asBooleanValue} never throws exceptions.
5757
* Note that you can't use <code>instanceof</code> or cast <code>((BooleanValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
5858
*/
5959
boolean isBooleanValue();
6060

6161
/**
6262
* Returns true if type of this value is Integer or Float.
63-
* <p/>
63+
*
6464
* If this method returns true, {@code asNumberValue} never throws exceptions.
6565
* Note that you can't use <code>instanceof</code> or cast <code>((NumberValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
6666
*/
6767
boolean isNumberValue();
6868

6969
/**
7070
* Returns true if type of this value is Integer.
71-
* <p/>
71+
*
7272
* If this method returns true, {@code asIntegerValue} never throws exceptions.
7373
* Note that you can't use <code>instanceof</code> or cast <code>((IntegerValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
7474
*/
7575
boolean isIntegerValue();
7676

7777
/**
7878
* Returns true if type of this value is Float.
79-
* <p/>
79+
*
8080
* If this method returns true, {@code asFloatValue} never throws exceptions.
8181
* Note that you can't use <code>instanceof</code> or cast <code>((FloatValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
8282
*/
8383
boolean isFloatValue();
8484

8585
/**
8686
* Returns true if type of this value is String or Binary.
87-
* <p/>
87+
*
8888
* If this method returns true, {@code asRawValue} never throws exceptions.
8989
* Note that you can't use <code>instanceof</code> or cast <code>((RawValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
9090
*/
9191
boolean isRawValue();
9292

9393
/**
9494
* Returns true if type of this value is Binary.
95-
* <p/>
95+
*
9696
* If this method returns true, {@code asBinaryValue} never throws exceptions.
9797
* Note that you can't use <code>instanceof</code> or cast <code>((BinaryValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
9898
*/
9999
boolean isBinaryValue();
100100

101101
/**
102102
* Returns true if type of this value is String.
103-
* <p/>
103+
*
104104
* If this method returns true, {@code asStringValue} never throws exceptions.
105105
* Note that you can't use <code>instanceof</code> or cast <code>((StringValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
106106
*/
107107
boolean isStringValue();
108108

109109
/**
110110
* Returns true if type of this value is Array.
111-
* <p/>
111+
*
112112
* If this method returns true, {@code asArrayValue} never throws exceptions.
113113
* Note that you can't use <code>instanceof</code> or cast <code>((ArrayValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
114114
*/
115115
boolean isArrayValue();
116116

117117
/**
118118
* Returns true if type of this value is Map.
119-
* <p/>
119+
*
120120
* If this method returns true, {@code asMapValue} never throws exceptions.
121121
* Note that you can't use <code>instanceof</code> or cast <code>((MapValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
122122
*/
123123
boolean isMapValue();
124124

125125
/**
126126
* Returns true if type of this an Extension.
127-
* <p/>
127+
*
128128
* If this method returns true, {@code asExtensionValue} never throws exceptions.
129129
* Note that you can't use <code>instanceof</code> or cast <code>((ExtensionValue) thisValue)</code> to check type of a value because
130130
* type of a mutable value is variable.
@@ -133,7 +133,7 @@ public interface Value
133133

134134
/**
135135
* Returns the value as {@code NilValue}. Otherwise throws {@code MessageTypeCastException}.
136-
* <p/>
136+
*
137137
* Note that you can't use <code>instanceof</code> or cast <code>((NilValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
138138
*
139139
* @throws MessageTypeCastException If type of this value is not Nil.
@@ -142,7 +142,7 @@ public interface Value
142142

143143
/**
144144
* Returns the value as {@code BooleanValue}. Otherwise throws {@code MessageTypeCastException}.
145-
* <p/>
145+
*
146146
* Note that you can't use <code>instanceof</code> or cast <code>((BooleanValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
147147
*
148148
* @throws MessageTypeCastException If type of this value is not Boolean.
@@ -151,7 +151,7 @@ public interface Value
151151

152152
/**
153153
* Returns the value as {@code NumberValue}. Otherwise throws {@code MessageTypeCastException}.
154-
* <p/>
154+
*
155155
* Note that you can't use <code>instanceof</code> or cast <code>((NumberValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
156156
*
157157
* @throws MessageTypeCastException If type of this value is not Integer or Float.
@@ -160,7 +160,7 @@ public interface Value
160160

161161
/**
162162
* Returns the value as {@code IntegerValue}. Otherwise throws {@code MessageTypeCastException}.
163-
* <p/>
163+
*
164164
* Note that you can't use <code>instanceof</code> or cast <code>((IntegerValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
165165
*
166166
* @throws MessageTypeCastException If type of this value is not Integer.
@@ -169,7 +169,7 @@ public interface Value
169169

170170
/**
171171
* Returns the value as {@code FloatValue}. Otherwise throws {@code MessageTypeCastException}.
172-
* <p/>
172+
*
173173
* Note that you can't use <code>instanceof</code> or cast <code>((FloatValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
174174
*
175175
* @throws MessageTypeCastException If type of this value is not Float.
@@ -178,7 +178,7 @@ public interface Value
178178

179179
/**
180180
* Returns the value as {@code RawValue}. Otherwise throws {@code MessageTypeCastException}.
181-
* <p/>
181+
*
182182
* Note that you can't use <code>instanceof</code> or cast <code>((RawValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
183183
*
184184
* @throws MessageTypeCastException If type of this value is not Binary or String.
@@ -187,7 +187,7 @@ public interface Value
187187

188188
/**
189189
* Returns the value as {@code BinaryValue}. Otherwise throws {@code MessageTypeCastException}.
190-
* <p/>
190+
*
191191
* Note that you can't use <code>instanceof</code> or cast <code>((BinaryValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
192192
*
193193
* @throws MessageTypeCastException If type of this value is not Binary.
@@ -196,7 +196,7 @@ public interface Value
196196

197197
/**
198198
* Returns the value as {@code StringValue}. Otherwise throws {@code MessageTypeCastException}.
199-
* <p/>
199+
*
200200
* Note that you can't use <code>instanceof</code> or cast <code>((StringValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
201201
*
202202
* @throws MessageTypeCastException If type of this value is not String.
@@ -205,7 +205,7 @@ public interface Value
205205

206206
/**
207207
* Returns the value as {@code ArrayValue}. Otherwise throws {@code MessageTypeCastException}.
208-
* <p/>
208+
*
209209
* Note that you can't use <code>instanceof</code> or cast <code>((ArrayValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
210210
*
211211
* @throws MessageTypeCastException If type of this value is not Array.
@@ -214,7 +214,7 @@ public interface Value
214214

215215
/**
216216
* Returns the value as {@code MapValue}. Otherwise throws {@code MessageTypeCastException}.
217-
* <p/>
217+
*
218218
* Note that you can't use <code>instanceof</code> or cast <code>((MapValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
219219
*
220220
* @throws MessageTypeCastException If type of this value is not Map.
@@ -223,7 +223,7 @@ public interface Value
223223

224224
/**
225225
* Returns the value as {@code ExtensionValue}. Otherwise throws {@code MessageTypeCastException}.
226-
* <p/>
226+
*
227227
* Note that you can't use <code>instanceof</code> or cast <code>((ExtensionValue) thisValue)</code> to check type of a value
228228
* because type of a mutable value is variable.
229229
*
@@ -241,17 +241,22 @@ void writeTo(MessagePacker pk)
241241

242242
/**
243243
* Compares this value to the specified object.
244-
* <p/>
244+
*
245245
* This method returns {@code true} if type and value are equivalent.
246246
* If this value is {@code MapValue} or {@code ArrayValue}, this method check equivalence of elements recursively.
247247
*/
248248
boolean equals(Object obj);
249249

250250
/**
251-
* Returns json representation of this Value for debugging purpose.
252-
* This output json format is subject to change in future.
253-
* Do not write code that depends on the resulting json format.
251+
* Returns json representation of this Value.
252+
*
253+
* Following behavior is not configurable at this release and they might be changed at future releases:
254+
*
255+
* * if a key of MapValue is not string, the key is converted to a string using toString method.
256+
* * NaN and Infinity of DoubleValue are converted to null.
257+
* * ExtensionValue is converted to a 2-element array where first element is a number and second element is the data encoded in hex.
258+
* * BinaryValue is converted to a string using UTF-8 encoding. Invalid byte sequence is replaced with <code>U+FFFD replacement character</code>.
259+
* * Invalid UTF-8 byte sequences in StringValue is replaced with <code>U+FFFD replacement character</code>
254260
*/
255261
String toJson();
256-
257262
}

0 commit comments

Comments
 (0)