Skip to content

Commit 7d26d7b

Browse files
committed
Fix #93
1 parent 8a1866e commit 7d26d7b

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

cbor/src/main/java/com/fasterxml/jackson/dataformat/cbor/CBORParser.java

+21
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,9 @@ public JsonToken nextToken() throws IOException
754754
return (_currToken = JsonToken.VALUE_TRUE);
755755
case 22:
756756
return (_currToken = JsonToken.VALUE_NULL);
757+
case 23:
758+
return (_currToken = _decodeUndefinedValue());
759+
757760
case 25: // 16-bit float...
758761
// As per [http://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript]
759762
{
@@ -1229,6 +1232,10 @@ public String nextTextValue() throws IOException
12291232
case 22:
12301233
_currToken = JsonToken.VALUE_NULL;
12311234
return null;
1235+
case 23:
1236+
_currToken = _decodeUndefinedValue();
1237+
return null;
1238+
12321239
case 25: // 16-bit float...
12331240
// As per [http://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript]
12341241
{
@@ -2888,6 +2895,20 @@ private final static long _long(int i1, int i2)
28882895
return (l1 << 32) + l2;
28892896
}
28902897

2898+
/**
2899+
* Helper method to encapsulate details of handling of mysterious `undefined` value
2900+
* that is allowed to be used as something encoder could not handle (as per spec),
2901+
* whatever the heck that should be.
2902+
* Current definition for 2.9 is that we will be return {@link JsonToken#VALUE_NULL}, but
2903+
* for later versions it is likely that we will alternatively allow decoding as
2904+
* {@link JsonToken#VALUE_EMBEDDED_OBJECT} with "embedded value" of `null`.
2905+
*
2906+
* @since 2.9.6
2907+
*/
2908+
protected JsonToken _decodeUndefinedValue() throws IOException {
2909+
return JsonToken.VALUE_NULL;
2910+
}
2911+
28912912
/*
28922913
/**********************************************************
28932914
/* Internal methods, UTF8 decoding
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fasterxml.jackson.dataformat.cbor.parse;
2+
3+
import java.io.ByteArrayOutputStream;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.JsonToken;
7+
import com.fasterxml.jackson.dataformat.cbor.CBORConstants;
8+
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
9+
import com.fasterxml.jackson.dataformat.cbor.CBORGenerator;
10+
import com.fasterxml.jackson.dataformat.cbor.CBORTestBase;
11+
12+
// for [dataformat-binary#93]
13+
public class UndefinedValueTest extends CBORTestBase
14+
{
15+
private final static byte BYTE_UNDEFINED = (byte) 0xF7;
16+
17+
private final CBORFactory CBOR_F = cborFactory();
18+
19+
public void testUndefinedLiteralStreaming() throws Exception
20+
{
21+
JsonParser p = cborParser(CBOR_F, new byte[] { BYTE_UNDEFINED });
22+
assertEquals(JsonToken.VALUE_NULL, p.nextToken());
23+
assertNull(p.nextToken());
24+
p.close();
25+
}
26+
27+
public void testUndefinedInArray() throws Exception
28+
{
29+
ByteArrayOutputStream out = new ByteArrayOutputStream();
30+
out.write(CBORConstants.BYTE_ARRAY_INDEFINITE);
31+
out.write(BYTE_UNDEFINED);
32+
out.write(CBORConstants.BYTE_BREAK);
33+
JsonParser p = cborParser(CBOR_F, out.toByteArray());
34+
assertEquals(JsonToken.START_ARRAY, p.nextToken());
35+
assertEquals(JsonToken.VALUE_NULL, p.nextToken());
36+
assertEquals(JsonToken.END_ARRAY, p.nextToken());
37+
assertNull(p.nextToken());
38+
p.close();
39+
}
40+
41+
public void testUndefinedInObject() throws Exception
42+
{
43+
ByteArrayOutputStream out = new ByteArrayOutputStream();
44+
CBORGenerator g = cborGenerator(out);
45+
g.writeStartObject();
46+
g.writeFieldName("bar");
47+
g.writeBoolean(true);
48+
g.writeEndObject();
49+
g.close();
50+
51+
byte[] doc = out.toByteArray();
52+
// assume we use end marker for Object, so
53+
doc[doc.length-2] = BYTE_UNDEFINED;
54+
55+
JsonParser p = cborParser(CBOR_F, doc);
56+
assertEquals(JsonToken.START_OBJECT, p.nextToken());
57+
assertEquals(JsonToken.FIELD_NAME, p.nextToken());
58+
assertEquals("bar", p.currentName());
59+
assertEquals(JsonToken.VALUE_NULL, p.nextToken());
60+
assertEquals(JsonToken.END_OBJECT, p.nextToken());
61+
assertNull(p.nextToken());
62+
p.close();
63+
}
64+
}

release-notes/VERSION

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Project: jackson-datatypes-binary
2-
Modules:
1+
Project: jackson-datatypes-binaryModules:
32
jackson-dataformat-avro
43
jackson-dataformat-cbor
54
jackson-dataformat-protobuf
@@ -11,6 +10,8 @@ Modules:
1110

1211
2.9.6 (not yet released)
1312

13+
#93: (cbor) `CBORParser` does not accept "undefined value"
14+
(reported by mbaril@github)
1415
#135: (protobuf) Infinite sequence of `END_OBJECT` tokens returned at end of streaming read
1516
(reported by Leo W)
1617
#136: (avro) Fix MapWriteContext not correctly resolving union values

0 commit comments

Comments
 (0)