com.dslplatform.json.JsonReader Java Examples

The following examples show how to use com.dslplatform.json.JsonReader. 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: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static OptionalDecoder analyzeDecoding(final Type manifest, final Type content, final Class<?> raw, final DslJson json) {
	if (raw != Optional.class) {
		return null;
	} else if (content == Optional.class) {
		final OptionalDecoder nested = analyzeDecoding(content, Object.class, Optional.class, json);
		final OptionalDecoder outer = new OptionalDecoder<>(nested);
		json.registerReader(manifest, outer);
		return outer;
	}
	final JsonReader.ReadObject<?> reader = json.tryFindReader(content);
	if (reader == null) {
		return null;
	}
	final OptionalDecoder decoder = new OptionalDecoder<>(reader);
	json.registerReader(manifest, decoder);
	return decoder;
}
 
Example #2
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 #3
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 #4
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 #5
Source File: ImmutableDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
ImmutableDescription(
		final Type manifest,
		final Object[] defArgs,
		final Settings.Function<Object[], T> newInstance,
		final JsonWriter.WriteObject[] encoders,
		final DecodePropertyInfo<JsonReader.ReadObject>[] decoders,
		final boolean alwaysSerialize,
		final boolean skipOnUnknown) {
	super(encoders, alwaysSerialize);
	if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
	if (defArgs == null) throw new IllegalArgumentException("defArgs can't be null");
	if (newInstance == null) throw new IllegalArgumentException("create can't be null");
	if (decoders == null) throw new IllegalArgumentException("decoders can't be null");
	this.manifest = manifest;
	this.defArgs = defArgs;
	this.newInstance = newInstance;
	this.decoders = DecodePropertyInfo.prepare(decoders);
	this.skipOnUnknown = skipOnUnknown;
	this.mandatoryFlag = DecodePropertyInfo.calculateMandatory(this.decoders);
	hasMandatory = mandatoryFlag != 0;
	this.startError = String.format("Expecting '{' to start decoding %s", Reflection.typeDescription(manifest));
	this.endError = String.format("Expecting '}' or ',' while decoding %s", Reflection.typeDescription(manifest));
}
 
Example #6
Source File: ArrayFormatDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public ArrayFormatDescription(
		final Type manifest,
		final InstanceFactory<B> newInstance,
		final Settings.Function<B, T> finalize,
		final JsonWriter.WriteObject[] encoders,
		final JsonReader.BindObject[] decoders) {
	if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
	if (newInstance == null) throw new IllegalArgumentException("create can't be null");
	if (finalize == null) throw new IllegalArgumentException("finalize can't be null");
	if (encoders == null) throw new IllegalArgumentException("encoders can't be null or empty");
	if (decoders == null) throw new IllegalArgumentException("decoders can't be null");
	if (encoders.length != decoders.length) throw new IllegalArgumentException("decoders must match encoders (" + decoders.length + " != " + encoders.length + ")");
	this.manifest = manifest;
	this.newInstance = newInstance;
	this.finalize = finalize;
	this.isEmpty = encoders.length == 0;
	this.encoders = encoders.clone();
	this.decoders = decoders.clone();
	this.startError = String.format("Expecting '[' to start decoding %s", Reflection.typeDescription(manifest));
	this.endError = String.format("Expecting ']' to end decoding %s", Reflection.typeDescription(manifest));
	this.countError = String.format("Expecting to read %d elements in the array while decoding %s", decoders.length, Reflection.typeDescription(manifest));
}
 
Example #7
Source File: DecodePropertyInfo.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
static void showMandatoryError(
		final JsonReader reader,
		final long mandatoryFlag,
		final DecodePropertyInfo[] decoders) throws ParsingException {
	final StringBuilder sb = new StringBuilder("Mandatory ");
	sb.append(Long.bitCount(mandatoryFlag) == 1 ? "property" : "properties");
	sb.append(" (");
	for (final DecodePropertyInfo ri : decoders) {
		if (ri.mandatory && (mandatoryFlag & ~ri.mandatoryValue) != 0) {
			sb.append(ri.name).append(", ");
		}
	}
	sb.setLength(sb.length() - 2);
	sb.append(") not found");
	throw reader.newParseErrorAt(sb.toString(), 1);
}
 
Example #8
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 #9
Source File: ArrayDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ArrayDecoder(
		final T[] emptyInstance,
		final JsonReader.ReadObject<T> decoder) {
	if (emptyInstance == null) throw new IllegalArgumentException("emptyInstance can't be null");
	if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
	this.emptyInstance = emptyInstance;
	this.decoder = decoder;
}
 
Example #10
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 #11
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 #12
Source File: AttributeDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
AttributeDecoder(
		final Settings.BiConsumer<T, P> write,
		final JsonReader.ReadObject<P> decoder) {
	if (write == null) throw new IllegalArgumentException("write can't be null");
	if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
	this.write = write;
	this.decoder = decoder;
}
 
Example #13
Source File: CollectionDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public CollectionDecoder(
		final Type manifest,
		final Callable<T> newInstance,
		final JsonReader.ReadObject<E> decoder) {
	if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
	if (newInstance == null) throw new IllegalArgumentException("create can't be null");
	if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
	this.manifest = manifest;
	this.newInstance = newInstance;
	this.decoder = decoder;
}
 
Example #14
Source File: MapDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MapDecoder(
		final Type manifest,
		final Callable<T> newInstance,
		final JsonReader.ReadObject<K> keyDecoder,
		final JsonReader.ReadObject<V> valueDecoder) {
	if (manifest == null) throw new IllegalArgumentException("manifest can't be null");
	if (newInstance == null) throw new IllegalArgumentException("create can't be null");
	if (keyDecoder == null) throw new IllegalArgumentException("keyDecoder can't be null");
	if (valueDecoder == null) throw new IllegalArgumentException("valueDecoder can't be null");
	this.manifest = manifest;
	this.newInstance = newInstance;
	this.keyDecoder = keyDecoder;
	this.valueDecoder = valueDecoder;
}
 
Example #15
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;
}
 
Example #16
Source File: LazyAttributeDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public T bind(final JsonReader reader, final T instance) throws IOException {
	if (decoder == null) {
		decoder = json.tryFindReader(type);
		if (decoder == null) {
			throw new ConfigurationException("Unable to find reader for " + type);
		}
	}
	final P attr = decoder.read(reader);
	write.accept(instance, attr);
	return instance;
}
 
Example #17
Source File: EnumDescription.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;
	final int hash = reader.calcHash();
	for (final DecodePropertyInfo<T> ri : decoders) {
		if (hash == ri.hash) {
			if (ri.exactName && !reader.wasLastName(ri.nameBytes)) continue;
			return ri.value;
		}
	}
	return Enum.valueOf(manifest, reader.getLastName());
}
 
Example #18
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 #19
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 #20
Source File: ArrayFormatDescription.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;
	final B instance = newInstance.create();
	bind(reader, instance);
	return finalize.apply(instance);
}
 
Example #21
Source File: ArrayFormatDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static <D> ArrayFormatDescription<D, D> create(
		final Class<D> manifest,
		final InstanceFactory<D> newInstance,
		final JsonWriter.WriteObject[] encoders,
		final JsonReader.BindObject[] decoders) {
	return new ArrayFormatDescription<>(manifest, newInstance, identity, encoders, decoders);
}
 
Example #22
Source File: ImmutableDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void skip(final JsonReader reader) throws IOException {
	if (!skipOnUnknown) {
		final String name = reader.getLastName();
		throw reader.newParseErrorFormat("Unknown property detected", name.length() + 3, "Unknown property detected: '%s' while reading %s", name, Reflection.typeDescription(manifest));
	}
	reader.getNextToken();
	reader.skip();
}
 
Example #23
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 #24
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 #25
Source File: ImmutableDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ImmutableDescription(
		final Class<T> manifest,
		final Object[] defArgs,
		final Settings.Function<Object[], T> newInstance,
		final JsonWriter.WriteObject[] encoders,
		final DecodePropertyInfo<JsonReader.ReadObject>[] decoders,
		final boolean alwaysSerialize,
		final boolean skipOnUnknown) {
	this((Type) manifest, defArgs, newInstance, encoders, decoders, alwaysSerialize, skipOnUnknown);
}
 
Example #26
Source File: ArrayFormatDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public T readContent(final JsonReader reader) throws IOException {
	final B instance = newInstance.create();
	bindContent(reader, instance);
	return finalize.apply(instance);
}
 
Example #27
Source File: AttributeDecoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public T bind(final JsonReader reader, final T instance) throws IOException {
	final P attr = decoder.read(reader);
	write.accept(instance, attr);
	return instance;
}
 
Example #28
Source File: BValueJsonCodec.java    From gridgo with MIT License 4 votes vote down vote up
@Override
public BValue read(JsonReader reader) throws IOException {
    return read(reader, true);
}
 
Example #29
Source File: BElementJsonCodec.java    From gridgo with MIT License 4 votes vote down vote up
@Override
public BElement read(JsonReader reader) throws IOException {
    return read(reader, true);
}
 
Example #30
Source File: FormatConverter.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
T readContent(JsonReader reader) throws IOException;