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

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

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

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

public abstract class BinaryConverter {

	static final JsonReader.ReadObject Base64Reader = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public byte[] read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserialize(reader);
		}
	};
	static final JsonWriter.WriteObject Base64Writer = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, @Nullable byte[] value) {
			serialize(value, writer);
		}
	};

	public static void serialize(@Nullable final byte[] value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else if (value.length == 0) {
			sw.writeAscii("\"\"");
		} else {
			sw.writeBinary(value);
		}
	}

	public static byte[] deserialize(final JsonReader reader) throws IOException {
		return reader.readBase64();
	}

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy