Skip to content

Commit 0568f83

Browse files
committed
Implement first part of #17, parsing BigInteger
1 parent 119b9cb commit 0568f83

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,16 @@ public final class CBORConstants
8282
public final static byte BYTE_FLOAT16 = (byte) (PREFIX_TYPE_MISC + 25);
8383
public final static byte BYTE_FLOAT32 = (byte) (PREFIX_TYPE_MISC + 26);
8484
public final static byte BYTE_FLOAT64 = (byte) (PREFIX_TYPE_MISC + 27);
85+
86+
public final static int TAG_BIGNUM_POS = 2;
87+
public final static int TAG_BIGNUM_NEG = 3;
88+
public final static int TAG_DECIMAL_FRACTION = 4;
89+
public final static int TAG_BIGFLOAT = 5;
8590

86-
public final static byte BYTE_TAG_BIGNUM_POS = (byte) (PREFIX_TYPE_TAG + 2);
87-
public final static byte BYTE_TAG_BIGNUM_NEG = (byte) (PREFIX_TYPE_TAG + 3);
88-
public final static byte BYTE_TAG_DECIMAL_FRACTION = (byte) (PREFIX_TYPE_TAG + 4);
89-
public final static byte BYTE_TAG_BIGFLOAT = (byte) (PREFIX_TYPE_TAG + 5);
91+
public final static byte BYTE_TAG_BIGNUM_POS = (byte) (PREFIX_TYPE_TAG + TAG_BIGNUM_POS);
92+
public final static byte BYTE_TAG_BIGNUM_NEG = (byte) (PREFIX_TYPE_TAG + TAG_BIGNUM_NEG);
93+
public final static byte BYTE_TAG_DECIMAL_FRACTION = (byte) (PREFIX_TYPE_TAG + TAG_DECIMAL_FRACTION);
94+
public final static byte BYTE_TAG_BIGFLOAT = (byte) (PREFIX_TYPE_TAG + TAG_BIGFLOAT);
9095

9196
public final static byte BYTE_BREAK = (byte) 0xFF;
9297

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,16 +511,18 @@ public final void writeEndObject() throws IOException
511511
_writeByte(BYTE_BREAK);
512512
}
513513

514-
/*
515514
@Override // since 2.8
516515
public void writeArray(int[] array, int offset, int length) throws IOException
517516
{
517+
/*
518518
// short-cut, do not create child array context etc
519519
_verifyValueWrite("start an object");
520520
521521
// !!! TODO
522+
*/
523+
524+
super.writeArray(array, offset, length);
522525
}
523-
*/
524526

525527
/*
526528
/**********************************************************
@@ -529,7 +531,7 @@ public void writeArray(int[] array, int offset, int length) throws IOException
529531
*/
530532

531533
@Override
532-
public void writeString(String text) throws IOException,JsonGenerationException
534+
public void writeString(String text) throws IOException
533535
{
534536
if (text == null) {
535537
writeNull();

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,9 @@ public JsonToken nextToken() throws IOException
712712
case 2: // byte[]
713713
_typeByte = ch;
714714
_tokenIncomplete = true;
715+
if (_tagValue >= 0) {
716+
return _nextBinaryWithTag(_tagValue);
717+
}
715718
return (_currToken = JsonToken.VALUE_EMBEDDED_OBJECT);
716719

717720
case 3: // String
@@ -777,16 +780,6 @@ public JsonToken nextToken() throws IOException
777780
}
778781
return null;
779782
}
780-
781-
protected JsonToken _handleCBOREOF() throws IOException {
782-
/* NOTE: here we can and should close input, release buffers,
783-
* since this is "hard" EOF, not a boundary imposed by
784-
* header token.
785-
*/
786-
_tagValue = -1;
787-
close();
788-
return (_currToken = null);
789-
}
790783

791784
protected String _numberToName(int ch, boolean neg) throws IOException
792785
{
@@ -823,17 +816,33 @@ protected String _numberToName(int ch, boolean neg) throws IOException
823816
return String.valueOf(1);
824817
}
825818

826-
// base impl is fine:
827-
//public String getCurrentName() throws IOException
828-
829-
protected void _invalidToken(int ch) throws JsonParseException {
830-
ch &= 0xFF;
831-
if (ch == 0xFF) {
832-
throw _constructError("Mismatched BREAK byte (0xFF): encountered where value expected");
819+
protected JsonToken _nextBinaryWithTag(int tag) throws IOException
820+
{
821+
// For now all we should get is BigInteger
822+
823+
boolean neg;
824+
if (tag == CBORConstants.TAG_BIGNUM_POS) {
825+
neg = false;
826+
} else if (tag == CBORConstants.TAG_BIGNUM_NEG) {
827+
neg = true;
828+
} else {
829+
// 12-May-2016, tatu: Since that's all we know, let's otherwise
830+
// just return default Binary data marker
831+
return (_currToken = JsonToken.VALUE_EMBEDDED_OBJECT);
833832
}
834-
throw _constructError("Invalid CBOR value token (first byte): 0x"+Integer.toHexString(ch));
833+
834+
// First: get the data
835+
_finishToken();
836+
837+
_numberBigInt = new BigInteger(_binaryValue);
838+
_numTypesValid |= NR_BIGINT;
839+
840+
return (_currToken = JsonToken.VALUE_NUMBER_INT);
835841
}
836842

843+
// base impl is fine:
844+
//public String getCurrentName() throws IOException
845+
837846
/**
838847
* Method for forcing full read of current token, even if it might otherwise
839848
* only be read if data is accessed via {@link #getText} and similar methods.
@@ -2939,10 +2948,28 @@ protected void _handleEOF() throws JsonParseException {
29392948

29402949
/*
29412950
/**********************************************************
2942-
/* Internal methods, error reporting
2951+
/* Internal methods, error handling, reporting
29432952
/**********************************************************
29442953
*/
29452954

2955+
protected JsonToken _handleCBOREOF() throws IOException {
2956+
/* NOTE: here we can and should close input, release buffers,
2957+
* since this is "hard" EOF, not a boundary imposed by
2958+
* header token.
2959+
*/
2960+
_tagValue = -1;
2961+
close();
2962+
return (_currToken = null);
2963+
}
2964+
2965+
protected void _invalidToken(int ch) throws JsonParseException {
2966+
ch &= 0xFF;
2967+
if (ch == 0xFF) {
2968+
throw _constructError("Mismatched BREAK byte (0xFF): encountered where value expected");
2969+
}
2970+
throw _constructError("Invalid CBOR value token (first byte): 0x"+Integer.toHexString(ch));
2971+
}
2972+
29462973
protected void _reportUnexpectedBreak() throws IOException {
29472974
if (_parsingContext.inRoot()) {
29482975
throw _constructError("Unexpected Break (0xFF) token in Root context");

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/failing/BigNumbersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void testBigInteger() throws Exception
3333

3434
// but verify that the original content can be parsed
3535
CBORParser parser = cborParser(sourceBytes.toByteArray());
36-
assertToken(JsonToken.VALUE_NUMBER_FLOAT, parser.nextToken());
36+
assertToken(JsonToken.VALUE_NUMBER_INT, parser.nextToken());
3737
assertEquals(BigInteger.TEN, parser.getBigIntegerValue());
3838
parser.close();
3939
}

0 commit comments

Comments
 (0)