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

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

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

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

public abstract class StringConverter {

	public static final JsonReader.ReadObject READER = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public String read(JsonReader reader) throws IOException {
			if (reader.wasNull()) return null;
			return reader.readString();
		}
	};
	public static final JsonWriter.WriteObject WRITER = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, @Nullable String value) {
			serializeNullable(value, writer);
		}
	};
	public static final JsonWriter.WriteObject WRITER_CHARS = new JsonWriter.WriteObject() {
		@Override
		public void write(JsonWriter writer, @Nullable CharSequence value) {
			if (value == null) writer.writeNull();
			else writer.writeString(value);
		}
	};
	public static final JsonReader.ReadObject READER_BUILDER = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public StringBuilder read(JsonReader reader) throws IOException {
			if (reader.wasNull()) return null;
			StringBuilder builder = new StringBuilder();
			return reader.appendString(builder);
		}
	};
	public static final JsonReader.ReadObject READER_BUFFER = new JsonReader.ReadObject() {
		@Nullable
		@Override
		public StringBuffer read(JsonReader reader) throws IOException {
			if (reader.wasNull()) return null;
			StringBuffer builder = new StringBuffer();
			return reader.appendString(builder);
		}
	};

	public static void serializeShortNullable(@Nullable final String value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else {
			sw.writeString(value);
		}
	}

	public static void serializeShort(final String value, final JsonWriter sw) {
		sw.writeString(value);
	}

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

	public static void serialize(final String value, final JsonWriter sw) {
		sw.writeString(value);
	}

	public static String deserialize(final JsonReader reader) throws IOException {
		return reader.readString();
	}

	@Nullable
	public static String deserializeNullable(final JsonReader reader) throws IOException {
		if (reader.last() == 'n') {
			if (!reader.wasNull()) throw reader.newParseErrorAt("Expecting 'null' for null constant", 0);
			return null;
		}
		return reader.readString();
	}

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

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

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

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

	public static void serialize(final List list, final JsonWriter writer) {
		writer.writeByte(JsonWriter.ARRAY_START);
		if (list.size() != 0) {
			writer.writeString(list.get(0));
			for (int i = 1; i < list.size(); i++) {
				writer.writeByte(JsonWriter.COMMA);
				writer.writeString(list.get(i));
			}
		}
		writer.writeByte(JsonWriter.ARRAY_END);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy