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

com.dslplatform.json.BoolConverter Maven / Gradle / Ivy

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public abstract class BoolConverter {

	static final JsonReader.ReadObject BooleanReader = new JsonReader.ReadObject() {
		@Override
		public Boolean read(JsonReader reader) throws IOException {
			return deserialize(reader);
		}
	};
	static final JsonReader.ReadObject NullableBooleanReader = new JsonReader.ReadObject() {
		@Override
		public Boolean read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserialize(reader);
		}
	};
	static final JsonWriter.WriteObject BooleanWriter = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, Boolean value) {
			serializeNullable(value, writer);
		}
	};
	static final JsonReader.ReadObject BooleanArrayReader = new JsonReader.ReadObject() {
		@Override
		public boolean[] read(JsonReader reader) throws IOException {
			if (reader.wasNull()) return null;
			if (reader.last() != '[') throw reader.expecting("[");
			reader.getNextToken();
			return deserializeBoolArray(reader);
		}
	};
	static final JsonWriter.WriteObject BooleanArrayWriter = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, boolean[] value) {
			serialize(value, writer);
		}
	};

	public static void serializeNullable(final Boolean value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else if (value) {
			sw.writeAscii("true");
		} else {
			sw.writeAscii("false");
		}
	}

	public static void serialize(final boolean value, final JsonWriter sw) {
		if (value) {
			sw.writeAscii("true");
		} else {
			sw.writeAscii("false");
		}
	}

	public static void serialize(final boolean[] value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else if (value.length == 0) {
			sw.writeAscii("[]");
		} else {
			sw.writeByte(JsonWriter.ARRAY_START);
			sw.writeAscii(value[0] ? "true" : "false");
			for(int i = 1; i < value.length; i++) {
				sw.writeAscii(value[i] ? ",true" : ",false");
			}
			sw.writeByte(JsonWriter.ARRAY_END);
		}
	}

	public static boolean deserialize(final JsonReader reader) throws IOException {
		if (reader.wasTrue()) {
			return true;
		} else if (reader.wasFalse()) {
			return false;
		}
		throw new IOException("Found invalid boolean value at: " + reader.positionInStream());
	}

	public static boolean[] deserializeBoolArray(final JsonReader reader) throws IOException {
		if (reader.last() == ']') {
			return new boolean[0];
		}
		boolean[] buffer = new boolean[4];
		buffer[0] = deserialize(reader);
		int i = 1;
		while (reader.getNextToken() == ',') {
			reader.getNextToken();
			if (i == buffer.length) {
				buffer = Arrays.copyOf(buffer, buffer.length << 1);
			}
			buffer[i++] = deserialize(reader);
		}
		reader.checkArrayEnd();
		return Arrays.copyOf(buffer, i);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList deserializeCollection(final JsonReader reader) throws IOException {
		return reader.deserializeCollection(BooleanReader);
	}

	public static void deserializeCollection(final JsonReader reader, final Collection res) throws IOException {
		reader.deserializeCollection(BooleanReader, res);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList deserializeNullableCollection(final JsonReader reader) throws IOException {
		return reader.deserializeNullableCollection(BooleanReader);
	}

	public static void deserializeNullableCollection(final JsonReader reader, final Collection res) throws IOException {
		reader.deserializeNullableCollection(BooleanReader, res);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy