com.fasterxml.jackson.jr.ob.impl.AnyReader Maven / Gradle / Ivy
package com.fasterxml.jackson.jr.ob.impl;
import static com.fasterxml.jackson.core.JsonTokenId.*;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonParser.NumberType;
import com.fasterxml.jackson.jr.ob.*;
/**
* {@link ValueReader} used for "untyped" values; ones that are bound
* to whatever {@link java.lang.Object} is the natural mapping to JSON
* value that parser currently points to
*/
public class AnyReader extends ValueReader
{
public final static AnyReader std = new AnyReader();
@Override
public Object readNext(JSONReader r, JsonParser p) throws IOException
{
p.nextToken();
return read(r, p);
}
@Override
public Object read(JSONReader r, JsonParser p) throws IOException
{
JsonToken t = p.getCurrentToken();
int id = (t == null) ? ID_NO_TOKEN : t.id();
switch (id) {
case ID_NULL:
return null;
case ID_START_OBJECT:
return readFromObject(r, p, r._mapBuilder);
case ID_START_ARRAY:
if (r.arraysAsLists()) {
return readCollectionFromArray(r, p, r._collectionBuilder);
}
return readArrayFromArray(r, p, r._collectionBuilder);
case ID_STRING:
return fromString(p.getText());
case ID_NUMBER_INT:
{
NumberType n = p.getNumberType();
if (n == NumberType.INT) {
return Integer.valueOf(p.getIntValue());
}
if (n == NumberType.LONG) {
return Long.valueOf(p.getLongValue());
}
return p.getBigIntegerValue();
}
case ID_NUMBER_FLOAT:
if (!JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS.isEnabled(r._features)) {
NumberType n = p.getNumberType();
if (n == NumberType.FLOAT) {
return Float.valueOf(p.getFloatValue());
}
if (n == NumberType.DOUBLE) {
return Double.valueOf(p.getDoubleValue());
}
}
return p.getDecimalValue();
case ID_TRUE:
return fromBoolean(true);
case ID_FALSE:
return fromBoolean(false);
case ID_EMBEDDED_OBJECT:
return fromEmbedded(p.getEmbeddedObject());
// Others are error cases...
/*
default:
case END_ARRAY:
case END_OBJECT:
case FIELD_NAME:
case NOT_AVAILABLE:
*/
}
throw JSONObjectException.from(p, "Unexpected value token: "+_tokenDesc(p));
}
public Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy