Java Code Examples for org.yaml.snakeyaml.nodes.Node#setTag()

The following examples show how to use org.yaml.snakeyaml.nodes.Node#setTag() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigurationRepresenter.java    From ServerListPlus with GNU General Public License v3.0 6 votes vote down vote up
@Override // Skip null values for configuration generating
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object value, Tag customTag) {
    if (value != null) {
        NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, value, customTag);
        Node valueNode = tuple.getValueNode();

        // Avoid using tags for enums
        if (customTag == null && valueNode.getNodeId() == NodeId.scalar && value instanceof Enum<?>) {
            valueNode.setTag(Tag.STR);
        }

        return tuple;
    } else {
        return null;
    }
}
 
Example 2
Source File: YamlDeserializationData.java    From Diorite with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <K, T, M extends Map<K, T>> void getMap(String key, Function<String, K> keyMapper, Class<T> type, M map)
{
    Node node = this.getNode(this.node, key);
    if (! (node instanceof MappingNode))
    {
        throw new DeserializationException(type, this, "Can't find valid value for key: " + key);
    }
    MappingNode mappingNode = (MappingNode) node;
    Tag typeTag = new Tag(type);
    for (NodeTuple tuple : mappingNode.getValue())
    {
        Node keyNode = tuple.getKeyNode();
        keyNode.setTag(Tag.STR);
        K keyObj = keyMapper.apply(this.constructor.constructObject(keyNode).toString());

        Node valueNode = tuple.getValueNode();
        if (type != Object.class)
        {
            valueNode.setTag(typeTag);
        }

        map.put(keyObj, (T) this.constructor.constructObject(valueNode));
    }
}
 
Example 3
Source File: Representer.java    From onedev with MIT License 5 votes vote down vote up
private void resetTag(Class<? extends Object> type, Node node) {
    Tag tag = node.getTag();
    if (tag.matches(type)) {
        if (Enum.class.isAssignableFrom(type)) {
            node.setTag(Tag.STR);
        } else {
            node.setTag(Tag.MAP);
        }
    }
}
 
Example 4
Source File: Serialization.java    From Diorite with MIT License 5 votes vote down vote up
/**
 * Serialize a Java object into a YAML Node.
 *
 * @param data
 *         Java object to be Serialized to YAML
 *
 * @return YAML Node
 */
public Node toYamlNode(@Nullable Object data)
{
    Node represent = this.yaml().represent(data);
    if (data != null)
    {
        if (! (data instanceof Map) && ! (data instanceof Collection))
        {
            represent.setType(data.getClass());
            represent.setTag(new Tag(data.getClass()));
        }
    }
    return represent;
}
 
Example 5
Source File: Representer.java    From Diorite with MIT License 5 votes vote down vote up
private void resetTag(Class<?> type, Node node)
{
    Tag tag = node.getTag();
    if (tag.matches(type))
    {
        if (Enum.class.isAssignableFrom(type))
        {
            node.setTag(Tag.STR);
        }
        else
        {
            node.setTag(Tag.MAP);
        }
    }
}
 
Example 6
Source File: Serializer.java    From Diorite with MIT License 5 votes vote down vote up
public void serialize(Node node) throws IOException
    {
        if (this.closed == null)
        {
            throw new SerializerException("serializer is not opened");
        }
        if (this.closed)
        {
            throw new SerializerException("serializer is closed");
        }
//        if (this.explicitRoot != null)
//        {
//            if (this.explicitRoot.getValue().startsWith(Tag.PREFIX))
//            {
//                try
//                {
//                    Class<?> type = Class.forName(this.explicitRoot.getClassName(), false, Thread.currentThread().getContextClassLoader());
//                    node.setType(type);
//                }
//                catch (Exception ignored)
//                {
//                }
//            }
//        }
        this.emitter.emit(new DocumentStartEvent(null, null, this.explicitStart, this.useVersion, this.useTags));
        this.anchorNode(node);
        if (this.explicitRoot != null)
        {
            node.setTag(this.explicitRoot);
        }

        this.emitter.writeComment(this.comments.getHeader(), 0, 1);
        this.serializeNode(node, null, new LinkedList<>(), false);
        this.emitter.writeComment(this.comments.getFooter(), 2, 0);

        this.emitter.emit(new DocumentEndEvent(null, null, this.explicitEnd));
        this.serializedNodes.clear();
        this.anchors.clear();
    }
 
Example 7
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Construct and return the next document
 *
 * @return constructed instance
 */
public Object getData() {
    // Construct and return the next document.
    composer.checkNode();
    Node node = composer.getNode();
    if (rootTag != null) {
        node.setTag(rootTag);
    }
    return constructDocument(node);
}
 
Example 8
Source File: Representer.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
private void resetTag(Class<? extends Object> type, Node node) {
    Tag tag = node.getTag();
    if (tag.matches(type)) {
        if (Enum.class.isAssignableFrom(type)) {
            node.setTag(Tag.STR);
        } else {
            node.setTag(Tag.MAP);
        }
    }
}
 
Example 9
Source File: Representer.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (propertyValue instanceof Enum<?>) {
                    nodeValue.setTag(Tag.STR);
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 10
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Ensure that the stream contains a single document and construct it
 * 
 * @return constructed instance
 * @throws ComposerException
 *             in case there are more documents in the stream
 */
public Object getSingleData(Class<?> type) {
    // Ensure that the stream contains a single document and construct it
    Node node = composer.getSingleNode();
    if (node != null) {
        if (Object.class != type) {
            node.setTag(new Tag(type));
        } else if (rootTag != null) {
            node.setTag(rootTag);
        }
        return constructDocument(node);
    }
    return null;
}
 
Example 11
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Construct and return the next document
 * 
 * @return constructed instance
 */
public Object getData() {
    // Construct and return the next document.
    composer.checkNode();
    Node node = composer.getNode();
    if (rootTag != null) {
        node.setTag(rootTag);
    }
    return constructDocument(node);
}
 
Example 12
Source File: Representer.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private void resetTag(Class<? extends Object> type, Node node) {
    Tag tag = node.getTag();
    if (tag.matches(type)) {
        if (Enum.class.isAssignableFrom(type)) {
            node.setTag(Tag.STR);
        } else {
            node.setTag(Tag.MAP);
        }
    }
}
 
Example 13
Source File: Representer.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 * 
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (propertyValue instanceof Enum<?>) {
                    nodeValue.setTag(Tag.STR);
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 14
Source File: BaseConstructor.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Ensure that the stream contains a single document and construct it
 *
 * @param type the class of the instance being created
 * @return constructed instance
 * @throws ComposerException in case there are more documents in the stream
 */
public Object getSingleData(Class<?> type) {
    // Ensure that the stream contains a single document and construct it
    Node node = composer.getSingleNode();
    if (node != null && !Tag.NULL.equals(node.getTag())) {
        if (Object.class != type) {
            node.setTag(new Tag(type));
        } else if (rootTag != null) {
            node.setTag(rootTag);
        }
        return constructDocument(node);
    }
    return null;
}
 
Example 15
Source File: BaseConstructor.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Construct and return the next document
 *
 * @return constructed instance
 */
public Object getData() {
    // Construct and return the next document.
    composer.checkNode();
    Node node = composer.getNode();
    if (rootTag != null) {
        node.setTag(rootTag);
    }
    return constructDocument(node);
}
 
Example 16
Source File: Representer.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (property.getType() == propertyValue.getClass()) {
                    if (propertyValue instanceof Enum<?>) {
                        nodeValue.setTag(Tag.STR);
                    }
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 17
Source File: YamlDeserializationData.java    From Diorite with MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <K, T, M extends Map<K, T>> void getMap(String key, Class<K> keyType, Class<T> type, M map)
{
    Node node = this.getNode(this.node, key);
    if (! (node instanceof MappingNode))
    {
        throw new DeserializationException(type, this, "Can't find valid value for key: " + key);
    }

    MappingNode mappingNode = (MappingNode) node;
    Tag keyTag = new Tag(keyType);
    Tag typeTag = new Tag(type);
    for (NodeTuple tuple : mappingNode.getValue())
    {
        K keyObj;
        Node keyNode = tuple.getKeyNode();
        if (Serialization.isSimpleType(keyType))
        {
            if (keyType != Object.class)
            {
                keyNode.setTag(keyTag);
            }
            keyObj = (K) this.constructor.constructObject(keyNode);
        }
        else if (this.serialization.isStringSerializable(keyType))
        {
            keyNode.setTag(Tag.STR);
            keyObj = this.serialization.deserializeFromString(keyType, this.constructor.constructObject(keyNode).toString());
        }
        else
        {
            throw new DeserializationException(type, this, "Can't deserialize given node to key: (" + type.getName() + ") -> " + keyNode);
        }

        Node valueNode = tuple.getValueNode();
        if (type != Object.class)
        {
            valueNode.setTag(typeTag);
        }
        map.put(keyObj, (T) this.constructor.constructObject(valueNode));
    }
}
 
Example 18
Source File: Representer.java    From Diorite with MIT License 4 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *         - the instance to be represented
 * @param property
 *         - the property of the instance
 * @param propertyValue
 *         - value to be represented
 * @param customTag
 *         - user defined Tag
 *
 * @return NodeTuple to be used in a MappingNode. Return null to skip the property
 */
@Nullable
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, @Nullable Object propertyValue, @Nullable Tag customTag)
{
    ScalarNode nodeKey = (ScalarNode) this.representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = this.representData(propertyValue);

    if ((propertyValue != null) && ! hasAlias)
    {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null)
        {
            if (nodeId == NodeId.scalar)
            {
                if (propertyValue instanceof Enum<?>)
                {
                    nodeValue.setTag(Tag.STR);
                }
            }
            else
            {
                if (nodeId == NodeId.mapping)
                {
                    if (property.getType() == propertyValue.getClass())
                    {
                        if (! (propertyValue instanceof Map<?, ?>))
                        {
                            if (! nodeValue.getTag().equals(Tag.SET))
                            {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                this.checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 19
Source File: YamlDeserializationData.java    From Diorite with MIT License 4 votes vote down vote up
@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
private <T> T deserializeSpecial(Class<T> type, Node node, @Nullable T def)
{
    if (DioriteReflectionUtils.getWrapperClass(type).equals(Boolean.class))
    {
        node.setTag(Tag.STR);
        T t = (T) this.toBool(this.constructor.constructObject(node).toString());
        if (t == null)
        {
            return def;
        }
        return t;
    }
    if (Enum.class.isAssignableFrom(type))
    {
        node.setTag(Tag.STR);
        Enum valueSafe = DioriteReflectionUtils.getEnumValueSafe(this.constructor.constructObject(node).toString(), - 1, (Class) type);
        if (valueSafe == null)
        {
            return def;
        }
        return (T) valueSafe;
    }
    if (Number.class.isAssignableFrom(DioriteReflectionUtils.getWrapperClass(type)))
    {
        Class<?> numType = DioriteReflectionUtils.getWrapperClass(type);
        node.setTag(new Tag(String.class));
        String deserialize = this.constructor.constructObject(node).toString();
        if (numType == Byte.class)
        {
            return (T) (Byte) Byte.parseByte(deserialize);
        }
        else if (numType == Short.class)
        {
            return (T) (Short) Short.parseShort(deserialize);
        }
        else if (numType == Integer.class)
        {
            return (T) (Integer) Integer.parseInt(deserialize);
        }
        else if (numType == Long.class)
        {
            return (T) (Long) Long.parseLong(deserialize);
        }
        else if (numType == Float.class)
        {
            return (T) (Float) Float.parseFloat(deserialize);
        }
        else if (numType == Double.class)
        {
            return (T) (Double) Double.parseDouble(deserialize);
        }
    }
    if (type != Object.class)
    {
        node.setTag(new Tag(type));
    }
    return (T) this.constructor.constructObject(node);
}
 
Example 20
Source File: Yaml.java    From Diorite with MIT License 3 votes vote down vote up
/**
 * Construct object from given node.
 *
 * @param node
 *         node to load.
 * @param type
 *         type of node.
 * @param <T>
 *         type of node.
 *
 * @return loaded object.
 */
@SuppressWarnings("unchecked")
public <T> T fromYamlNode(Node node, Class<T> type)
{
    node.setTag(new Tag(type));
    node.setType(type);
    return (T) this.constructor.constructFromNode(node);
}