org.apache.pulsar.io.http.JsonConverter Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.pulsar.io.http;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.avro.Conversion;
import org.apache.avro.Conversions;
import org.apache.avro.Schema;
import org.apache.avro.data.TimeConversions;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericFixed;
import org.apache.avro.generic.GenericRecord;
/**
* Convert an AVRO GenericRecord to a JsonNode.
*/
public class JsonConverter {
private static final Map> logicalTypeConverters = new HashMap<>();
private static final JsonNodeFactory jsonNodeFactory = JsonNodeFactory.withExactBigDecimals(true);
public static JsonNode topLevelMerge(JsonNode n1, JsonNode n2) {
ObjectNode objectNode = jsonNodeFactory.objectNode();
n1.fieldNames().forEachRemaining(f -> objectNode.put(f, n1.get(f)));
n2.fieldNames().forEachRemaining(f -> objectNode.put(f, n2.get(f)));
return objectNode;
}
public static JsonNode toJson(GenericRecord genericRecord) {
if (genericRecord == null) {
return null;
}
ObjectNode objectNode = jsonNodeFactory.objectNode();
for (Schema.Field field : genericRecord.getSchema().getFields()) {
objectNode.set(field.name(), toJson(field.schema(), genericRecord.get(field.name())));
}
return objectNode;
}
public static JsonNode toJson(Schema schema, Object value) {
if (schema.getLogicalType() != null && logicalTypeConverters.containsKey(schema.getLogicalType().getName())) {
return logicalTypeConverters.get(schema.getLogicalType().getName()).toJson(schema, value);
}
if (value == null) {
return jsonNodeFactory.nullNode();
}
switch(schema.getType()) {
case NULL: // this should not happen
return jsonNodeFactory.nullNode();
case INT:
return jsonNodeFactory.numberNode((Integer) value);
case LONG:
return jsonNodeFactory.numberNode((Long) value);
case DOUBLE:
return jsonNodeFactory.numberNode((Double) value);
case FLOAT:
return jsonNodeFactory.numberNode((Float) value);
case BOOLEAN:
return jsonNodeFactory.booleanNode((Boolean) value);
case BYTES:
return jsonNodeFactory.binaryNode((byte[]) value);
case FIXED:
return jsonNodeFactory.binaryNode(((GenericFixed) value).bytes());
case ENUM: // GenericEnumSymbol
case STRING:
return jsonNodeFactory.textNode(value.toString()); // can be a String or org.apache.avro.util.Utf8
case ARRAY: {
Schema elementSchema = schema.getElementType();
ArrayNode arrayNode = jsonNodeFactory.arrayNode();
Object[] iterable;
if (value instanceof GenericData.Array) {
iterable = ((GenericData.Array) value).toArray();
} else {
iterable = (Object[]) value;
}
for (Object elem : iterable) {
JsonNode fieldValue = toJson(elementSchema, elem);
arrayNode.add(fieldValue);
}
return arrayNode;
}
case MAP: {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy