com.fasterxml.jackson.core.json.JsonWriteContext Java Examples

The following examples show how to use com.fasterxml.jackson.core.json.JsonWriteContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @since 2.7
 */
public TokenBuffer(JsonParser p, DeserializationContext ctxt)
{
    _objectCodec = p.getCodec();
    _parentContext = p.getParsingContext();
    _generatorFeatures = DEFAULT_GENERATOR_FEATURES;
    _writeContext = JsonWriteContext.createRootContext(null);
    // at first we have just one segment
    _first = _last = new Segment();
    _appendAt = 0;
    _hasNativeTypeIds = p.canReadTypeId();
    _hasNativeObjectIds = p.canReadObjectId();
    _mayHaveNativeIds = _hasNativeTypeIds | _hasNativeObjectIds;
    _forceBigDecimal = (ctxt == null) ? false
            : ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
}
 
Example #2
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param codec Object codec to use for stream-based object
 *   conversion through parser/generator interfaces. If null,
 *   such methods cannot be used.
 * @param hasNativeIds Whether resulting {@link JsonParser} (if created)
 *   is considered to support native type and object ids
 */
public TokenBuffer(ObjectCodec codec, boolean hasNativeIds)
{
    _objectCodec = codec;
    _generatorFeatures = DEFAULT_GENERATOR_FEATURES;
    _writeContext = JsonWriteContext.createRootContext(null);
    // at first we have just one segment
    _first = _last = new Segment();
    _appendAt = 0;
    _hasNativeTypeIds = hasNativeIds;
    _hasNativeObjectIds = hasNativeIds;

    _mayHaveNativeIds = _hasNativeTypeIds | _hasNativeObjectIds;
}
 
Example #3
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void writeEndArray() throws IOException
{
    _append(JsonToken.END_ARRAY);
    // Let's allow unbalanced tho... i.e. not run out of root level, ever
    JsonWriteContext c = _writeContext.getParent();
    if (c != null) {
        _writeContext = c;
    }
}
 
Example #4
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override // since 2.8
public void writeStartObject(Object forValue) throws IOException
{
    _writeContext.writeValue();
    _append(JsonToken.START_OBJECT);
    JsonWriteContext ctxt = _writeContext.createChildObjectContext();
    _writeContext = ctxt;
    if (forValue != null) {
        ctxt.setCurrentValue(forValue);
    }
}
 
Example #5
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void writeEndObject() throws IOException
{
    _append(JsonToken.END_OBJECT);
    // Let's allow unbalanced tho... i.e. not run out of root level, ever
    JsonWriteContext c = _writeContext.getParent();
    if (c != null) {
        _writeContext = c;
    }
}
 
Example #6
Source File: GeneratorBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected GeneratorBase(int features, ObjectCodec codec) {
    super();
    _features = features;
    _objectCodec = codec;
    DupDetector dups = Feature.STRICT_DUPLICATE_DETECTION.enabledIn(features)
            ? DupDetector.rootDetector(this) : null;
    _writeContext = JsonWriteContext.createRootContext(dups);
    _cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
}
 
Example #7
Source File: GeneratorBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
protected GeneratorBase(int features, ObjectCodec codec, JsonWriteContext ctxt) {
    super();
    _features = features;
    _objectCodec = codec;
    _writeContext = ctxt;
    _cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
}
 
Example #8
Source File: StringUnicodeSerializer.java    From springboot-seed with MIT License 5 votes vote down vote up
@Override
public void serialize(String str, JsonGenerator gen,
                      SerializerProvider provider) throws IOException {
    int status = ((JsonWriteContext) gen.getOutputContext()).writeValue();
    switch (status) {
        case JsonWriteContext.STATUS_OK_AFTER_COLON:
            gen.writeRaw(':');
            break;
        case JsonWriteContext.STATUS_OK_AFTER_COMMA:
            gen.writeRaw(',');
            break;
        case JsonWriteContext.STATUS_EXPECT_NAME:
            throw new IOException("Can not write string value here");
    }
    gen.writeRaw('"');//写入JSON中字符串的开头引号
    for (char c : str.toCharArray()) {
        if (c >= 0x80) {
            writeUnicodeEscape(gen, c); // 为所有非ASCII字符生成转义的unicode字符
        } else {
            // 为ASCII字符中前128个字符使用转义的unicode字符
            int code = (c < ESCAPE_CODES.length ? ESCAPE_CODES[c] : 0);
            if (code == 0) {
                gen.writeRaw(c); // 此处不用转义
            } else if (code < 0) {
                writeUnicodeEscape(gen, (char) (-code - 1)); // 通用转义字符
            } else {
                writeShortEscape(gen, (char) code); // 短转义字符 (\n \t ...)
            }
        }
    }
    gen.writeRaw('"');//写入JSON中字符串的结束引号
}
 
Example #9
Source File: GeneratorBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
protected GeneratorBase(int features, ObjectCodec codec) {
    super();
    _features = features;
    _objectCodec = codec;
    DupDetector dups = Feature.STRICT_DUPLICATE_DETECTION.enabledIn(features)
            ? DupDetector.rootDetector(this) : null;
    _writeContext = JsonWriteContext.createRootContext(dups);
    _cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
}
 
Example #10
Source File: GeneratorBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @since 2.5
 */
protected GeneratorBase(int features, ObjectCodec codec, JsonWriteContext ctxt) {
    super();
    _features = features;
    _objectCodec = codec;
    _writeContext = ctxt;
    _cfgNumbersAsStrings = Feature.WRITE_NUMBERS_AS_STRINGS.enabledIn(features);
}
 
Example #11
Source File: MessagePackGenerator.java    From jackson-dataformat-msgpack with Apache License 2.0 5 votes vote down vote up
@Override
protected void _verifyValueWrite(String typeMsg) throws IOException, JsonGenerationException {
    int status = _writeContext.writeValue();
    if (status == JsonWriteContext.STATUS_EXPECT_NAME) {
        _reportError("Can not "+typeMsg+", expecting field name");
    }
}
 
Example #12
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final JsonWriteContext getOutputContext() { return _writeContext; }
 
Example #13
Source File: GeneratorBase.java    From openbd-core with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Note: co-variant return type.
 */
@Override public final JsonWriteContext getOutputContext() { return _writeContext; }