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

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

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

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;

public abstract class NetConverter {

	static final JsonReader.ReadObject UriReader = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public URI read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserializeUri(reader);
		}
	};
	static final JsonWriter.WriteObject UriWriter = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, @Nullable URI value) {
			serializeNullable(value, writer);
		}
	};
	static final JsonReader.ReadObject AddressReader = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public InetAddress read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserializeIp(reader);
		}
	};
	static final JsonWriter.WriteObject AddressWriter = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, @Nullable InetAddress value) {
			serializeNullable(value, writer);
		}
	};

	public static void serializeNullable(@Nullable final URI value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else {
			serialize(value, sw);
		}
	}

	public static void serialize(final URI value, final JsonWriter sw) {
		StringConverter.serializeShort(value.toString(), sw);
	}

	public static URI deserializeUri(final JsonReader reader) throws IOException {
		return URI.create(reader.readString());
	}

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

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

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

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

	public static void serializeNullable(@Nullable final InetAddress value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else {
			serialize(value, sw);
		}
	}

	public static void serialize(final InetAddress value, final JsonWriter sw) {
		sw.writeByte(JsonWriter.QUOTE);
		sw.writeAscii(value.getHostAddress());
		sw.writeByte(JsonWriter.QUOTE);
	}

	public static InetAddress deserializeIp(final JsonReader reader) throws IOException {
		return InetAddress.getByName(reader.readSimpleString());
	}

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy