com.adgear.anoa.read.MapReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anoa-core Show documentation
Show all versions of anoa-core Show documentation
Core classes for Anoa library, which aims to be a safe, convenient and fast record
de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms
of Java 8.
The anoa-core module tries to keep upstream dependencies to a minimum.
package com.adgear.anoa.read;
import com.adgear.anoa.AnoaJacksonTypeException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.IOException;
import java.util.HashMap;
class MapReader extends AbstractReader> {
final AbstractReader> valueElementReader;
MapReader(AbstractReader> valueElementReader) {
this.valueElementReader = valueElementReader;
}
@Override
protected HashMap read(JsonParser jacksonParser) throws IOException {
if (jacksonParser.getCurrentToken() == JsonToken.START_OBJECT) {
HashMap result = new HashMap<>();
doMap(jacksonParser, (k, p) -> result.put(k, valueElementReader.read(p)));
return result;
} else {
gobbleValue(jacksonParser);
return null;
}
}
@Override
protected HashMap readStrict(JsonParser jacksonParser) throws
AnoaJacksonTypeException, IOException {
switch (jacksonParser.getCurrentToken()) {
case VALUE_NULL:
return null;
case START_OBJECT:
HashMap result = new HashMap<>();
doMap(jacksonParser, (k, p) -> result.put(k, valueElementReader.readStrict(p)));
return result;
default:
throw new AnoaJacksonTypeException("Token is not '{': " + jacksonParser.getCurrentToken());
}
}
}