Refine search
/** * Evaluates the length of the given argument. * * @param evaluatedArgs List of expressions to be evaluated. * Can be a string, array or object * @return Length of the argument */ @Override public JsonNode evaluate(List<JsonNode> evaluatedArgs) throws InvalidTypeException { JsonNode arg = evaluatedArgs.get(0); if (arg.isTextual()) { return getStringLength(arg); } else if (arg.isArray() || arg.isObject()) { return new IntNode(arg.size()); } throw new InvalidTypeException("Type mismatch. Expecting a string or an array or an object."); }
/** * Alternate factory method that will handle wrapper value, which may * be null. * Due to possibility of null, returning type is not guaranteed to be * {@link NumericNode}, but just {@link ValueNode}. */ @Override public ValueNode numberNode(Byte value) { return (value == null) ? nullNode() : IntNode.valueOf(value.intValue()); }
public JsonNode length(final JsonNode in) throws JsonQueryException { if (in.isTextual()) { return IntNode.valueOf(UnicodeUtils.lengthUtf32(in.asText())); } else if (in.isArray() || in.isObject()) { return new IntNode(in.size()); } else if (in.isNull()) { return new IntNode(0); } else if (in.isNumber()) { return JsonNodeUtils.asNumericNode(Math.abs(in.asDouble())); } else { throw JsonQueryException.format("%s has no length", in.getNodeType()); } } }
private void applyRecursive(final Scope scope, final JsonNode in, final List<JsonNode> out, final Stack<JsonNode> stack, final JsonQuery predicate) throws JsonQueryException { if (!stack.isEmpty()) { for (final JsonNode shouldInclude : predicate.apply(scope, in)) if (JsonNodeUtils.asBoolean(shouldInclude)) out.add(JsonNodeUtils.asArrayNode(scope.getObjectMapper(), stack)); } if (in.isArray()) { for (int i = 0; i < in.size(); ++i) { stack.push(new IntNode(i)); applyRecursive(scope, in.get(i), out, stack, predicate); stack.pop(); } } else if (in.isObject()) { final Iterator<Entry<String, JsonNode>> iter = in.fields(); while (iter.hasNext()) { final Entry<String, JsonNode> i = iter.next(); stack.push(new TextNode(i.getKey())); applyRecursive(scope, i.getValue(), out, stack, predicate); stack.pop(); } } } }
@Override public JsonNode apply(final JsonNode tree) { if(tree == null) { return null; } final JsonNode temp = tree.get("data"); if(temp == null) { return null; } final Iterator<String> ids = temp.fieldNames(); while(ids.hasNext()) { final String runeId = ids.next(); final ObjectNode rune = (ObjectNode)temp.get(runeId); final String runeName = rune.get("name").asText(); if(id != null && id.intValue() == Integer.parseInt(runeId) || name != null && name.equals(runeName)) { rune.set("id", new IntNode(Integer.parseInt(runeId))); INCLUDED_DATA_PROCESSOR.apply(rune); return rune; } } return null; } }, content);
@Override public JsonNode apply(final JsonNode spellTree) { if(spellTree == null) { return spellTree; } // Swap key and id. They're reversed between ddragon and the API. if(spellTree.has("key") && spellTree.has("id")) { final ObjectNode spell = (ObjectNode)spellTree; final String id = spell.get("key").asText(); spell.set("key", spell.get("id")); spell.set("id", new IntNode(Integer.parseInt(id))); } final JsonNode temp = spellTree.get("vars"); if(temp == null) { return spellTree; } for(final JsonNode vars : temp) { if(vars == null) { continue; } final JsonNode coeff = vars.get("coeff"); if(coeff == null) { continue; } else if(!coeff.isArray()) { ((ObjectNode)vars).putArray("coeff").add(coeff.asDouble()); } } return spellTree; } };
final JsonNode pathIdNode = path.get("id"); final JsonNode slots = path.get("slots"); if(slots != null) { for(int i = 0; i < slots.size(); i++) { final JsonNode slot = slots.get(i); final JsonNode runes = slot.get("runes"); final ObjectNode node = (ObjectNode)rune; node.set("pathId", pathIdNode); node.set("slot", new IntNode(i)); return rune;
public JsonNode call(JsonNode input, JsonNode[] arguments) { if (arguments[0].isArray() || arguments[0].isObject()) return new IntNode(arguments[0].size()); else if (arguments[0].isTextual()) return new IntNode(arguments[0].asText().length()); else if (arguments[0].isNull()) return arguments[0]; else throw new JsltException("Function size() cannot work on " + arguments[0]); } }
@Override public JsonNode apply(final JsonNode tree) { if(tree == null) { return tree; } final JsonNode temp = tree.get("data"); if(temp == null) { return tree; } final Iterator<String> ids = temp.fieldNames(); while(ids.hasNext()) { final String runeId = ids.next(); final ObjectNode rune = (ObjectNode)temp.get(runeId); rune.set("id", new IntNode(Integer.parseInt(runeId))); INCLUDED_DATA_PROCESSOR.apply(rune); } return tree; } }, content);
@Override public JsonNode apply(final JsonNode championTree) { if(championTree == null) { return championTree; } // Swap key and id. They're reversed between ddragon and the API. if(championTree.has("key") && championTree.has("id")) { final ObjectNode champion = (ObjectNode)championTree; final String id = champion.get("key").asText(); champion.set("key", champion.get("id")); champion.set("id", new IntNode(Integer.parseInt(id))); } // Fix spell coeff field final JsonNode temp = championTree.get("spells"); if(temp == null) { return championTree; } for(final JsonNode spell : temp) { SPELL_PROCESSOR.apply(spell); } return championTree; } };
@Override public ResolvedFieldAccess resolveFieldAccess(final Scope scope, final JsonNode in) throws JsonQueryException { if (isRange) { final List<JsonNode> accessorBeginTuple = begin == null ? Collections.singletonList((JsonNode) new IntNode(0)) : begin.apply(scope, in); final List<JsonNode> accessorEndTuple = end == null ? Collections.singletonList((JsonNode) new IntNode(Integer.MAX_VALUE)) : end.apply(scope, in); final List<Range> ranges = new ArrayList<>(); for (final JsonNode accessorBegin : accessorBeginTuple) { for (final JsonNode accessorEnd : accessorEndTuple) { if (JsonNodeUtils.isIntegralNumber(accessorBegin) && JsonNodeUtils.isIntegralNumber(accessorEnd)) { final long indexBegin = accessorBegin.asLong(); final long indexEnd = accessorEnd.asLong(); ranges.add(new Range(indexBegin, indexEnd)); } else { if (!permissive) throw JsonQueryException.format("Start and end indices of an %s slice must be numbers", in.getNodeType()); return new ResolvedEmptyFieldAccess(permissive);
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { if (!in.isTextual()) throw JsonQueryException.format("%s (%s) only strings have UTF-8 byte length", in.getNodeType(), in); return Collections.<JsonNode> singletonList(IntNode.valueOf(UnicodeUtils.lengthUtf8(in.asText()))); } }
@Override public JsonNode apply(final JsonNode tree) { if(tree == null) { return tree; } for(final JsonNode path : tree) { final JsonNode pathIdNode = path.get("id"); final JsonNode slots = path.get("slots"); if(slots != null) { for(int i = 0; i < slots.size(); i++) { final JsonNode slot = slots.get(i); final JsonNode runes = slot.get("runes"); if(runes != null) { for(final JsonNode rune : runes) { final ObjectNode node = (ObjectNode)rune; node.set("pathId", pathIdNode); node.set("slot", new IntNode(i)); } } } } } return tree; } }, content);
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { Preconditions.checkInputType("keys", in, JsonNodeType.OBJECT, JsonNodeType.ARRAY); if (in.isObject()) { final List<String> keys = Lists.newArrayList(in.fieldNames()); Collections.sort(keys); final ArrayNode result = scope.getObjectMapper().createArrayNode(); for (final String key : keys) result.add(new TextNode(key)); return Collections.singletonList((JsonNode) result); } else if (in.isArray()) { final ArrayNode result = scope.getObjectMapper().createArrayNode(); for (int i = 0; i < in.size(); ++i) result.add(new IntNode(i)); return Collections.singletonList((JsonNode) result); } else { throw new IllegalStateException(); } } }
for (int i = 0; i < fieldNames.size(); i++) { current.put(fieldNames.get(i), fields.get(i).sample()); changes.put(fieldNames.get(i), IntNode.valueOf(0)); Iterator<String> fx = newValue.fieldNames(); while (fx.hasNext()) { String fieldName = fx.next(); current.put(fieldName, newValue.get(fieldName)); changes.put(fieldName, new IntNode(1)); changes.put(fieldNames.get(change), new IntNode(1)); Iterator<String> fx = newValue.fieldNames(); while (fx.hasNext()) { String fieldName = fx.next(); current.put(prefix + fieldName, new IntNode(0)); changes.put(fieldNames.get(change), IntNode.valueOf(0));
@Override public List<JsonNode> apply(final Scope scope, final List<JsonQuery> args, final JsonNode in) throws JsonQueryException { Preconditions.checkInputType("to_entries", in, JsonNodeType.OBJECT, JsonNodeType.ARRAY); final ArrayNode out = scope.getObjectMapper().createArrayNode(); if (in.isObject()) { final Iterator<Entry<String, JsonNode>> iter = in.fields(); while (iter.hasNext()) { final Entry<String, JsonNode> entry = iter.next(); final ObjectNode entryNode = scope.getObjectMapper().createObjectNode(); entryNode.set("key", new TextNode(entry.getKey())); entryNode.set("value", entry.getValue()); out.add(entryNode); } } else if (in.isArray()) { final Iterator<JsonNode> iter = in.elements(); int i = 0; while (iter.hasNext()) { final JsonNode entry = iter.next(); final ObjectNode entryNode = scope.getObjectMapper().createObjectNode(); entryNode.set("key", IntNode.valueOf(i++)); entryNode.set("value", entry); out.add(entryNode); } } else { throw new IllegalStateException(); } return Collections.singletonList((JsonNode) out); } }
public ArrayNode scan(String table, ScanBuilder scanBuilder) throws IOException { ObjectNode body = createRequest(table, "scan", null); if (scanBuilder.projectedColumns != null) { body.set("projectedColumns", scanBuilder.projectedColumns); } if (scanBuilder.predicates != null) { body.set("predicates", scanBuilder.predicates); } if (scanBuilder.lowerBound != null) { body.set("start", scanBuilder.lowerBound); } if (scanBuilder.exclusiveUpperBound != null) { body.set("end", scanBuilder.exclusiveUpperBound); } body.set("limit", IntNode.valueOf(scanBuilder.limit)); body.set("ordered", BooleanNode.valueOf(scanBuilder.ordered)); JsonNode result = doPOST(body); // { // "error":0,"message":"OK", // "result":{"columnNames":["id","name"],"columnTypes":["INT","STRING"],"data":[[1,"a"]]} // } return (ArrayNode) ((ArrayNode) result.get("result").get("data")); }
@Test public void testToJsonNode() { assertEquals(null, toJsonNode(null)); assertEquals(NullNode.getInstance(), toJsonNode(JsonProperties.NULL_VALUE)); assertEquals(BooleanNode.TRUE, toJsonNode(true)); assertEquals(IntNode.valueOf(1), toJsonNode(1)); assertEquals(LongNode.valueOf(2), toJsonNode(2L)); assertEquals(FloatNode.valueOf(1.0f), toJsonNode(1.0f)); assertEquals(DoubleNode.valueOf(2.0), toJsonNode(2.0)); assertEquals(TextNode.valueOf("\u0001\u0002"), toJsonNode(new byte[] { 1, 2 })); assertEquals(TextNode.valueOf("a"), toJsonNode("a")); assertEquals(TextNode.valueOf("UP"), toJsonNode(Direction.UP)); ArrayNode an = JsonNodeFactory.instance.arrayNode(); an.add(1); assertEquals(an, toJsonNode(Collections.singletonList(1))); ObjectNode on = JsonNodeFactory.instance.objectNode(); on.put("a", 1); assertEquals(on, toJsonNode(Collections.singletonMap("a", 1))); }
String data = newargs[2]; JsonNode jsondata = mapper.readTree(data); ArrayNode datas = JsonNodeFactory.instance.arrayNode(); for (int i = 0; i < num; i++) { JsonNode copy = jsondata.deepCopy(); if (copy instanceof ArrayNode) { ArrayNode d = (ArrayNode)copy; d.set(0, IntNode.valueOf(i)); datas.add(d); } else if (copy instanceof ObjectNode) { ObjectNode d = (ObjectNode)copy; d.set("_partition", IntNode.valueOf(i)); datas.add(d); } else { ArrayNode datas = JsonNodeFactory.instance.arrayNode(); for (int i = 2; i < newargs.length; i++) { String data = newargs[i];