
com.netflix.zeno.json.JsonFrameworkDeserializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of netflix-zeno Show documentation
Show all versions of netflix-zeno Show documentation
netflix-zeno developed by Netflix
/*
*
* Copyright 2013 Netflix, Inc.
*
* 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.netflix.zeno.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.netflix.zeno.serializer.FrameworkDeserializer;
import com.netflix.zeno.serializer.NFTypeSerializer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.codec.binary.Base64;
public class JsonFrameworkDeserializer extends FrameworkDeserializer {
JsonFrameworkDeserializer(JsonSerializationFramework framework) {
super(framework);
}
@Override
public Boolean deserializeBoolean(JsonReadGenericRecord rec, String fieldName) {
JsonReadGenericRecord record = (JsonReadGenericRecord) rec;
JsonNode node = record.getNode().isBoolean() ? record.getNode() : getJsonNode(rec, fieldName);
if (node == null)
return null;
return node.booleanValue();
}
@Override
public Integer deserializeInteger(JsonReadGenericRecord record, String fieldName) {
JsonNode node = record.getNode().isNumber() ? record.getNode() : getJsonNode(record, fieldName);
if (node == null)
return null;
return node.intValue();
}
@Override
public Long deserializeLong(JsonReadGenericRecord record, String fieldName) {
JsonNode node = record.getNode().isNumber() ? record.getNode() : getJsonNode(record, fieldName);
if (node == null)
return null;
return node.longValue();
}
@Override
public Float deserializeFloat(JsonReadGenericRecord record, String fieldName) {
JsonNode node = record.getNode().isNumber() ? record.getNode() : getJsonNode(record, fieldName);
if (node == null)
return null;
return node.numberValue().floatValue();
}
@Override
public Double deserializeDouble(JsonReadGenericRecord record, String fieldName) {
JsonNode node = record.getNode().isNumber() ? record.getNode() : getJsonNode(record, fieldName);
if (node == null)
return null;
return node.numberValue().doubleValue();
}
@Override
public String deserializeString(JsonReadGenericRecord record, String fieldName) {
JsonNode node = record.getNode().isTextual() ? record.getNode() : getJsonNode(record, fieldName);
if (node == null)
return null;
return node.textValue();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public T deserializeObject(JsonReadGenericRecord rec, String fieldName, String typeName, Class clazz) {
JsonNode node = getJsonNode(rec, fieldName);
if (node == null)
return null;
NFTypeSerializer serializer = ((NFTypeSerializer) (framework.getSerializer(typeName)));
return (T) serializer.deserialize(new JsonReadGenericRecord(node));
}
@Override
public List deserializeList(JsonReadGenericRecord record, String fieldName, NFTypeSerializer itemSerializer) {
JsonNode node = getJsonNode(record, "list");
if (node == null)
return null;
List list = new ArrayList();
deserializeCollection(node, itemSerializer, list);
return list;
}
@Override
public Set deserializeSet(JsonReadGenericRecord record, String fieldName, NFTypeSerializer itemSerializer) {
JsonNode node = getJsonNode(record, "set");
if (node == null)
return null;
Set set = new HashSet();
deserializeCollection(node, itemSerializer, set);
return set;
}
private JsonNode getJsonNode(Object rec, String fieldName) {
JsonReadGenericRecord record = (JsonReadGenericRecord) rec;
JsonNode node = record.getNode().get(fieldName);
if (node == null || node.isNull()) {
return null;
}
return node;
}
private void deserializeCollection(JsonNode nodes, NFTypeSerializer itemSerializer, Collection elements) {
try {
for (Iterator it = nodes.elements(); it.hasNext();) {
JsonNode node = it.next();
T element = itemSerializer.deserialize(new JsonReadGenericRecord(node));
elements.add(element);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@Override
public Map deserializeMap(JsonReadGenericRecord record, String fieldName, NFTypeSerializer keySerializer, NFTypeSerializer valueSerializer) {
return deserializeIntoMap(record, fieldName, keySerializer, valueSerializer, new HashMap());
}
@Override
public byte[] deserializeBytes(JsonReadGenericRecord record, String fieldName) {
String str = deserializeString(record, fieldName);
if (str == null) {
return null;
}
return Base64.decodeBase64(str);
}
@Override
public SortedMap deserializeSortedMap(JsonReadGenericRecord record, String fieldName, NFTypeSerializer keySerializer, NFTypeSerializer valueSerializer) {
return deserializeIntoMap(record, fieldName, keySerializer, valueSerializer, new TreeMap());
}
private > M deserializeIntoMap(JsonReadGenericRecord rec, String fieldName, NFTypeSerializer keySerializer, NFTypeSerializer valueSerializer, M map) {
JsonNode node = getJsonNode(rec, fieldName);
if (node == null) {
return null;
}
for (Iterator it = node.elements(); it.hasNext();) {
JsonNode element = it.next();
K key = keySerializer.deserialize(new JsonReadGenericRecord(element.get("key")));
V value = valueSerializer.deserialize(new JsonReadGenericRecord(element.get("value")));
map.put(key, value);
}
return map;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy