org.apache.avro.JsonProperties 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.avro;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.io.IOException;
import org.apache.avro.util.internal.JacksonUtils;
import com.facebook.presto.hive.$internal.org.codehaus.jackson.JsonNode;
import com.facebook.presto.hive.$internal.org.codehaus.jackson.JsonGenerator;
import com.facebook.presto.hive.$internal.org.codehaus.jackson.node.TextNode;
/**
* Base class for objects that have JSON-valued properties. Avro and JSON values are
* represented in Java using the following mapping:
*
*
*
* Avro type
* JSON type
* Java type
*
*
* null
* null
* {@link #NULL_VALUE}
*
*
* boolean
* Boolean
* boolean
*
*
* int
* Number
* int
*
*
* long
* Number
* long
*
*
* float
* Number
* float
*
*
* double
* Number
* double
*
*
* bytes
* String
* byte[]
*
*
* string
* String
* {@link java.lang.String}
*
*
* record
* Object
* {@link java.util.Map}
*
*
* enum
* String
* {@link java.lang.String}
*
*
* array
* Array
* {@link java.util.Collection}
*
*
* map
* Object
* {@link java.util.Map}
*
*
* fixed
* String
* byte[]
*
*
*
* @see org.apache.avro.data.Json
*/
public abstract class JsonProperties {
public static class Null {
private Null() {}
}
/** A value representing a JSON null
. */
public static final Null NULL_VALUE = new Null();
Map props = new LinkedHashMap(1);
private Set reserved;
JsonProperties(Set reserved) {
this.reserved = reserved;
}
/**
* Returns the value of the named, string-valued property in this schema.
* Returns null if there is no string-valued property with that name.
*/
public String getProp(String name) {
JsonNode value = getJsonProp(name);
return value != null && value.isTextual() ? value.getTextValue() : null;
}
/**
* Returns the value of the named property in this schema.
* Returns null if there is no property with that name.
* @deprecated use {@link #getObjectProp(String)}
*/
@Deprecated
public synchronized JsonNode getJsonProp(String name) {
return props.get(name);
}
/**
* Returns the value of the named property in this schema.
* Returns null if there is no property with that name.
*/
public synchronized Object getObjectProp(String name) {
return JacksonUtils.toObject(props.get(name));
}
/**
* Adds a property with the given name name and
* value value. Neither name nor value can be
* null. It is illegal to add a property if another with
* the same name but different value already exists in this schema.
*
* @param name The name of the property to add
* @param value The value for the property to add
*/
public void addProp(String name, String value) {
addProp(name, TextNode.valueOf(value));
}
/**
* Adds a property with the given name name and
* value value. Neither name nor value can be
* null. It is illegal to add a property if another with
* the same name but different value already exists in this schema.
*
* @param name The name of the property to add
* @param value The value for the property to add
* @deprecated use {@link #addProp(String, Object)}
*/
@Deprecated
public synchronized void addProp(String name, JsonNode value) {
if (reserved.contains(name))
throw new AvroRuntimeException("Can't set reserved property: " + name);
if (value == null)
throw new AvroRuntimeException("Can't set a property to null: " + name);
JsonNode old = props.get(name);
if (old == null)
props.put(name, value);
else if (!old.equals(value))
throw new AvroRuntimeException("Can't overwrite property: " + name);
}
public synchronized void addProp(String name, Object value) {
addProp(name, JacksonUtils.toJsonNode(value));
}
/** Return the defined properties that have string values. */
@Deprecated public Map getProps() {
Map result = new LinkedHashMap();
for (Map.Entry e : props.entrySet())
if (e.getValue().isTextual())
result.put(e.getKey(), e.getValue().getTextValue());
return result;
}
/** Convert a map of string-valued properties to Json properties. */
Map jsonProps(Map stringProps) {
Map result = new LinkedHashMap();
for (Map.Entry e : stringProps.entrySet())
result.put(e.getKey(), TextNode.valueOf(e.getValue()));
return result;
}
/**
* Return the defined properties as an unmodifieable Map.
* @deprecated use {@link #getObjectProps()}
*/
@Deprecated
public Map getJsonProps() {
return Collections.unmodifiableMap(props);
}
/** Return the defined properties as an unmodifieable Map. */
public Map getObjectProps() {
Map result = new LinkedHashMap();
for (Map.Entry e : props.entrySet())
result.put(e.getKey(), JacksonUtils.toObject(e.getValue()));
return result;
}
void writeProps(JsonGenerator gen) throws IOException {
for (Map.Entry e : props.entrySet())
gen.writeObjectField(e.getKey(), e.getValue());
}
}