Java Code Examples for com.fasterxml.jackson.jr.ob.JSONObjectException#from()

The following examples show how to use com.fasterxml.jackson.jr.ob.JSONObjectException#from() . 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: ArrayReader.java    From jackson-jr with Apache License 2.0 6 votes vote down vote up
@Override
public Object readNext(JSONReader r, JsonParser p) throws IOException {
    if (p.nextToken() != JsonToken.START_ARRAY) {
        if (p.hasToken(JsonToken.VALUE_NULL)) {
            return null;
        }
        throw JSONObjectException.from(p, "Unexpected token %s; should get START_ARRAY",
                p.currentToken());
    }
    CollectionBuilder b = r._collectionBuilder(null);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        return b.emptyArray(_elementType);
    }
    Object value = _valueReader.read(r, p);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        return b.singletonArray(_elementType, value);
    }
    b = b.start().add(value);
    do {
        b = b.add(_valueReader.read(r, p));
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return b.buildArray(_elementType);
}
 
Example 2
Source File: BeanReader.java    From jackson-jr with Apache License 2.0 6 votes vote down vote up
protected void handleUnknown(JSONReader reader, JsonParser parser, String fieldName) throws IOException {
        if (JSON.Feature.FAIL_ON_UNKNOWN_BEAN_PROPERTY.isEnabled(reader._features)) {
            // 20-Jan-2020, tatu: With optional annotation support, may have "known ignorable"
            //    that usually should behave as if safely ignorable
            if (!_ignorableNames.contains(fieldName)) {
                final StringBuilder sb = new StringBuilder(60);
                Iterator<String> it = new TreeSet<String>(_propsByName.keySet()).iterator();
                if (it.hasNext()) {
                    sb.append('"').append(it.next()).append('"');
                    while (it.hasNext()) {
                        sb.append(", \"").append(it.next()).append('"');
                    }
                }
                throw JSONObjectException.from(parser,
"Unrecognized JSON property \"%s\" for Bean type `%s` (known properties: [%s])",
                        fieldName, _valueType.getName(), sb.toString());
            }
        }
        parser.nextToken();
        parser.skipChildren();
    }
 
Example 3
Source File: CollectionReader.java    From jackson-jr with Apache License 2.0 6 votes vote down vote up
@Override
public Object readNext(JSONReader r, JsonParser p) throws IOException {
    if (p.nextToken() != JsonToken.START_ARRAY) {
        if (p.hasToken(JsonToken.VALUE_NULL)) {
            return null;
        }
        throw JSONObjectException.from(p, "Unexpected token %s; should get START_ARRAY",
                p.currentToken());
    }
    CollectionBuilder b = r._collectionBuilder(_collectionType);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        return b.emptyCollection();
    }
    Object value = _valueReader.read(r, p);
    if (p.nextToken() == JsonToken.END_ARRAY) {
        return b.singletonCollection(value);
    }
    b = b.start().add(value);
    do {
        b = b.add(_valueReader.read(r, p));
    } while (p.nextToken() != JsonToken.END_ARRAY);
    return b.buildCollection();
}
 
Example 4
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
/**
 * Method for reading a JSON Object from input and building a {@link java.util.Map}
 * out of it. Note that if input does NOT contain a
 * JSON Object, {@link JSONObjectException} will be thrown.
 */
public Map<String,Object> readMap() throws IOException {
    if (_parser.isExpectedStartObjectToken()) {
        return (Map<String,Object>) AnyReader.std.readFromObject(this, _parser, _mapBuilder);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read a Map: expect to see START_OBJECT ('{'), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 5
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
/**
 * Method for reading a JSON Array from input and building a {@link java.util.List}
 * out of it. Note that if input does NOT contain a
 * JSON Array, {@link JSONObjectException} will be thrown.
 */
public List<Object> readList() throws IOException {
    if (_parser.isExpectedStartArrayToken()) {
        return (List<Object>) AnyReader.std.readCollectionFromArray(this, _parser, _collectionBuilder);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read a List: expect to see START_ARRAY ('['), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 6
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
/**
 * Method for reading a JSON Array from input and building a <code>Object[]</code>
 * out of it. Note that if input does NOT contain a
 * JSON Array, {@link JSONObjectException} will be thrown.
 */
public Object[] readArray() throws IOException
{
    if (_parser.isExpectedStartArrayToken()) {
        return AnyReader.std.readArrayFromArray(this, _parser, _collectionBuilder);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read an array: expect to see START_ARRAY ('['), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 7
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T[] readArrayOf(Class<T> type) throws IOException {
    if (_parser.isExpectedStartArrayToken()) {
        // NOTE: "array type" we give is incorrect, but should usually not matter
        // -- to fix would need to instantiate 0-element array, get that type
        return (T[]) new ArrayReader(type, type,
            _readerLocator.findReader(type))
                .read(this, _parser);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read an array: expect to see START_ARRAY ('['), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 8
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
/**
 * Method for reading a JSON Array from input and building a {@link java.util.List}
 * out of it, binding values into specified {@code type}.
 * Note that if input does NOT contain a JSON Array, {@link JSONObjectException} will be thrown.
 */
@SuppressWarnings("unchecked")
public <T> List<T> readListOf(Class<T> type) throws IOException
{
    if (_parser.isExpectedStartArrayToken()) {
        return (List<T>) new CollectionReader(List.class, _readerLocator.findReader(type))
                .read(this, _parser);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read a List: expect to see START_ARRAY ('['), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 9
Source File: JSONReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
/**
 * Method for reading a JSON Object from input and building a {@link java.util.Map}
 * out of it, binding values into specified {@code type}.
 * Note that if input does NOT contain a JSON Object, {@link JSONObjectException} will be thrown.
 */
@SuppressWarnings("unchecked")
public <T> Map<String, T> readMapOf(Class<T> type) throws IOException
{
    if (_parser.isExpectedStartObjectToken()) {
        return (Map<String, T>) new MapReader(Map.class, _readerLocator.findReader(type))
                .read(this, _parser);
    }
    if (_parser.hasToken(JsonToken.VALUE_NULL)) {
        return null;
    }
    throw JSONObjectException.from(_parser,
            "Can not read a Map: expect to see START_OBJECT ('{'), instead got: "+ValueReader._tokenDesc(_parser));
}
 
Example 10
Source File: MapReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
@Override
public Object read(JSONReader r, JsonParser p) throws IOException {
    MapBuilder b = r._mapBuilder(_mapType);
    String propName0 = p.nextFieldName();
    if (propName0 == null) {
        if (p.hasToken(JsonToken.END_OBJECT)) {
            return b.emptyMap();
        }
        throw _reportWrongToken(p);
    }
    Object value = _valueReader.readNext(r, p);
    String propName = p.nextFieldName();
    if (propName == null) {
        if (p.hasToken(JsonToken.END_OBJECT)) {
            return b.singletonMap(propName0, value);
        }
        throw _reportWrongToken(p);
    }
    try {
        b = b.start().put(propName0, value);
        while (true) {
            b = b.put(propName, _valueReader.readNext(r, p));
            propName = p.nextFieldName();
            if (propName == null) {
                if (p.hasToken(JsonToken.END_OBJECT)) {
                    return b.build();
                }
                throw _reportWrongToken(p);
            }
        }
    } catch (IllegalArgumentException e) {
        throw JSONObjectException.from(p, e.getMessage());
    }
}
 
Example 11
Source File: BeanReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
protected Object _reportFailureToCreate(JsonParser p, Exception e) throws IOException
{
    if (e instanceof IOException) {
        throw (IOException) e;
    }
    throw JSONObjectException.from(p, e,
            "Failed to create an instance of `%s` due to (%s): %s",
            _valueType.getName(), e.getClass().getName(), e.getMessage());
}
 
Example 12
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
protected long _fetchLong(JsonParser p) throws IOException
{
    JsonToken t = p.currentToken();
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return p.getLongValue();
    }
    throw JSONObjectException.from(p, "Can not get long numeric value from JSON (to construct "
            +_valueType.getName()+") from "+_tokenDesc(p, t));
}
 
Example 13
Source File: MapReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
@Override
public Object readNext(JSONReader r, JsonParser p) throws IOException {
    if (p.nextToken() != JsonToken.START_OBJECT) {
        if (p.hasToken(JsonToken.VALUE_NULL)) {
            return null;
        }
        throw JSONObjectException.from(p, "Unexpected token %s; should get START_OBJECT",
                p.currentToken());
    }
    
    MapBuilder b = r._mapBuilder(_mapType);
    String propName0 = p.nextFieldName();
    if (propName0 == null) {
        if (p.hasToken(JsonToken.END_OBJECT)) {
            return b.emptyMap();
        }
        throw _reportWrongToken(p);
    }
    Object value = _valueReader.readNext(r, p);
    String propName = p.nextFieldName();
    if (propName == null) {
        if (p.hasToken(JsonToken.END_OBJECT)) {
            return b.singletonMap(propName0, value);
        }
        throw _reportWrongToken(p);
    }
    try {
        b = b.start().put(propName0, value);
        while (true) {
            b = b.put(propName, _valueReader.readNext(r, p));
            propName = p.nextFieldName();
            if (propName == null) {
                if (p.hasToken(JsonToken.END_OBJECT)) {
                    return b.build();
                }
                throw _reportWrongToken(p);
            }
        }
    } catch (IllegalArgumentException e) {
        throw JSONObjectException.from(p, e.getMessage());
    }
}
 
Example 14
Source File: MapReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
protected JSONObjectException _reportWrongToken(JsonParser p) {
    return JSONObjectException.from(p, "Unexpected token %s; should get FIELD_NAME or END_OBJECT",
            p.currentToken());
}
 
Example 15
Source File: BeanReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
protected IOException _reportProblem(JsonParser p) {
    return JSONObjectException.from(p, "Unexpected token %s; should get FIELD_NAME or END_OBJECT",
            p.currentToken());
}
 
Example 16
Source File: SimpleValueReader.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
@Override
    public Object read(JSONReader reader, JsonParser p) throws IOException
    {
        switch (_typeId) {

        case SER_INT_ARRAY:
            return _readIntArray(p);

        case SER_TREE_NODE:
            return reader.readTree();

        // Textual types, related:
        case SER_STRING:
        case SER_CHARACTER_SEQUENCE:
            return p.getValueAsString();
        case SER_CHAR_ARRAY:
            return p.getValueAsString().toCharArray();
        case SER_BYTE_ARRAY:
            return _readBinary(p);

        // Number types:
            
        case SER_NUMBER_FLOAT: // fall through
            return Float.valueOf((float) p.getValueAsDouble());
        case SER_NUMBER_DOUBLE:
            return p.getValueAsDouble();

        case SER_NUMBER_BYTE: // fall through
            return (byte) p.getValueAsInt();
            
        case SER_NUMBER_SHORT: // fall through
            return (short) p.getValueAsInt();
        case SER_NUMBER_INTEGER:
            return p.getValueAsInt();
        case SER_NUMBER_LONG:
            return p.getValueAsLong();

        case SER_NUMBER_BIG_DECIMAL:
            return p.getDecimalValue();

        case SER_NUMBER_BIG_INTEGER:
            return p.getBigIntegerValue();

        // Other scalar types:

        case SER_BOOLEAN:
            return p.getValueAsBoolean();
            
        case SER_CHAR:
            {
                String str = p.getValueAsString();
                return (str == null || str.isEmpty()) ? ' ' : str.charAt(0);
            }
            
        case SER_CALENDAR:
            {
                long l = _fetchLong(p);
                Calendar cal = Calendar.getInstance();
                cal.setTimeInMillis(l);
                return cal;
            }

        case SER_DATE:
            return new Date(_fetchLong(p));

        case SER_CLASS:
        {
            String v = p.getValueAsString();
            try {
                return Class.forName(v);
            } catch (Exception e) {
                throw new JSONObjectException("Failed to bind java.lang.Class from value '"+v+"'");
            }
        }
        case SER_FILE:
            return new File(p.getValueAsString());
        case SER_UUID:
            return UUID.fromString(p.getValueAsString());
        case SER_URL:
            return new URL(p.getValueAsString());
        case SER_URI:
        
            return URI.create(p.getValueAsString());

//        case SER_MAP:
//        case SER_LIST:
//        case SER_COLLECTION:
//        case SER_OBJECT_ARRAY:
            // should never get here: we have dedicated readers
        default: // types that shouldn't get here
        //case SER_ENUM:
        }
        
        throw JSONObjectException.from(p,
                "Can not create a "+_valueType.getName()+" instance out of "+_tokenDesc(p));
    }