com.hurence.logisland.serializer.ExtendedJsonSerializer Maven / Gradle / Ivy
/**
* Copyright (C) 2016 Hurence ([email protected])
*
* 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.hurence.logisland.serializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hurence.logisland.record.*;
import com.hurence.logisland.util.time.DateUtil;
import org.apache.avro.LogicalType;
import org.apache.avro.LogicalTypes;
import org.apache.avro.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.text.DateFormat;
import java.text.ParseException;
import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Basic but complete Json {@link RecordSerializer}.
* It support nested object and arrays.
*
* Useful when the input data is coming directly in JSON format from Kafka.
*
* @author amarziali
*/
public class ExtendedJsonSerializer implements RecordSerializer {
private static Logger logger = LoggerFactory.getLogger(com.hurence.logisland.serializer.JsonSerializer.class);
private final Schema schema;
private transient volatile ObjectMapper mapper;
public ExtendedJsonSerializer(Schema schema) {
this.schema = schema;
}
public ExtendedJsonSerializer() {
this.schema = null;
}
public void doFilter(Map map, String name, Object value, Schema schema) {
final Schema.Field field = name != null ? schema.getField(name) : null;
if (field != null || name == null) {
if (value == null) {
map.put(name, null);
} else {
final Schema currentSchema = field != null ? field.schema() : schema;
LogicalType logicalType = currentSchema.getLogicalType();
if (logicalType instanceof LogicalTypes.Date ||
logicalType instanceof LogicalTypes.TimestampMillis) {
if (value instanceof Number) {
map.put(name, new Date(((Number) value).longValue()));
} else {
try {
map.put(name, new Date(Instant.parse(value.toString()).toEpochMilli()));
} catch (DateTimeParseException ignored) {
try {
//falling back to logisland date parser
map.put(name, DateUtil.parse(value.toString()));
} catch (ParseException pe) {
logger.warn("Error converting field {} with value {} to date : {}",
field.name(), value, pe.getMessage());
}
}
}
} else {
switch (currentSchema.getType()) {
case FLOAT:
map.put(name, Float.parseFloat(value.toString()));
break;
case LONG:
map.put(name, Long.parseLong(value.toString()));
break;
case ARRAY:
final Map tmpMap = new LinkedHashMap<>();
List