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.
package com.vladkrava.converter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Optional;
import org.apache.avro.Schema;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* The {@code AvroSchemaProcessor} class is used for defining a type of {@link JSONObject}
* values based on Avro Schema ({@link Schema} class).
*
* @author Vlad Krava - [email protected]
* @see Integer
* @see Boolean
* @see Long
* @see Double
* @see Float
* @see String
* @see Schema
* @see JSONObject
* @since 0.2-SNAPSHOT
*/
public final class AvroSchemaProcessor {
/**
* Utility class constructor
*/
private AvroSchemaProcessor() {
}
/**
* Processing an Avro Schema ({@link Schema} class) with {@link JSONObject} to define a value and a type of JSON node.
*
* @param jsonNode a node which a processing/traversing will occur
* @param objectSchema referenced Avro Schema object
* @param objectKey a key of JSON node
* @return {@code Object} processed JSON node value wrapped into corresponding to Avro Schema type
*/
public static Object processValue(final JSONObject jsonNode, final Schema objectSchema, final String objectKey) {
switch (objectSchema.getType()) {
case INT:
return Integer.valueOf(String.valueOf(jsonNode.get(objectKey)));
case LONG:
return Long.valueOf(String.valueOf(jsonNode.get(objectKey)));
case FLOAT:
return Float.valueOf(String.valueOf(jsonNode.get(objectKey)));
case DOUBLE:
return Double.valueOf(String.valueOf(jsonNode.get(objectKey)));
case BOOLEAN:
return Boolean.valueOf(String.valueOf(jsonNode.get(objectKey)));
case STRING:
case BYTES:
case FIXED:
return String.valueOf(jsonNode.get(objectKey));
case ARRAY:
return processArrayValue(jsonNode, objectSchema.getElementType(), objectKey);
case RECORD:
return processRecordValue(jsonNode, objectSchema, objectKey);
case ENUM:
return objectSchema.getEnumSymbols().get(objectSchema.getEnumOrdinal(String.valueOf(jsonNode.get(objectKey))));
case UNION:
return processUnionValue(jsonNode, objectSchema, objectKey);
case MAP:
return jsonNode.get(objectKey);
default:
return JSONObject.NULL;
}
}
/**
* Processing complex Avro Schema ({@code Schema.Type.ARRAY}) with {@link JSONObject} to define a value and for
* corresponding {@link JSONArray} data structure.
*
* @param jsonNode a node which stores values in {@link JSONArray} data structure
* @param objectSchema referenced Avro Schema object
* @param objectKey a key of JSON node by which traversing/searching will occur
* @return {@code Collection