All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dslplatform.json.runtime.Settings Maven / Gradle / Ivy

There is a newer version: 1.10.0
Show newest version
package com.dslplatform.json.runtime;

import com.dslplatform.json.*;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.function.BiConsumer;
import java.util.function.Function;

public abstract class Settings {
	private static final DslJson.ConverterFactory UNKNOWN_READER = new DslJson.ConverterFactory() {
		@Override
		public JsonReader.ReadObject tryCreate(Type manifest, DslJson dslJson) {
			return Object.class == manifest ? new JsonReader.ReadObject() {
				@Override
				public Object read(JsonReader reader) throws IOException {
					return ObjectConverter.deserializeObject(reader);
				}
			} : null;
		}
	};

	static boolean isKnownType(final Type type) {
		if (type == Object.class) return false;
		if (type instanceof Class) {
			Class manifest = (Class)type;
			if (manifest.isInterface()) return false;
			return (manifest.getModifiers() & Modifier.ABSTRACT) == 0;
		}
		return type != null;
	}

	public static  JsonWriter.WriteObject createEncoder(
			final Function read,
			final String name,
			final DslJson json,
			final Type type) {
		if (read == null) throw new IllegalArgumentException("read can't be null");
		if (name == null) throw new IllegalArgumentException("name can't be null");
		if (json == null) throw new IllegalArgumentException("json can't be null");
		final JsonWriter.WriteObject encoder = type != null ? json.tryFindWriter(type) : null;
		if (encoder == null || Object.class.equals(type)) {
			return new LazyAttributeObjectEncoder<>(read, name, json, type);
		}
		if (json.omitDefaults) {
			return new AttributeObjectNonDefaultEncoder<>(read, name, encoder, (R)json.getDefault(type));
		}
		return new AttributeObjectAlwaysEncoder<>(read, name, encoder);
	}

	public static  JsonWriter.WriteObject createArrayEncoder(
			final Function read,
			final DslJson json,
			final Type type) {
		if (read == null) throw new IllegalArgumentException("read can't be null");
		if (json == null) throw new IllegalArgumentException("json can't be null");
		final JsonWriter.WriteObject encoder = type != null ? json.tryFindWriter(type) : null;
		if (encoder == null || Object.class.equals(type)) return new LazyAttributeArrayEncoder<>(read, json, type);
		return new AttributeArrayEncoder<>(read, encoder);
	}

	public static  JsonWriter.WriteObject createArrayEncoder(
			final Function read,
			final JsonWriter.WriteObject encoder) {
		if (read == null) throw new IllegalArgumentException("read can't be null");
		if (encoder == null) throw new IllegalArgumentException("encoder can't be null");
		return new AttributeArrayEncoder<>(read, encoder);
	}

	public static  DecodePropertyInfo> createDecoder(
			final BiConsumer write,
			final String name,
			final DslJson json,
			final Class manifest) {
		return createDecoder(write, name, json, false, false, -1, false, manifest);
	}

	public static  DecodePropertyInfo> createDecoder(
			final BiConsumer write,
			final String name,
			final DslJson json,
			final boolean exactNameMatch,
			final boolean isMandatory,
			final int index,
			final boolean nonNull,
			final Type type) {
		if (write == null) throw new IllegalArgumentException("write can't be null");
		if (name == null) throw new IllegalArgumentException("name can't be null");
		if (json == null) throw new IllegalArgumentException("json can't be null");
		final JsonReader.ReadObject decoder = type != null ? json.tryFindReader(type) : null;
		if (decoder == null || !isKnownType(type)) return new DecodePropertyInfo<>(name, exactNameMatch, isMandatory, index, nonNull, new LazyAttributeDecoder<>(write, json, type));
		return new DecodePropertyInfo<>(name, exactNameMatch, isMandatory, index, nonNull, new AttributeDecoder<>(write, decoder));
	}

	public static  DecodePropertyInfo> createDecoder(
			final BiConsumer write,
			final String name,
			final DslJson json,
			final boolean exactNameMatch,
			final boolean isMandatory,
			final int index,
			final boolean nonNull,
			final JsonReader.ReadObject decoder) {
		if (write == null) throw new IllegalArgumentException("write can't be null");
		if (name == null) throw new IllegalArgumentException("name can't be null");
		if (json == null) throw new IllegalArgumentException("json can't be null");
		if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
		return new DecodePropertyInfo<>(name, exactNameMatch, isMandatory, index, nonNull, new AttributeDecoder<>(write, decoder));
	}

	public static  JsonReader.BindObject createArrayDecoder(
			final BiConsumer write,
			final DslJson json,
			final Type type) {
		if (write == null) throw new IllegalArgumentException("write can't be null");
		if (json == null) throw new IllegalArgumentException("json can't be null");
		final JsonReader.ReadObject decoder = type != null ? json.tryFindReader(type) : null;
		if (decoder == null || !isKnownType(type)) return new LazyAttributeDecoder<>(write, json, type);
		return new AttributeDecoder<>(write, decoder);
	}

	public static  JsonReader.BindObject createArrayDecoder(
			final BiConsumer write,
			final JsonReader.ReadObject decoder) {
		if (write == null) throw new IllegalArgumentException("write can't be null");
		if (decoder == null) throw new IllegalArgumentException("decoder can't be null");
		return new AttributeDecoder<>(write, decoder);
	}

	public static  DslJson.Settings withRuntime() {
		return new DslJson.Settings()
				.resolveReader(UNKNOWN_READER)
				.resolveReader(CollectionAnalyzer.READER)
				.resolveWriter(CollectionAnalyzer.WRITER)
				.resolveReader(ArrayAnalyzer.READER)
				.resolveWriter(ArrayAnalyzer.WRITER)
				.resolveReader(MapAnalyzer.READER)
				.resolveWriter(MapAnalyzer.WRITER)
				.resolveWriter(EnumAnalyzer.CONVERTER)
				.resolveReader(EnumAnalyzer.CONVERTER)
				.resolveWriter(ObjectAnalyzer.CONVERTER)
				.resolveBinder(ObjectAnalyzer.CONVERTER)
				.resolveReader(ObjectAnalyzer.CONVERTER)
				.resolveWriter(ImmutableAnalyzer.CONVERTER)
				.resolveReader(ImmutableAnalyzer.CONVERTER)
				.resolveWriter(MixinAnalyzer.WRITER)
				.with(new ConfigureJava8());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy