Search in sources :

Example 1 with ShortNode

use of com.fasterxml.jackson.databind.node.ShortNode in project symja_android_library by axkr.

the class ExpressionJSONConvert method importExpressionJSONRecursive.

public static IExpr importExpressionJSONRecursive(JsonNode node) {
    if (node instanceof ArrayNode) {
        ArrayNode arrayNode = (ArrayNode) node;
        Iterator<JsonNode> iter = arrayNode.elements();
        IASTAppendable ast;
        if (iter.hasNext()) {
            JsonNode next = iter.next();
            IExpr temp = importExpressionJSONRecursive(next);
            if (temp.isPresent()) {
                ast = F.ast(temp, arrayNode.size() - 1);
                while (iter.hasNext()) {
                    next = iter.next();
                    temp = importExpressionJSONRecursive(next);
                    if (temp.isPresent()) {
                        ast.append(temp);
                    }
                }
                return ast;
            }
        }
        return F.NIL;
    } else if (node instanceof ObjectNode) {
        IASTAppendable list = F.ListAlloc();
        ObjectNode objectNode = (ObjectNode) node;
        Iterator<Entry<String, JsonNode>> iter = objectNode.fields();
        while (iter.hasNext()) {
            Entry<String, JsonNode> next = iter.next();
            IExpr temp = importExpressionJSONRecursive(next.getValue());
            if (temp.isPresent()) {
                list.append(F.Rule(F.$str(next.getKey()), temp));
            }
        }
        return list;
    } else if (node instanceof ValueNode) {
        ValueNode valueNode = (ValueNode) node;
        if (valueNode instanceof NumericNode) {
            if (valueNode instanceof DoubleNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof FloatNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof IntNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof LongNode) {
                return F.ZZ(valueNode.longValue());
            } else if (valueNode instanceof ShortNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof BigIntegerNode) {
                return F.ZZ(valueNode.bigIntegerValue());
            } else if (valueNode instanceof DecimalNode) {
                return F.num(new Apfloat(valueNode.decimalValue()));
            }
        }
        if (valueNode instanceof BooleanNode) {
            return valueNode.booleanValue() ? S.True : S.False;
        } else if (valueNode instanceof NullNode) {
            return S.Null;
        } else if (valueNode instanceof TextNode) {
            String symbolName = valueNode.textValue();
            if (symbolName.length() > 1 && symbolName.charAt(0) == '\'' && symbolName.charAt(symbolName.length() - 1) == '\'') {
                return F.$str(symbolName.substring(1, symbolName.length() - 1));
            }
            if (Scanner.isIdentifier(symbolName)) {
                return F.symbol(symbolName);
            }
            return F.$str(symbolName);
        }
        return F.$str(valueNode.toString());
    }
    return F.NIL;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FloatNode(com.fasterxml.jackson.databind.node.FloatNode) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigIntegerNode(com.fasterxml.jackson.databind.node.BigIntegerNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) NumericNode(com.fasterxml.jackson.databind.node.NumericNode) LongNode(com.fasterxml.jackson.databind.node.LongNode) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) Apfloat(org.apfloat.Apfloat) ShortNode(com.fasterxml.jackson.databind.node.ShortNode) Entry(java.util.Map.Entry) IntNode(com.fasterxml.jackson.databind.node.IntNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Iterator(java.util.Iterator) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IExpr(org.matheclipse.core.interfaces.IExpr) DecimalNode(com.fasterxml.jackson.databind.node.DecimalNode) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Example 2 with ShortNode

use of com.fasterxml.jackson.databind.node.ShortNode in project symja_android_library by axkr.

the class JSONConvert method importJSONRecursive.

public static IExpr importJSONRecursive(JsonNode node) {
    if (node instanceof ArrayNode) {
        ArrayNode arrayNode = (ArrayNode) node;
        Iterator<JsonNode> iter = arrayNode.elements();
        IASTAppendable list = F.ListAlloc(arrayNode.size());
        while (iter.hasNext()) {
            JsonNode next = iter.next();
            IExpr temp = importJSONRecursive(next);
            if (temp.isPresent()) {
                list.append(temp);
            }
        }
        return list;
    } else if (node instanceof ObjectNode) {
        IASTAppendable list = F.ListAlloc();
        ObjectNode objectNode = (ObjectNode) node;
        Iterator<Entry<String, JsonNode>> iter = objectNode.fields();
        while (iter.hasNext()) {
            Entry<String, JsonNode> next = iter.next();
            IExpr temp = importJSONRecursive(next.getValue());
            if (temp.isPresent()) {
                list.append(F.Rule(F.$str(next.getKey()), temp));
            }
        }
        return list;
    } else if (node instanceof ValueNode) {
        ValueNode valueNode = (ValueNode) node;
        if (valueNode instanceof NumericNode) {
            if (valueNode instanceof DoubleNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof FloatNode) {
                return F.num(valueNode.doubleValue());
            } else if (valueNode instanceof IntNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof LongNode) {
                return F.ZZ(valueNode.longValue());
            } else if (valueNode instanceof ShortNode) {
                return F.ZZ(valueNode.intValue());
            } else if (valueNode instanceof BigIntegerNode) {
                return F.ZZ(valueNode.bigIntegerValue());
            } else if (valueNode instanceof DecimalNode) {
                return F.num(new Apfloat(valueNode.decimalValue()));
            }
        }
        if (valueNode instanceof BooleanNode) {
            return valueNode.booleanValue() ? S.True : S.False;
        } else if (valueNode instanceof NullNode) {
            return S.Null;
        } else if (valueNode instanceof TextNode) {
            return F.$str(valueNode.textValue());
        }
        return F.$str(valueNode.toString());
    }
    return F.NIL;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FloatNode(com.fasterxml.jackson.databind.node.FloatNode) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) BigIntegerNode(com.fasterxml.jackson.databind.node.BigIntegerNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) NumericNode(com.fasterxml.jackson.databind.node.NumericNode) LongNode(com.fasterxml.jackson.databind.node.LongNode) BooleanNode(com.fasterxml.jackson.databind.node.BooleanNode) Apfloat(org.apfloat.Apfloat) ShortNode(com.fasterxml.jackson.databind.node.ShortNode) Entry(java.util.Map.Entry) IntNode(com.fasterxml.jackson.databind.node.IntNode) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) Iterator(java.util.Iterator) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IExpr(org.matheclipse.core.interfaces.IExpr) DecimalNode(com.fasterxml.jackson.databind.node.DecimalNode) NullNode(com.fasterxml.jackson.databind.node.NullNode)

Example 3 with ShortNode

use of com.fasterxml.jackson.databind.node.ShortNode in project kafka by apache.

the class FileBasedStateStore method writeElectionStateToFile.

private void writeElectionStateToFile(final File stateFile, QuorumStateData state) {
    final File temp = new File(stateFile.getAbsolutePath() + ".tmp");
    deleteFileIfExists(temp);
    log.trace("Writing tmp quorum state {}", temp.getAbsolutePath());
    try (final FileOutputStream fileOutputStream = new FileOutputStream(temp);
        final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8))) {
        short version = state.highestSupportedVersion();
        ObjectNode jsonState = (ObjectNode) QuorumStateDataJsonConverter.write(state, version);
        jsonState.set(DATA_VERSION, new ShortNode(version));
        writer.write(jsonState.toString());
        writer.flush();
        fileOutputStream.getFD().sync();
        Utils.atomicMoveWithFallback(temp.toPath(), stateFile.toPath());
    } catch (IOException e) {
        throw new UncheckedIOException(String.format("Error while writing the Quorum status from the file %s", stateFile.getAbsolutePath()), e);
    } finally {
        // cleanup the temp file when the write finishes (either success or fail).
        deleteFileIfExists(temp);
    }
}
Also used : ShortNode(com.fasterxml.jackson.databind.node.ShortNode) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ShortNode (com.fasterxml.jackson.databind.node.ShortNode)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 BigIntegerNode (com.fasterxml.jackson.databind.node.BigIntegerNode)2 BooleanNode (com.fasterxml.jackson.databind.node.BooleanNode)2 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)2 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)2 FloatNode (com.fasterxml.jackson.databind.node.FloatNode)2 IntNode (com.fasterxml.jackson.databind.node.IntNode)2 LongNode (com.fasterxml.jackson.databind.node.LongNode)2 NullNode (com.fasterxml.jackson.databind.node.NullNode)2 NumericNode (com.fasterxml.jackson.databind.node.NumericNode)2 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2 Apfloat (org.apfloat.Apfloat)2 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)2 IExpr (org.matheclipse.core.interfaces.IExpr)2