com.basistech.rosette.dm.jackson.MapAttributeDeserializer Maven / Gradle / Ivy
/*
* Copyright 2018 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.basistech.rosette.dm.jackson;
import com.basistech.rosette.dm.BaseAttribute;
import com.basistech.rosette.dm.MapAttribute;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.JsonParserSequence;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.util.TokenBuffer;
import com.google.common.collect.Maps;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Map;
/**
* Jackson deserializer for MapAttributes that avoids writing out
* the same type information repeatedly.
*/
public class MapAttributeDeserializer extends JsonDeserializer {
@Override
@SuppressWarnings("unchecked")
public MapAttribute deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
if (jp.getCurrentToken() == JsonToken.START_OBJECT) { // this is what we expect.
// we advance to be in the same place the 'else' will be -- the first FIELD_NAME.
jp.nextToken();
} else {
/* In a full AnnotatedText, which is already doing some polymorphism shuffling, we end up here. */
/* We find a FIELD_NAME for the first field of the object */
if (jp.getCurrentToken() != JsonToken.FIELD_NAME) {
throw ctxt.wrongTokenException(jp, JsonToken.START_OBJECT, "MapAttributeDeserializer called not at or FIELD_NAME of first field");
}
/* We are at the field name, ready for the loop. */
}
TokenBuffer tb = null;
String keyName = null;
String valueName = null;
for (JsonToken t = jp.getCurrentToken(); t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
String name = jp.getCurrentName();
if ("keyType".equals(name)) { // gotcha!
keyName = getName(jp);
}
if ("valueType".equals(name)) {
valueName = getName(jp);
}
if (keyName != null && valueName != null) {
// both have been found
return deserialize(jp, ctxt, tb, keyName, valueName);
}
if (tb == null) {
tb = new TokenBuffer(null, false);
}
if (!("keyType".equals(name) || "valueType".equals(name))) {
tb.copyCurrentStructure(jp);
}
}
if (keyName == null) {
throw ctxt.mappingException("No keyType provided in a map");
}
throw ctxt.mappingException("No valueType provided in a map");
}
private String getName(JsonParser jp) throws IOException {
jp.nextToken();
return jp.getText();
}
private TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy