use of com.fasterxml.jackson.databind.node.DecimalNode 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;
}
use of com.fasterxml.jackson.databind.node.DecimalNode 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;
}
use of com.fasterxml.jackson.databind.node.DecimalNode in project template-compiler by Squarespace.
the class DecimalFormatterTest method run.
private void run(CLDR.Locale locale, String number, String args, String expected) {
try {
String json = new DecimalNode(new BigDecimal(number)).toString();
String actual = format(locale, mk.args(args), json);
Assert.assertEquals(actual, expected);
} catch (CodeException e) {
fail("formatter raised an error", e);
}
}
use of com.fasterxml.jackson.databind.node.DecimalNode in project template-compiler by Squarespace.
the class CodeExecuteTest method testVariableTypes.
@Test
public void testVariableTypes() throws CodeException {
RootInst root = builder().var("@").eof().build();
String value = "12345678900000000.1234567890000000";
DecimalNode node = new DecimalNode(new BigDecimal(value));
assertContext(execute(node, root), value);
value = "123.0";
node = new DecimalNode(new BigDecimal(value));
assertContext(execute(node, root), value);
}
use of com.fasterxml.jackson.databind.node.DecimalNode in project template-compiler by Squarespace.
the class DecimalFormatterTest method run.
private void run(String locale, String number, String args, String expected) {
try {
String json = new DecimalNode(new BigDecimal(number)).toString();
String actual = format(locale, mk.args(args), json);
Assert.assertEquals(actual, expected);
} catch (CodeException e) {
fail("formatter raised an error", e);
}
}
Aggregations