Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.jackson.core.base;
  
  import java.io.*;
  
This base class implements part of API that a JSON generator exposes to applications, adds shared internal methods that sub-classes can use and adds some abstract methods sub-classes must implement.
 
 public abstract class GeneratorBase
     extends JsonGenerator
 {
     /*
     /**********************************************************
     /* Configuration
     /**********************************************************
      */
 
     protected ObjectCodec _objectCodec;

    
Bit flag composed of bits that indicate which com.fasterxml.jackson.core.JsonGenerator.Features are enabled.
 
     protected int _features;

    
Flag set to indicate that implicit conversion from number to JSON String is needed (as per com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS).
 
     protected boolean _cfgNumbersAsStrings;
     
     /*
     /**********************************************************
     /* State
     /**********************************************************
      */

    
Object that keeps track of the current contextual state of the generator.
 
     protected JsonWriteContext _writeContext;

    
Flag that indicates whether generator is closed or not. Gets set when it is closed by an explicit call (close()).
 
     protected boolean _closed;
 
     /*
     /**********************************************************
     /* Life-cycle
     /**********************************************************
      */
 
     protected GeneratorBase(int featuresObjectCodec codec)
     {
         super();
          = features;
          = JsonWriteContext.createRootContext();
          = codec;
     }
    
Implemented with detection that tries to find "VERSION.txt" in same package as the implementation class.
 
     @Override
     public Version version() {
         return VersionUtil.versionFor(getClass());
     }
     
     /*
     /**********************************************************
     /* Configuration
     /**********************************************************
      */
 
     @Override
     public JsonGenerator enable(Feature f) {
          |= f.getMask();
         if (f == .) {
              = true;
         } else if (f == .) {
             setHighestNonEscapedChar(127);
        }
        return this;
    }
    @Override
    public JsonGenerator disable(Feature f) {
         &= ~f.getMask();
        if (f == .) {
             = false;
        } else if (f == .) {
            setHighestNonEscapedChar(0);
        }
        return this;
    }
    //public JsonGenerator configure(Feature f, boolean state) { }
    @Override
    public final boolean isEnabled(Feature f) {
        return ( & f.getMask()) != 0;
    }
    
    @Override
        /* 28-Sep-2012, tatu: As per [Issue#84], should not override a
         *  pretty printer if one already assigned.
         */
        if (getPrettyPrinter() != null) {
            return this;
        }
        return setPrettyPrinter(new DefaultPrettyPrinter());
    }
    
    @Override
    public JsonGenerator setCodec(ObjectCodec oc) {
         = oc;
        return this;
    }
    @Override
    public final ObjectCodec getCodec() { return ; }
    /*
    /**********************************************************
    /* Public API, accessors
    /**********************************************************
     */

    
Note: co-variant return type.
    @Override
    public final JsonWriteContext getOutputContext() { return ; }
    /*
    /**********************************************************
    /* Public API, write methods, structural
    /**********************************************************
     */
    //public void writeStartArray() throws IOException, JsonGenerationException
    //public void writeEndArray() throws IOException, JsonGenerationException
    //public void writeStartObject() throws IOException, JsonGenerationException
    //public void writeEndObject() throws IOException, JsonGenerationException
    /*
    /**********************************************************
    /* Public API, write methods, textual
    /**********************************************************
     */
    @Override
    public void writeFieldName(SerializableString namethrows IOExceptionJsonGenerationException {
        writeFieldName(name.getValue());
    }
    
    //public abstract void writeString(String text) throws IOException, JsonGenerationException;
    //public abstract void writeString(char[] text, int offset, int len) throws IOException, JsonGenerationException;
    //public abstract void writeRaw(String text) throws IOException, JsonGenerationException;
    //public abstract void writeRaw(char[] text, int offset, int len) throws IOException, JsonGenerationException;
    @Override
    public void writeString(SerializableString textthrows IOExceptionJsonGenerationException {
        writeString(text.getValue());
    }
    
    @Override
    public void writeRawValue(String textthrows IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write raw value");
        writeRaw(text);
    }
    @Override
    public void writeRawValue(String textint offsetint len)
        throws IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write raw value");
        writeRaw(textoffsetlen);
    }
    @Override
    public void writeRawValue(char[] textint offsetint len)
        throws IOExceptionJsonGenerationException
    {
        _verifyValueWrite("write raw value");
        writeRaw(textoffsetlen);
    }
    @Override
    public int writeBinary(Base64Variant b64variantInputStream dataint dataLength)
        throws IOExceptionJsonGenerationException {
        // Let's implement this as "unsupported" to make it easier to add new parser impls
        return 0;
    }
    /*
   /**********************************************************
   /* Public API, write methods, primitive
   /**********************************************************
    */
    // Not implemented at this level, added as placeholders
     /*
    public abstract void writeNumber(int i)
    public abstract void writeNumber(long l)
    public abstract void writeNumber(double d)
    public abstract void writeNumber(float f)
    public abstract void writeNumber(BigDecimal dec)
    public abstract void writeBoolean(boolean state)
    public abstract void writeNull()
    */
    /*
    /**********************************************************
    /* Public API, write methods, POJOs, trees
    /**********************************************************
     */
    @Override
    public void writeObject(Object value)
        throws IOExceptionJsonProcessingException
    {
        if (value == null) {
            // important: call method that does check value write:
            writeNull();
        } else {
            /* 02-Mar-2009, tatu: we are NOT to call _verifyValueWrite here,
             *   because that will be done when codec actually serializes
             *   contained POJO. If we did call it it would advance state
             *   causing exception later on
             */
            if ( != null) {
                .writeValue(thisvalue);
                return;
            }
            _writeSimpleObject(value);
        }
    }
    @Override
    public void writeTree(TreeNode rootNode)
        throws IOExceptionJsonProcessingException
    {
        // As with 'writeObject()', we are not check if write would work
        if (rootNode == null) {
            writeNull();
        } else {
            if ( == null) {
                throw new IllegalStateException("No ObjectCodec defined for the generator, can not serialize JsonNode-based trees");
            }
            .writeValue(thisrootNode);
        }
    }
    /*
    /**********************************************************
    /* Public API, low-level output handling
    /**********************************************************
     */
    @Override
    public abstract void flush() throws IOException;
    @Override
    public void close() throws IOException
    {
         = true;
    }
    @Override
    public boolean isClosed() { return ; }
    /*
    /**********************************************************
    /* Public API, copy-through methods
    /**********************************************************
     */
    @Override
    public final void copyCurrentEvent(JsonParser jp)
        throws IOExceptionJsonProcessingException
    {
        JsonToken t = jp.getCurrentToken();
        // sanity check; what to do?
        if (t == null) {
            _reportError("No current event to copy");
        }
        switch(t) {
        case :
            writeStartObject();
            break;
        case :
            writeEndObject();
            break;
        case :
            writeStartArray();
            break;
        case :
            writeEndArray();
            break;
        case :
            writeFieldName(jp.getCurrentName());
            break;
        case :
            if (jp.hasTextCharacters()) {
                writeString(jp.getTextCharacters(), jp.getTextOffset(), jp.getTextLength());
            } else {
                writeString(jp.getText());
            }
            break;
        case :
            switch (jp.getNumberType()) {
            case :
                writeNumber(jp.getIntValue());
                break;
            case :
                writeNumber(jp.getBigIntegerValue());
                break;
            default:
                writeNumber(jp.getLongValue());
            }
            break;
        case :
            switch (jp.getNumberType()) {
            case :
                writeNumber(jp.getDecimalValue());
                break;
            case :
                writeNumber(jp.getFloatValue());
                break;
            default:
                writeNumber(jp.getDoubleValue());
            }
            break;
        case :
            writeBoolean(true);
            break;
        case :
            writeBoolean(false);
            break;
        case :
            writeNull();
            break;
        case :
            writeObject(jp.getEmbeddedObject());
            break;
        default:
            _cantHappen();
        }
    }
    @Override
    public final void copyCurrentStructure(JsonParser jp)
        throws IOExceptionJsonProcessingException
    {
        JsonToken t = jp.getCurrentToken();
        // Let's handle field-name separately first
        if (t == .) {
            writeFieldName(jp.getCurrentName());
            t = jp.nextToken();
            // fall-through to copy the associated value
        }
        switch (t) {
        case :
            writeStartArray();
            while (jp.nextToken() != .) {
                copyCurrentStructure(jp);
            }
            writeEndArray();
            break;
        case :
            writeStartObject();
            while (jp.nextToken() != .) {
                copyCurrentStructure(jp);
            }
            writeEndObject();
            break;
        default// others are simple:
            copyCurrentEvent(jp);
        }
    }
    /*
    /**********************************************************
    /* Package methods for this, sub-classes
    /**********************************************************
     */
    protected abstract void _releaseBuffers();
    protected abstract void _verifyValueWrite(String typeMsg)
        throws IOExceptionJsonGenerationException;
    protected void _reportError(String msg)
        throws JsonGenerationException
    {
        throw new JsonGenerationException(msg);
    }
    protected void _cantHappen()
    {
        throw new RuntimeException("Internal error: should never end up through this code path");
    }

    
Helper method to try to call appropriate write method for given untyped Object. At this point, no structural conversions should be done, only simple basic types are to be coerced as necessary.

Parameters:
value Non-null value to write
    protected void _writeSimpleObject(Object value
        throws IOExceptionJsonGenerationException
    {
        /* 31-Dec-2009, tatu: Actually, we could just handle some basic
         *    types even without codec. This can improve interoperability,
         *    and specifically help with TokenBuffer.
         */
        if (value == null) {
            writeNull();
            return;
        }
        if (value instanceof String) {
            writeString((Stringvalue);
            return;
        }
        if (value instanceof Number) {
            Number n = (Numbervalue;
            if (n instanceof Integer) {
                writeNumber(n.intValue());
                return;
            } else if (n instanceof Long) {
                writeNumber(n.longValue());
                return;
            } else if (n instanceof Double) {
                writeNumber(n.doubleValue());
                return;
            } else if (n instanceof Float) {
                writeNumber(n.floatValue());
                return;
            } else if (n instanceof Short) {
                writeNumber(n.shortValue());
                return;
            } else if (n instanceof Byte) {
                writeNumber(n.byteValue());
                return;
            } else if (n instanceof BigInteger) {
                writeNumber((BigIntegern);
                return;
            } else if (n instanceof BigDecimal) {
                writeNumber((BigDecimaln);
                return;
                
            // then Atomic types
                
            } else if (n instanceof AtomicInteger) {
                writeNumber(((AtomicIntegern).get());
                return;
            } else if (n instanceof AtomicLong) {
                writeNumber(((AtomicLongn).get());
                return;
            }
        } else if (value instanceof byte[]) {
            writeBinary((byte[]) value);
            return;
        } else if (value instanceof Boolean) {
            writeBoolean((Booleanvalue);
            return;
        } else if (value instanceof AtomicBoolean) {
            writeBoolean(((AtomicBooleanvalue).get());
            return;
        }
        throw new IllegalStateException("No ObjectCodec defined for the generator, can only serialize simple wrapper types (type passed "
                +value.getClass().getName()+")");
    }    
    protected final void _throwInternal() {
        throw new RuntimeException("Internal error: this code path should never get executed");
    }
    protected void _reportUnsupportedOperation() {
        throw new UnsupportedOperationException("Operation not supported by generator of type "+getClass().getName());
    }
New to GrepCode? Check out our FAQ X