Java Code Examples for com.dslplatform.json.JsonWriter#WriteObject

The following examples show how to use com.dslplatform.json.JsonWriter#WriteObject . 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: 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 2
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 3
Source File: OptionalAnalyzer.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Nullable
private static OptionalEncoder analyzeEncoding(final Type manifest, final Type content, final Class<?> raw, final DslJson json) {
	if (raw != Optional.class) {
		return null;
	} else if (content == Optional.class) {
		final OptionalEncoder nested = analyzeEncoding(content, Object.class, Optional.class, json);
		json.registerWriter(manifest, nested);
		return nested;
	}
	final JsonWriter.WriteObject<?> writer = Object.class == content ? null : json.tryFindWriter(content);
	if (Object.class != content && writer == null) {
		return null;
	}
	final OptionalEncoder encoder = new OptionalEncoder<>(json, Object.class == content ? null : writer);
	json.registerWriter(manifest, encoder);
	return encoder;
}
 
Example 4
Source File: BuilderTest.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public BuilderTest() {
	ObjectFormatDescription<Immutable.Builder, Immutable> description = new ObjectFormatDescription<Immutable.Builder, Immutable>(
			Immutable.class,
			Immutable.Builder::new,
			Immutable.Builder::build,
			new JsonWriter.WriteObject[] {
					Settings.<Immutable, Integer>createEncoder(c -> c.x, "x", json, int.class),
					Settings.<Immutable, Long>createEncoder(c -> c.y, "y", json, long.class)
			},
			new DecodePropertyInfo[] {
					Settings.<Immutable.Builder, Integer>createDecoder(Immutable.Builder::x, "x", json, int.class),
					Settings.<Immutable.Builder, Long>createDecoder(Immutable.Builder::y, "y", json, long.class)
			},
			json,
			true
	);
	json.registerReader(Immutable.class, description);
	json.registerWriter(Immutable.class, description);
}
 
Example 5
Source File: AttributeArrayEncoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
AttributeArrayEncoder(
		final Settings.Function<T, R> read,
		final JsonWriter.WriteObject<R> encoder) {
	if (read == null) throw new IllegalArgumentException("read can't be null");
	if (encoder == null) throw new IllegalArgumentException("encoder can't be null");
	this.read = read;
	this.encoder = encoder;
}
 
Example 6
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 7
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 8
Source File: AttributeObjectAlwaysEncoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
AttributeObjectAlwaysEncoder(
		final Settings.Function<T, R> read,
		final String name,
		final JsonWriter.WriteObject<R> encoder) {
	if (read == null) throw new IllegalArgumentException("read can't be null");
	if (name == null || name.isEmpty()) throw new IllegalArgumentException("name can't be null");
	if (encoder == null) throw new IllegalArgumentException("encoder can't be null");
	this.read = read;
	quotedName = ("\"" + name + "\":").getBytes(utf8);
	this.encoder = encoder;
}
 
Example 9
Source File: AttributeObjectNonDefaultEncoder.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
AttributeObjectNonDefaultEncoder(
		final Settings.Function<T, R> read,
		final String name,
		final JsonWriter.WriteObject<R> encoder,
		@Nullable final R defaultValue) {
	if (read == null) throw new IllegalArgumentException("read can't be null");
	if (name == null || name.isEmpty()) throw new IllegalArgumentException("name can't be null");
	if (encoder == null) throw new IllegalArgumentException("encoder can't be null");
	this.read = read;
	quotedName = ("\"" + name + "\":").getBytes(utf8);
	this.defaultValue = defaultValue;
	this.encoder = encoder;
}
 
Example 10
Source File: WriteDescription.java    From dsl-json with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
WriteDescription(final JsonWriter.WriteObject[] encoders, final boolean alwaysSerialize) {
	if (encoders == null) throw new IllegalArgumentException("encoders can't be null or empty");
	this.encoders = encoders.clone();
	this.alwaysSerialize = alwaysSerialize;
	this.isEmpty = encoders.length == 0;
}