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

io.activej.json.AbstractMapJsonCodec Maven / Gradle / Ivy

The newest version!
package io.activej.json;

import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import static com.dslplatform.json.JsonWriter.*;
import static io.activej.common.Checks.checkNotNull;

public abstract class AbstractMapJsonCodec implements JsonCodec {
	public record JsonMapEntry(String key, V value) {
		public static  JsonMapEntry of(Map.Entry mapEntry) {
			return new JsonMapEntry<>(mapEntry.getKey(), mapEntry.getValue());
		}

		public static  JsonMapEntry of(Map.Entry mapEntry, JsonKeyEncoder keyEncoder) {
			return new JsonMapEntry<>(keyEncoder.encode(mapEntry.getKey()), mapEntry.getValue());
		}
	}

	protected abstract Iterator> iterate(T item);

	protected abstract @Nullable JsonEncoder encoder(String key, int index, T item, V value);

	protected abstract @Nullable JsonDecoder decoder(String key, int index, A accumulator) throws JsonValidationException;

	protected abstract A accumulator();

	protected abstract void accumulate(A accumulator, String key, int index, V value) throws JsonValidationException;

	protected abstract T result(A accumulator, int count) throws JsonValidationException;

	@Override
	public final void write(JsonWriter writer, T item) {
		checkNotNull(item);
		writer.writeByte(OBJECT_START);
		boolean comma = false;
		Iterator> iterator = iterate(item);
		int i = 0;
		while (iterator.hasNext()) {
			JsonMapEntry entry = iterator.next();
			String key = entry.key;
			V value = entry.value;
			JsonEncoder encoder = encoder(key, i++, item, value);
			if (encoder == null) continue;
			if (comma) writer.writeByte(COMMA);
			writer.writeString(key);
			writer.writeByte(SEMI);
			encoder.write(writer, value);
			comma = true;
		}
		writer.writeByte(OBJECT_END);
	}

	@Override
	public final T read(JsonReader reader) throws IOException {
		if (reader.last() != OBJECT_START) throw reader.newParseError("Expected '{'");
		A accumulator = accumulator();
		int i = 0;
		if (reader.getNextToken() != OBJECT_END) {
			while (true) {
				String key = reader.readKey();
				JsonDecoder decoder = decoder(key, i, accumulator);
				if (decoder == null) {
					reader.skip();
					continue;
				}
				V value = decoder.read(reader);
				accumulate(accumulator, key, i++, value);
				if (reader.getNextToken() != COMMA) break;
				reader.getNextToken();
			}
			reader.checkObjectEnd();
		}
		return result(accumulator, i);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy