Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright The Microcks Authors.
*
* 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 io.github.microcks.util;
import org.apache.avro.AvroTypeException;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.io.JsonDecoder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Helper class using utility methods for converting Avro format from and to JSON.
* @author laurent
*/
public class AvroUtil {
private AvroUtil() {
// Private constructor to hide implicit public one.
}
/**
* Convert a JSON string into an Avro binary representation using specified schema.
* @param json A JSON string to convert to Avro
* @param avroSchema String representation of an Avro Schema to use for conversion
* @return The Avro binary representation of JSON
* @throws AvroTypeException if there's a mismatch between JSON string and Avro Schema
* @throws IOException if something goes wrong during conversion
*/
public static byte[] jsonToAvro(String json, String avroSchema) throws AvroTypeException, IOException {
return jsonToAvro(json, new Schema.Parser().parse(avroSchema));
}
/**
* Convert a JSON string into an Avro binary representation using specified schema.
* @param json A JSON string to convert to Avro
* @param avroSchema The Avro Schema to use for conversion
* @return The Avro binary representation of JSON
* @throws AvroTypeException if there's a mismatch between JSON string and Avro Schema
* @throws IOException if something goes wrong during conversion
*/
public static byte[] jsonToAvro(String json, Schema avroSchema) throws AvroTypeException, IOException {
// Prepare reader an input stream from Json string.
GenericDatumReader reader = new GenericDatumReader<>(avroSchema);
InputStream input = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(avroSchema, input);
// Prepare write and output stream to produce binary encoding.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GenericDatumWriter