org.apache.avro.data.Json 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
*
* https://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.avro.data;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.avro.util.internal.JacksonUtils;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.core.JsonFactory;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.JsonNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.ObjectMapper;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.LongNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.DoubleNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.TextNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.BooleanNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.NullNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.ArrayNode;
import com.facebook.presto.hive.$internal.com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.avro.Schema;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.ResolvingDecoder;
/** Utilities for reading and writing arbitrary Json data in Avro format. */
public class Json {
private Json() {
} // singleton: no public ctor
static final JsonFactory FACTORY = new JsonFactory();
static final ObjectMapper MAPPER = new ObjectMapper(FACTORY);
/** The schema for Json data. */
public static final Schema SCHEMA;
static {
try {
try (InputStream in = Json.class.getResourceAsStream("/org/apache/avro/data/Json.avsc")) {
SCHEMA = new Schema.Parser().parse(in);
}
} catch (IOException e) {
throw new AvroRuntimeException(e);
}
}
/**
* {@link DatumWriter} for arbitrary Json data using the object model described
* in {@link org.apache.avro.JsonProperties}.
*/
public static class ObjectWriter implements DatumWriter