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

com.dslplatform.json.ObjectConverter 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.*;

public abstract class ObjectConverter {

	private static final JsonReader.ReadObject> TypedMapReader = new JsonReader.ReadObject>() {
		@Override
		public Map read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserializeMap(reader);
		}
	};
	@SuppressWarnings("rawtypes")
	static final JsonReader.ReadObject MapReader = new JsonReader.ReadObject() {
		@Override
		public LinkedHashMap read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserializeMap(reader);
		}
	};

	public static void serializeNullableMap(final Map value, final JsonWriter sw) {
		if (value == null) {
			sw.writeNull();
		} else {
			serializeMap(value, sw);
		}
	}

	public static void serializeMap(final Map value, final JsonWriter sw) {
		sw.writeByte(JsonWriter.OBJECT_START);
		final int size = value.size();
		if (size > 0) {
			final Iterator> iterator = value.entrySet().iterator();
			Map.Entry kv = iterator.next();
			sw.writeString(kv.getKey());
			sw.writeByte(JsonWriter.SEMI);
			sw.serializeObject(kv.getValue());
			for (int i = 1; i < size; i++) {
				sw.writeByte(JsonWriter.COMMA);
				kv = iterator.next();
				sw.writeString(kv.getKey());
				sw.writeByte(JsonWriter.SEMI);
				sw.serializeObject(kv.getValue());
			}
		}
		sw.writeByte(JsonWriter.OBJECT_END);
	}

	public static void serializeObject(final Object value, final JsonWriter sw) throws IOException {
		sw.serializeObject(value);
	}

	public static Object deserializeObject(final JsonReader reader) throws IOException {
		switch (reader.last()) {
			case 'n':
				if (!reader.wasNull()) {
					throw reader.expecting("null");
				}
				return null;
			case 't':
				if (!reader.wasTrue()) {
					throw reader.expecting("true");
				}
				return true;
			case 'f':
				if (!reader.wasFalse()) {
					throw reader.expecting("false");
				}
				return false;
			case '"':
				return reader.readString();
			case '{':
				return deserializeMap(reader);
			case '[':
				return deserializeList(reader);
			default:
				return NumberConverter.deserializeNumber(reader);
		}
	}

	public static ArrayList deserializeList(final JsonReader reader) throws IOException {
		if (reader.last() != '[') {
			throw reader.expecting("[");
		}
		byte nextToken = reader.getNextToken();
		if (nextToken == ']') return new ArrayList(0);
		final ArrayList res = new ArrayList(4);
		res.add(deserializeObject(reader));
		while ((nextToken = reader.getNextToken()) == ',') {
			reader.getNextToken();
			res.add(deserializeObject(reader));
		}
		if (nextToken != ']') {
			throw reader.expecting("]", nextToken);
		}
		return res;
	}

	public static LinkedHashMap deserializeMap(final JsonReader reader) throws IOException {
		if (reader.last() != '{') {
			throw reader.expecting("{");
		}
		byte nextToken = reader.getNextToken();
		if (nextToken == '}') return new LinkedHashMap(0);
		final LinkedHashMap res = new LinkedHashMap();
		String key = reader.readKey();
		res.put(key, deserializeObject(reader));
		while ((nextToken = reader.getNextToken()) == ',') {
			reader.getNextToken();
			key = reader.readKey();
			res.put(key, deserializeObject(reader));
		}
		if (nextToken != '}') {
			throw reader.expecting("}", nextToken);
		}
		return res;
	}

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

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

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

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