Java Code Examples for com.dslplatform.json.JsonReader#last()

The following examples show how to use com.dslplatform.json.JsonReader#last() . 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: BObjectJsonCodec.java    From gridgo with MIT License 6 votes vote down vote up
@Override
public BObject read(JsonReader reader) throws IOException {
    if (reader.last() != '{')
        throw reader.newParseError("Expecting '{' for map start");
    var res = BObject.ofEmpty();
    byte nextToken = reader.getNextToken();
    if (nextToken == '}')
        return res;

    String key = reader.readKey();
    res.put(key, compositeCodec.read(reader, false));
    while ((nextToken = reader.getNextToken()) == ',') {
        reader.getNextToken();
        key = reader.readKey();
        res.put(key, compositeCodec.read(reader, false));
    }
    if (nextToken != '}')
        throw reader.newParseError("Expecting '}' for map end");
    return res;
}
 
Example 2
Source File: BValueJsonCodec.java    From gridgo with MIT License 6 votes vote down vote up
BValue read(JsonReader reader, boolean isBeginning) throws ParsingException, IOException {
    try {
        switch (reader.last()) {
        case 'n':
            if (!reader.wasNull())
                throw reader.newParseErrorAt("Expecting 'null' for null constant", 0);
            return BValue.of(null);
        case 't':
            if (!reader.wasTrue())
                throw reader.newParseErrorAt("Expecting 'true' for true constant", 0);
            return BValue.of(true);
        case 'f':
            if (!reader.wasFalse())
                throw reader.newParseErrorAt("Expecting 'false' for false constant", 0);
            return BValue.of(false);
        case '"':
            return BValue.of(reader.readString());
        default:
            return BValue.of(NumberConverter.deserializeNumber(reader));
        }
    } catch (ParsingException e) {
        if (isBeginning && !USE_STRICT)
            return BValue.of(reader.toString());
        throw e;
    }
}
 
Example 3
Source File: BArrayJsonCodec.java    From gridgo with MIT License 6 votes vote down vote up
@Override
public BArray read(JsonReader reader) throws IOException {
    if (reader.last() != '[')
        throw reader.newParseError("Expecting '[' for list start");

    var res = BArray.ofEmpty();

    byte nextToken = reader.getNextToken();
    if (nextToken == ']')
        return res;

    res.add(compositeCodec.read(reader, false));
    while ((nextToken = reader.getNextToken()) == ',') {
        reader.getNextToken();
        res.add(compositeCodec.read(reader, false));
    }

    if (nextToken != ']')
        throw reader.newParseError("Expecting ']' for list end");

    return res;
}
 
Example 4
Source File: CollectionDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
@Override
public T read(final JsonReader reader) throws IOException {
	if (reader.wasNull()) return null;
	if (reader.last() != '[') {
		throw reader.newParseError("Expecting '[' for collection start");
	}
	final T instance;
	try {
		instance = newInstance.call();
	} catch (Exception e) {
		throw new ConfigurationException("Unable to create a new instance of " + manifest, e);
	}
	if (reader.getNextToken() == ']') return instance;
	instance.add(decoder.read(reader));
	while (reader.getNextToken() == ','){
		reader.getNextToken();
		instance.add(decoder.read(reader));
	}
	if (reader.last() != ']') {
		throw reader.newParseError("Expecting ']' for collection end");
	}
	return instance;
}
 
Example 5
Source File: BElementJsonCodec.java    From gridgo with MIT License 5 votes vote down vote up
BElement read(JsonReader reader, boolean isBeginning) throws IOException {
    switch (reader.last()) {
    case '{':
        return objectCodec.read(reader);
    case '[':
        return arrayCodec.read(reader);
    default:
        return valueCodec.read(reader, isBeginning);
    }
}
 
Example 6
Source File: ImmutableDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
public T read(final JsonReader reader) throws IOException {
	if (reader.wasNull()) return null;
	else if (reader.last() != '{') {
		throw reader.newParseError(startError);
	}
	if (reader.getNextToken() == '}') {
		if (hasMandatory) {
			DecodePropertyInfo.showMandatoryError(reader, mandatoryFlag, decoders);
		}
		return newInstance.apply(defArgs);
	}
	final Object[] args = defArgs.clone();
	long currentMandatory = mandatoryFlag;
	int i = 0;
	while(i < decoders.length) {
		final DecodePropertyInfo<JsonReader.ReadObject> ri = decoders[i++];
		final int weakHash = reader.fillNameWeakHash();
		if (weakHash != ri.weakHash || !reader.wasLastName(ri.nameBytes)) {
			return readObjectSlow(args, reader, currentMandatory);
		}
		reader.getNextToken();
		if (ri.nonNull && reader.wasNull()) {
			throw reader.newParseErrorWith("Null value found for non-null attribute", ri.name);
		}
		args[ri.index] = ri.value.read(reader);
		currentMandatory = currentMandatory & ri.mandatoryValue;
		if (reader.getNextToken() == ',' && i != decoders.length) reader.getNextToken();
		else break;
	}
	return finalChecks(args, reader, currentMandatory);
}
 
Example 7
Source File: ImmutableDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
private T finalChecks(Object[] args, JsonReader reader, long currentMandatory) throws IOException {
	if (reader.last() != '}') {
		if (reader.last() != ',') {
			throw reader.newParseError(endError);
		}
		reader.getNextToken();
		reader.fillNameWeakHash();
		return readObjectSlow(args, reader, currentMandatory);
	}
	if (hasMandatory && currentMandatory != 0) {
		DecodePropertyInfo.showMandatoryError(reader, currentMandatory, decoders);
	}
	return newInstance.apply(args);
}
 
Example 8
Source File: ArrayFormatDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public B bind(final JsonReader reader, final B instance) throws IOException {
	if (reader.last() != '[') throw reader.newParseError(startError);
	reader.getNextToken();
	bindContent(reader, instance);
	return instance;
}
 
Example 9
Source File: ArrayFormatDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void bindContent(final JsonReader reader, final B instance) throws IOException {
	int i = 0;
	while (i < decoders.length) {
		decoders[i].bind(reader, instance);
		i++;
		if (reader.getNextToken() == ',') reader.getNextToken();
		else break;
	}
	if (i != decoders.length) {
		throw reader.newParseErrorWith(countError, 0, countError, ". Read only: ", i, "");
	}
	if (reader.last() != ']') throw reader.newParseError(endError, 1);
}
 
Example 10
Source File: ArrayDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
@Override
public T[] read(final JsonReader reader) throws IOException {
	if (reader.wasNull()) return null;
	if (reader.last() != '[') throw reader.newParseError("Expecting '[' for array start");
	if (reader.getNextToken() == ']') return emptyInstance;
	final ArrayList<T> list = new ArrayList<>(4);
	list.add(decoder.read(reader));
	while (reader.getNextToken() == ','){
		reader.getNextToken();
		list.add(decoder.read(reader));
	}
	if (reader.last() != ']') throw reader.newParseError("Expecting ']' for array end");
	return list.toArray(emptyInstance);
}
 
Example 11
Source File: MapDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Nullable
@Override
public T read(JsonReader reader) throws IOException {
	if (reader.wasNull()) return null;
	if (reader.last() != '{') throw reader.newParseError("Expecting '{' for map start");
	final T instance;
	try {
		instance = newInstance.call();
	} catch (Exception e) {
		throw new ConfigurationException("Unable to create a new instance of " + Reflection.typeDescription(manifest), e);
	}
	if (reader.getNextToken() == '}') return instance;
	K key = keyDecoder.read(reader);
	if (key == null) {
		throw reader.newParseErrorFormat("Null value detected for key element of map", 0, "Null value detected for key element of %s", Reflection.typeDescription(manifest));
	}
	if (reader.getNextToken() != ':') throw reader.newParseError("Expecting ':' after key attribute");
	reader.getNextToken();
	V value = valueDecoder.read(reader);
	instance.put(key, value);
	while (reader.getNextToken() == ','){
		reader.getNextToken();
		key = keyDecoder.read(reader);
		if (key == null) throw reader.newParseErrorFormat("Null value detected for key element of map", 0, "Null value detected for key element of %s", Reflection.typeDescription(manifest));
		if (reader.getNextToken() != ':') throw reader.newParseError("Expecting ':' after key attribute");
		reader.getNextToken();
		value = valueDecoder.read(reader);
		instance.put(key, value);
	}
	if (reader.last() != '}') throw reader.newParseError("Expecting '}' as map ending");
	return instance;
}