Skip to content

Commit d65d633

Browse files
committed
...
1 parent d06a1e9 commit d65d633

File tree

6 files changed

+100
-40
lines changed

6 files changed

+100
-40
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,13 @@ public final void writeFieldName(SerializableString name)
283283
_tokenWriteContext.writeFieldName(name.getValue());
284284
}
285285

286+
@Override
287+
public void writeFieldId(long id) throws IOException {
288+
// TODO: Should not force construction of a String here...
289+
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
290+
_tokenWriteContext.writeFieldName(idStr);
291+
}
292+
286293
@Override
287294
public final void writeStringField(String fieldName, String value)
288295
throws IOException
@@ -361,6 +368,12 @@ public final void writeStartArray() throws IOException {
361368
_complete = false;
362369
}
363370

371+
@Override
372+
public final void writeStartArray(Object currValue) throws IOException {
373+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(currValue);
374+
_complete = false;
375+
}
376+
364377
@Override
365378
public final void writeStartArray(Object currValue, int len) throws IOException {
366379
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(currValue);

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,21 @@ public final void writeStartArray() throws IOException {
509509
_writeByte(BYTE_ARRAY_INDEFINITE);
510510
}
511511

512-
/*
513-
* Unlike with JSON, this method is using slightly optimized version since
514-
* CBOR has a variant that allows embedding length in array start marker.
515-
*/
516-
517512
@Override
518-
public void writeStartArray(int elementsToWrite) throws IOException {
513+
public void writeStartArray(Object currValue) throws IOException {
519514
_verifyValueWrite("start an array");
520-
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(null);
521-
_pushRemainingElements();
522-
_currentRemainingElements = elementsToWrite;
523-
_writeLengthMarker(PREFIX_TYPE_ARRAY, elementsToWrite);
515+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(currValue);
516+
if (_elementCountsPtr > 0) {
517+
_pushRemainingElements();
518+
}
519+
_currentRemainingElements = INDEFINITE_LENGTH;
520+
_writeByte(BYTE_ARRAY_INDEFINITE);
524521
}
525522

523+
/*
524+
* Unlike with JSON, this method is using slightly optimized version since
525+
* CBOR has a variant that allows embedding length in array start marker.
526+
*/
526527
@Override
527528
public void writeStartArray(Object forValue, int elementsToWrite) throws IOException {
528529
_verifyValueWrite("start an array");
@@ -563,9 +564,9 @@ public final void writeStartObject(Object forValue) throws IOException {
563564
_writeByte(BYTE_OBJECT_INDEFINITE);
564565
}
565566

566-
public final void writeStartObject(int elementsToWrite) throws IOException {
567+
public final void writeStartObject(Object forValue, int elementsToWrite) throws IOException {
567568
_verifyValueWrite("start an object");
568-
_tokenWriteContext = _tokenWriteContext.createChildObjectContext(null);
569+
_tokenWriteContext = _tokenWriteContext.createChildObjectContext(forValue);
569570
_pushRemainingElements();
570571
_currentRemainingElements = elementsToWrite;
571572
_writeLengthMarker(PREFIX_TYPE_OBJECT, elementsToWrite);

cbor/src/test/java/com/fasterxml/jackson/dataformat/cbor/MapAndArrayTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ public void testCborSizedMap() throws IOException {
107107
ByteArrayOutputStream payloadOut = new ByteArrayOutputStream();
108108
CBORGenerator gen = cborGenerator(payloadOut);
109109

110-
gen.writeStartObject(5);
110+
gen.writeStartObject(null, 5);
111111
gen.writeFieldId(1504);
112112
gen.writeNumber(-33);
113113
gen.writeFieldId(1505);
114114
gen.writeBoolean(false);
115115
gen.writeFieldId(1506);
116116
gen.writeString("Fun");
117117
gen.writeFieldName("Amt");
118-
gen.writeStartArray(2);
118+
gen.writeStartArray(null, 2);
119119
gen.writeNumber(2);
120120
gen.writeNumber(3);
121121
gen.writeEndArray();
@@ -220,7 +220,7 @@ public void testCborMultilevelMapWithMultilevelArrays() throws IOException {
220220
ByteArrayOutputStream payloadOut = new ByteArrayOutputStream();
221221
CBORGenerator gen = cborGenerator(payloadOut);
222222

223-
gen.writeStartObject(4);
223+
gen.writeStartObject(null, 4);
224224
gen.writeFieldName("a");
225225
gen.writeNumber(1);
226226

@@ -230,18 +230,18 @@ public void testCborMultilevelMapWithMultilevelArrays() throws IOException {
230230
gen.writeNumber(3);
231231
gen.writeEndArray();
232232
gen.writeFieldId(1501);
233-
gen.writeStartArray(5);
233+
gen.writeStartArray(null, 5);
234234
gen.writeString("Fun");
235235
gen.writeNumber(44);
236236
gen.writeStartArray();
237237
gen.writeNumber(45);
238238
gen.writeNumber(46);
239-
gen.writeStartArray(2);
239+
gen.writeStartArray(null, 2);
240240
gen.writeNumber(47);
241241
gen.writeNumber(48);
242242
gen.writeEndArray();
243243
gen.writeEndArray();
244-
gen.writeStartObject(2);
244+
gen.writeStartObject(null, 2);
245245
gen.writeFieldName("key");
246246
gen.writeStartObject();
247247
gen.writeFieldName("complex");
@@ -257,7 +257,7 @@ public void testCborMultilevelMapWithMultilevelArrays() throws IOException {
257257
gen.writeFieldId(54);
258258
gen.writeString("value");
259259
gen.writeFieldId(55);
260-
gen.writeStartObject(2);
260+
gen.writeStartObject(null, 2);
261261
gen.writeFieldId(56);
262262
gen.writeNumber(61);
263263
gen.writeFieldId(57);
@@ -282,7 +282,7 @@ public void testCborUnsizedMapWithAllInside() throws IOException {
282282

283283
gen.writeStartObject();
284284
gen.writeFieldId(1504);
285-
gen.writeStartObject(1);
285+
gen.writeStartObject(null, 1);
286286
gen.writeFieldId(2504);
287287
gen.writeNumber(-33);
288288
gen.writeEndObject();
@@ -316,7 +316,7 @@ public void testCborArraysInArray() throws IOException {
316316

317317
gen.writeStartArray();
318318
gen.writeNumber(1);
319-
gen.writeStartArray(2);
319+
gen.writeStartArray(null, 2);
320320
gen.writeNumber(2);
321321
gen.writeNumber(3);
322322
gen.writeEndArray();
@@ -340,28 +340,28 @@ public void testCborArraysInUnsizedArray() throws IOException {
340340
CBORGenerator gen = cborGenerator(payloadOut);
341341
gen.writeStartArray();
342342
gen.writeNumber(1);
343-
gen.writeStartArray(2);
343+
gen.writeStartArray(null, 2);
344344
gen.writeNumber(2);
345345
gen.writeNumber(3);
346346
gen.writeEndArray();
347347
gen.writeStartArray();
348348
gen.writeNumber(4);
349349
gen.writeNumber(5);
350350
gen.writeEndArray();
351-
gen.writeStartArray(3);
351+
gen.writeStartArray(null, 3);
352352
gen.writeNumber(6);
353353
gen.writeNumber(7);
354354
gen.writeStartArray();
355355
gen.writeNumber(8);
356356
gen.writeNumber(8);
357357
gen.writeNumber(8);
358-
gen.writeStartArray(2);
358+
gen.writeStartArray(null, 2);
359359
gen.writeNumber(1);
360360
gen.writeNumber(1);
361361
gen.writeEndArray();
362362
gen.writeEndArray();
363363
gen.writeEndArray();
364-
gen.writeStartArray(2);
364+
gen.writeStartArray(null, 2);
365365
gen.writeNumber(9);
366366
gen.writeNumber(9);
367367
gen.writeEndArray();
@@ -385,7 +385,7 @@ public void testCborArraysInSizedArray() throws IOException {
385385
ByteArrayOutputStream payloadOut = new ByteArrayOutputStream();
386386
CBORGenerator gen = cborGenerator(payloadOut);
387387

388-
gen.writeStartArray(4);// [
388+
gen.writeStartArray(null, 4);// [
389389
gen.writeNumber(1); // [1
390390
gen.writeStartArray(); // [1,[_
391391
gen.writeNumber(2); // [1,[_2,
@@ -394,7 +394,7 @@ public void testCborArraysInSizedArray() throws IOException {
394394
gen.writeEndArray(); // [1,[_2,3,4,_]
395395
gen.writeStartArray(); // [1,[_2,3,4,_][_
396396
gen.writeNumber(4); // [1,[_2,3,4,_][_4,
397-
gen.writeStartArray(2);// [1,[_2,3,4,_][_4,[
397+
gen.writeStartArray(null, 2);// [1,[_2,3,4,_][_4,[
398398
gen.writeNumber(5); // [1,[_2,3,4,_][_4,[5,
399399
gen.writeStartArray(); // [1,[_2,3,4,_][_4,[5,[
400400
gen.writeNumber(6); // [1,[_2,3,4,_][_4,[5,[_6
@@ -403,7 +403,7 @@ public void testCborArraysInSizedArray() throws IOException {
403403
gen.writeEndArray(); // [1,[_2,3,4,_][_4,[5,[_6,6,6]
404404
gen.writeEndArray(); // [1,[_2,3,4,_][_4,[5,[_6,6,6]]
405405
gen.writeEndArray(); // [1,[_2,3,4,_][_4,[5,[_6,6,6]]]
406-
gen.writeStartArray(3);// [1,[_2,3,4,_][_4,[5,[_6,6,6]]],[
406+
gen.writeStartArray(null, 3);// [1,[_2,3,4,_][_4,[5,[_6,6,6]]],[
407407
gen.writeNumber(7); // [1,[_2,3,4,_][_4,[5,[_6,6,6]]],[7
408408
gen.writeNumber(8); // [1,[_2,3,4,_][_4,[5,[_6,6,6]]],[7,8
409409
gen.writeStartArray(); // [1,[_2,3,4,_][_4,[5,[_6,6,6]]],[7,8,[_
@@ -444,9 +444,9 @@ public void testCborSizedArray() throws IOException {
444444
CBORGenerator gen = cborGenerator(payloadOut);
445445
int size_finite_array = 258;
446446

447-
gen.writeStartArray(3);
447+
gen.writeStartArray(null, 3);
448448
gen.writeNumber(33);
449-
gen.writeStartArray(size_finite_array - 2);
449+
gen.writeStartArray(null, size_finite_array - 2);
450450
for (int i = 0; i < size_finite_array - 2; i++) {
451451
gen.writeNumber(i + 1);
452452
}
@@ -467,7 +467,7 @@ public void testCborSizedArrayWithMap() throws IOException {
467467
ByteArrayOutputStream payloadOut = new ByteArrayOutputStream();
468468
CBORGenerator gen = cborGenerator(payloadOut);
469469

470-
gen.writeStartArray(2);
470+
gen.writeStartArray(null, 2);
471471
gen.writeString("a");
472472
gen.writeStartObject();
473473
gen.writeFieldName("b");

ion/src/main/java/com/fasterxml/jackson/dataformat/ion/IonGenerator.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ protected void _verifyValueWrite(String msg) throws IOException, JsonGenerationE
440440

441441
@Override
442442
public void writeEndArray() throws IOException, JsonGenerationException {
443-
_tokenWriteContext = _tokenWriteContext.getParent(); // <-- copied from UTF8JsonGenerator
443+
_tokenWriteContext = _tokenWriteContext.getParent();
444444
_writer.stepOut();
445445
}
446446

447447
@Override
448448
public void writeEndObject() throws IOException, JsonGenerationException {
449-
_tokenWriteContext = _tokenWriteContext.getParent(); // <-- copied from UTF8JsonGenerator
449+
_tokenWriteContext = _tokenWriteContext.getParent();
450450
_writer.stepOut();
451451
}
452452

@@ -459,27 +459,48 @@ public void writeFieldName(String value) throws IOException, JsonGenerationExcep
459459

460460
_writeFieldName(value);
461461
}
462-
463-
protected void _writeFieldName(String value) throws IOException, JsonGenerationException {
462+
463+
@Override
464+
public void writeFieldId(long id) throws IOException {
465+
// Should not force construction of a String here...
466+
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
467+
writeFieldName(idStr);
468+
}
469+
470+
protected void _writeFieldName(String value) throws IOException {
464471
//Even though this is a one-liner, putting it into a function "_writeFieldName"
465472
//to keep this code matching the factoring in Jackson's UTF8JsonGenerator.
466473
_writer.setFieldName(value);
467474
}
468475

469476
@Override
470-
public void writeStartArray() throws IOException, JsonGenerationException {
477+
public void writeStartArray() throws IOException {
471478
_verifyValueWrite("start an array");
472479
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(null);
473480
_writer.stepIn(IonType.LIST);
474481
}
475482

476483
@Override
477-
public void writeStartObject() throws IOException, JsonGenerationException {
484+
public void writeStartArray(Object currValue) throws IOException {
485+
_verifyValueWrite("start an array");
486+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(currValue);
487+
_writer.stepIn(IonType.LIST);
488+
}
489+
490+
@Override
491+
public void writeStartObject() throws IOException {
478492
_verifyValueWrite("start an object");
479493
_tokenWriteContext = _tokenWriteContext.createChildObjectContext(null);
480494
_writer.stepIn(IonType.STRUCT);
481495
}
482496

497+
@Override
498+
public void writeStartObject(Object currValue) throws IOException {
499+
_verifyValueWrite("start an object");
500+
_tokenWriteContext = _tokenWriteContext.createChildObjectContext(currValue);
501+
_writer.stepIn(IonType.STRUCT);
502+
}
503+
483504
/*
484505
/*****************************************************************
485506
/* Support for type ids

protobuf/src/main/java/com/fasterxml/jackson/dataformat/protobuf/ProtobufGenerator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ public final void writeFieldName(SerializableString sstr) throws IOException {
293293
_currField = f;
294294
}
295295

296+
@Override
297+
public void writeFieldId(long id) throws IOException {
298+
// 24-Jul-2019, tatu: Should not force construction of a String here...
299+
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
300+
writeFieldName(idStr);
301+
}
302+
296303
/*
297304
/**********************************************************
298305
/* Public API: low-level I/O
@@ -385,7 +392,7 @@ public final void writeStartArray() throws IOException
385392
}
386393

387394
@Override
388-
public final void writeStartArray(Object currValue, int len) throws IOException {
395+
public final void writeStartArray(Object currValue) throws IOException {
389396
writeStartArray();
390397
_tokenWriteContext.setCurrentValue(currValue);
391398
}

smile/src/main/java/com/fasterxml/jackson/dataformat/smile/SmileGenerator.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,16 @@ public final void writeFieldName(SerializableString name)
497497
_writeFieldName(name);
498498
}
499499

500+
@Override
501+
public void writeFieldId(long id) throws IOException {
502+
// 24-Jul-2019, tatu: Should not force construction of a String here...
503+
String idStr = Long.valueOf(id).toString(); // since instances for small values cached
504+
if (!_tokenWriteContext.writeFieldName(idStr)) {
505+
_reportError("Can not write a field name, expecting a value");
506+
}
507+
_writeFieldName(idStr);
508+
}
509+
500510
@Override
501511
public final void writeStringField(String fieldName, String value)
502512
throws IOException
@@ -582,10 +592,10 @@ public final void writeStartArray() throws IOException
582592
}
583593

584594
@Override
585-
public final void writeStartArray(int size) throws IOException
595+
public final void writeStartArray(Object forValue) throws IOException
586596
{
587597
_verifyValueWrite("start an array");
588-
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(null);
598+
_tokenWriteContext = _tokenWriteContext.createChildArrayContext(forValue);
589599
_writeByte(TOKEN_LITERAL_START_ARRAY);
590600
}
591601

@@ -623,6 +633,14 @@ public final void writeStartObject(Object forValue) throws IOException
623633
_writeByte(TOKEN_LITERAL_START_OBJECT);
624634
}
625635

636+
@Override
637+
public final void writeStartObject(Object forValue, int size) throws IOException
638+
{
639+
_verifyValueWrite("start an object");
640+
_tokenWriteContext = _tokenWriteContext.createChildObjectContext(forValue);
641+
_writeByte(TOKEN_LITERAL_START_OBJECT);
642+
}
643+
626644
@Override
627645
public final void writeEndObject() throws IOException
628646
{

0 commit comments

Comments
 (0)