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

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

public abstract class MapConverter {

	private static final JsonReader.ReadObject> TypedMapReader = new JsonReader.ReadObject>() {
		@Nullable
		@Override
		public Map read(JsonReader reader) throws IOException {
			return reader.wasNull() ? null : deserialize(reader);
		}
	};

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

	public static void serialize(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();
			StringConverter.serializeShort(kv.getKey(), sw);
			sw.writeByte(JsonWriter.SEMI);
			StringConverter.serializeNullable(kv.getValue(), sw);
			for (int i = 1; i < size; i++) {
				sw.writeByte(JsonWriter.COMMA);
				kv = iterator.next();
				StringConverter.serializeShort(kv.getKey(), sw);
				sw.writeByte(JsonWriter.SEMI);
				StringConverter.serializeNullable(kv.getValue(), sw);
			}
		}
		sw.writeByte(JsonWriter.OBJECT_END);
	}

	public static Map deserialize(final JsonReader reader) throws IOException {
		if (reader.last() != '{') throw reader.newParseError("Expecting '{' for map start");
		byte nextToken = reader.getNextToken();
		if (nextToken == '}') return new LinkedHashMap(0);
		final LinkedHashMap res = new LinkedHashMap();
		String key = StringConverter.deserialize(reader);
		nextToken = reader.getNextToken();
		if (nextToken != ':') throw reader.newParseError("Expecting ':' after attribute name");
		reader.getNextToken();
		String value = StringConverter.deserializeNullable(reader);
		res.put(key, value);
		while ((nextToken = reader.getNextToken()) == ',') {
			reader.getNextToken();
			key = StringConverter.deserialize(reader);
			nextToken = reader.getNextToken();
			if (nextToken != ':') throw reader.newParseError("Expecting ':' after attribute name");
			reader.getNextToken();
			value = StringConverter.deserializeNullable(reader);
			res.put(key, value);
		}
		if (nextToken != '}') throw reader.newParseError("Expecting '}' for map end");
		return res;
	}

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy