8
8
9
9
import com .fasterxml .jackson .core .*;
10
10
import com .fasterxml .jackson .core .io .*;
11
- import com .fasterxml .jackson .core .json .JsonWriteContext ;
11
+ import com .fasterxml .jackson .core .json .DupDetector ;
12
12
import com .fasterxml .jackson .core .base .GeneratorBase ;
13
13
14
14
import static com .fasterxml .jackson .dataformat .smile .SmileConstants .*;
@@ -178,6 +178,17 @@ public SharedStringNode(String value, int index, SharedStringNode next)
178
178
*/
179
179
final protected SmileBufferRecycler <SharedStringNode > _smileBufferRecycler ;
180
180
181
+ /*
182
+ /**********************************************************
183
+ /* Output state
184
+ /**********************************************************
185
+ */
186
+
187
+ /**
188
+ * @since 2.10
189
+ */
190
+ protected SmileWriteContext _smileContext ;
191
+
181
192
/*
182
193
/**********************************************************
183
194
/* Output buffering
@@ -265,10 +276,15 @@ public SharedStringNode(String value, int index, SharedStringNode next)
265
276
/**********************************************************
266
277
*/
267
278
268
- public SmileGenerator (IOContext ctxt , int jsonFeatures , int smileFeatures ,
279
+ public SmileGenerator (IOContext ctxt , int stdFeatures , int smileFeatures ,
269
280
ObjectCodec codec , OutputStream out )
270
281
{
271
- super (jsonFeatures , codec );
282
+ super (stdFeatures , codec , /*WriteContext*/ null );
283
+ DupDetector dups = JsonGenerator .Feature .STRICT_DUPLICATE_DETECTION .enabledIn (stdFeatures )
284
+ ? DupDetector .rootDetector (this )
285
+ : null ;
286
+ // NOTE: we passed `null` for default write context
287
+ _smileContext = SmileWriteContext .createRootContext (dups );
272
288
_formatFeatures = smileFeatures ;
273
289
_ioContext = ctxt ;
274
290
_smileBufferRecycler = _smileBufferRecycler ();
@@ -305,11 +321,16 @@ public SmileGenerator(IOContext ctxt, int jsonFeatures, int smileFeatures,
305
321
}
306
322
}
307
323
308
- public SmileGenerator (IOContext ctxt , int jsonFeatures , int smileFeatures ,
324
+ public SmileGenerator (IOContext ctxt , int stdFeatures , int smileFeatures ,
309
325
ObjectCodec codec , OutputStream out , byte [] outputBuffer , int offset ,
310
326
boolean bufferRecyclable )
311
327
{
312
- super (jsonFeatures , codec );
328
+ super (stdFeatures , codec , null );
329
+ DupDetector dups = JsonGenerator .Feature .STRICT_DUPLICATE_DETECTION .enabledIn (stdFeatures )
330
+ ? DupDetector .rootDetector (this )
331
+ : null ;
332
+ // NOTE: we passed `null` for default write context
333
+ _smileContext = SmileWriteContext .createRootContext (dups );
313
334
_formatFeatures = smileFeatures ;
314
335
_ioContext = ctxt ;
315
336
_smileBufferRecycler = _smileBufferRecycler ();
@@ -451,6 +472,27 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
451
472
return this ;
452
473
}
453
474
475
+ /*
476
+ /**********************************************************
477
+ /* Overridden methods, output context (and related)
478
+ /**********************************************************
479
+ */
480
+
481
+ @ Override
482
+ public Object getCurrentValue () {
483
+ return _smileContext .getCurrentValue ();
484
+ }
485
+
486
+ @ Override
487
+ public void setCurrentValue (Object v ) {
488
+ _smileContext .setCurrentValue (v );
489
+ }
490
+
491
+ @ Override
492
+ public JsonStreamContext getOutputContext () {
493
+ return _smileContext ;
494
+ }
495
+
454
496
/*
455
497
/**********************************************************
456
498
/* Overridden methods, write methods
@@ -464,7 +506,7 @@ public JsonGenerator overrideFormatFeatures(int values, int mask) {
464
506
@ Override
465
507
public final void writeFieldName (String name ) throws IOException
466
508
{
467
- if (_writeContext .writeFieldName (name ) == JsonWriteContext . STATUS_EXPECT_VALUE ) {
509
+ if (! _smileContext .writeFieldName (name )) {
468
510
_reportError ("Can not write a field name, expecting a value" );
469
511
}
470
512
_writeFieldName (name );
@@ -475,7 +517,7 @@ public final void writeFieldName(SerializableString name)
475
517
throws IOException
476
518
{
477
519
// Object is a value, need to verify it's allowed
478
- if (_writeContext .writeFieldName (name .getValue ()) == JsonWriteContext . STATUS_EXPECT_VALUE ) {
520
+ if (! _smileContext .writeFieldName (name .getValue ())) {
479
521
_reportError ("Can not write a field name, expecting a value" );
480
522
}
481
523
_writeFieldName (name );
@@ -485,7 +527,7 @@ public final void writeFieldName(SerializableString name)
485
527
public final void writeStringField (String fieldName , String value )
486
528
throws IOException
487
529
{
488
- if (_writeContext .writeFieldName (fieldName ) == JsonWriteContext . STATUS_EXPECT_VALUE ) {
530
+ if (! _smileContext .writeFieldName (fieldName )) {
489
531
_reportError ("Can not write a field name, expecting a value" );
490
532
}
491
533
_writeFieldName (fieldName );
@@ -562,55 +604,52 @@ public void writeBytes(byte[] data, int offset, int len) throws IOException
562
604
public final void writeStartArray () throws IOException
563
605
{
564
606
_verifyValueWrite ("start an array" );
565
- _writeContext = _writeContext .createChildArrayContext ();
607
+ _smileContext = _smileContext .createChildArrayContext ();
566
608
_writeByte (TOKEN_LITERAL_START_ARRAY );
567
609
}
568
610
569
611
@ Override // defined since 2.6.3
570
612
public final void writeStartArray (int size ) throws IOException
571
613
{
572
614
_verifyValueWrite ("start an array" );
573
- _writeContext = _writeContext .createChildArrayContext ();
615
+ _smileContext = _smileContext .createChildArrayContext ();
574
616
_writeByte (TOKEN_LITERAL_START_ARRAY );
575
617
}
576
618
577
619
@ Override
578
620
public final void writeEndArray () throws IOException
579
621
{
580
- if (!_writeContext .inArray ()) {
581
- _reportError ("Current context not Array but " +_writeContext .typeDesc ());
622
+ if (!_smileContext .inArray ()) {
623
+ _reportError ("Current context not Array but " +_smileContext .typeDesc ());
582
624
}
583
625
_writeByte (TOKEN_LITERAL_END_ARRAY );
584
- _writeContext = _writeContext .getParent ();
626
+ _smileContext = _smileContext .getParent ();
585
627
}
586
628
587
629
@ Override
588
630
public final void writeStartObject () throws IOException
589
631
{
590
632
_verifyValueWrite ("start an object" );
591
- _writeContext = _writeContext .createChildObjectContext ();
633
+ _smileContext = _smileContext .createChildObjectContext (null );
592
634
_writeByte (TOKEN_LITERAL_START_OBJECT );
593
635
}
594
636
595
637
@ Override // since 2.8
596
638
public final void writeStartObject (Object forValue ) throws IOException
597
639
{
598
640
_verifyValueWrite ("start an object" );
599
- JsonWriteContext ctxt = _writeContext .createChildObjectContext ();
600
- _writeContext = ctxt ;
601
- if (forValue != null ) {
602
- ctxt .setCurrentValue (forValue );
603
- }
641
+ SmileWriteContext ctxt = _smileContext .createChildObjectContext (forValue );
642
+ _smileContext = ctxt ;
604
643
_writeByte (TOKEN_LITERAL_START_OBJECT );
605
644
}
606
645
607
646
@ Override
608
647
public final void writeEndObject () throws IOException
609
648
{
610
- if (!_writeContext .inObject ()) {
611
- _reportError ("Current context not Object but " +_writeContext .typeDesc ());
649
+ if (!_smileContext .inObject ()) {
650
+ _reportError ("Current context not Object but " +_smileContext .typeDesc ());
612
651
}
613
- _writeContext = _writeContext .getParent ();
652
+ _smileContext = _smileContext .getParent ();
614
653
_writeByte (TOKEN_LITERAL_END_OBJECT );
615
654
}
616
655
@@ -1758,8 +1797,7 @@ protected void _writeDecimalNumber(String enc) throws IOException
1758
1797
protected final void _verifyValueWrite (String typeMsg )
1759
1798
throws IOException
1760
1799
{
1761
- int status = _writeContext .writeValue ();
1762
- if (status == JsonWriteContext .STATUS_EXPECT_NAME ) {
1800
+ if (!_smileContext .writeValue ()) {
1763
1801
_reportError ("Can not " +typeMsg +", expecting field name" );
1764
1802
}
1765
1803
}
0 commit comments