void writeTo ( JsonGenerator generator ) throws IOException { DeserializationState deserState = _deserState . get ( ) ; if ( deserState . isDeserialized ( ) ) { // Object has already been deserialized, use standard writer generator . writeObject ( deserState . deserialized ) ; return ; } if ( deserState . overrides . isEmpty ( ) ) { // With no overrides the most efficient action is to copy the original JSON verbatim. try { generator . writeRaw ( deserState . json ) ; return ; } catch ( UnsupportedOperationException e ) { // Not all parsers are guaranteed to support this. If this is one then use the default // generator implementation which follows. } } ObjectCodec codec = generator . getCodec ( ) ; if ( codec == null ) { // No codec, defer to generator generator . writeObject ( deserialized ( ) ) ; return ; } JsonParser parser = codec . getFactory ( ) . createParser ( deserState . json ) ; checkState ( parser . nextToken ( ) == JsonToken . START_OBJECT , "JSON did not contain an object" ) ; generator . writeStartObject ( ) ; // Typically the JSON string has been pre-sorted. Insert the overrides in order. If it turns out the // JSON wasn't sorted then we'll just dump the remaining overrides at the end; it's valid JSON // one way or the other. //noinspection unchecked Iterator < Map . Entry < String , Object > > sortedOverrides = ( ( Map < String , Object > ) OrderedJson . ordered ( deserState . overrides ) ) . entrySet ( ) . iterator ( ) ; Map . Entry < String , Object > nextOverride = sortedOverrides . hasNext ( ) ? sortedOverrides . next ( ) : null ; JsonToken token ; while ( ( token = parser . nextToken ( ) ) != JsonToken . END_OBJECT ) { assert token == JsonToken . FIELD_NAME ; String field = parser . getText ( ) ; if ( deserState . overrides . containsKey ( field ) ) { // There's an override for this entry; skip it token = parser . nextToken ( ) ; if ( token . isStructStart ( ) ) { parser . skipChildren ( ) ; } } else { // Write all overrides which sort prior to this field while ( nextOverride != null && OrderedJson . KEY_COMPARATOR . compare ( nextOverride . getKey ( ) , field ) < 0 ) { generator . writeFieldName ( nextOverride . getKey ( ) ) ; generator . writeObject ( nextOverride . getValue ( ) ) ; nextOverride = sortedOverrides . hasNext ( ) ? sortedOverrides . next ( ) : null ; } // Copy this field name and value to the generator generator . copyCurrentStructure ( parser ) ; } // Both of the above operations leave the current token immediately prior to the next // field or the end object token. } // Write any remaining overrides while ( nextOverride != null ) { generator . writeFieldName ( nextOverride . getKey ( ) ) ; generator . writeObject ( nextOverride . getValue ( ) ) ; nextOverride = sortedOverrides . hasNext ( ) ? sortedOverrides . next ( ) : null ; } generator . writeEndObject ( ) ; }
Writes this record to the provided generator in the most efficient manner possible in the current state .