Java Code Examples for com.fasterxml.jackson.databind.node.NullNode#instance()

The following examples show how to use com.fasterxml.jackson.databind.node.NullNode#instance() . 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: FunctionExpression.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode apply(Scope scope, JsonNode input) {
  JsonNode[] params = new JsonNode[arguments.length];
  for (int ix = 0; ix < params.length; ix++)
    params[ix] = arguments[ix].apply(scope, input);

  if (declared != null)
    return declared.call(scope, input, params);
  else {
    JsonNode value = function.call(input, params);

    // if the user-implemented function returns Java null, silently
    // turn it into a JSON null. (the alternative is to throw an
    // exception.)
    if (value == null)
      value = NullNode.instance;

    return value;
  }
}
 
Example 2
Source File: ForExpression.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode apply(Scope scope, JsonNode input) {
  JsonNode array = valueExpr.apply(scope, input);
  if (array.isNull())
    return NullNode.instance;
  else if (array.isObject())
    array = NodeUtils.convertObjectToArray(array);
  else if (!array.isArray())
    throw new JsltException("For loop can't iterate over " + array, location);

  ArrayNode result = NodeUtils.mapper.createArrayNode();
  for (int ix = 0; ix < array.size(); ix++) {
    JsonNode value = array.get(ix);

    // must evaluate lets over again for each value because of context
    if (lets.length > 0)
      NodeUtils.evalLets(scope, value, lets);

    if (ifExpr == null || NodeUtils.isTrue(ifExpr.apply(scope, value)))
      result.add(loopExpr.apply(scope, value));
  }
  return result;
}
 
Example 3
Source File: ExpressionImpl.java    From jslt with Apache License 2.0 6 votes vote down vote up
public JsonNode apply(Scope scope, JsonNode input) {
  // Jackson 2.9.2 can parse to Java null. See unit test
  // QueryTest.testNullInput. so we have to handle that
  if (input == null)
    input = NullNode.instance;

  // evaluate lets in global modules
  if (fileModules != null) {
    for (int ix = 0; ix < fileModules.length; ix++)
      fileModules[ix].evaluateLetsOnly(scope, input);
  }

  // evaluate own lets
  NodeUtils.evalLets(scope, input, lets);

  return actual.apply(scope, input);
}
 
Example 4
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  String string = NodeUtils.toString(arguments[0], true);
  if (string == null)
    return NullNode.instance;

  return new TextNode(string.trim());
}
 
Example 5
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else if ((Boolean) node)
    return BooleanNode.TRUE;
  else
    return BooleanNode.FALSE;
}
 
Example 6
Source File: IfExpression.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode apply(Scope scope, JsonNode input) {
  if (NodeUtils.isTrue(test.apply(scope, input))) {
    NodeUtils.evalLets(scope, input, thenlets);
    return then.apply(scope, input);
  }

  // test was false, so return null or else
  if (orelse != null) {
    NodeUtils.evalLets(scope, input, elselets);
    return orelse.apply(scope, input);
  } else
    return NullNode.instance;
}
 
Example 7
Source File: DotExpression.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode apply(Scope scope, JsonNode input) {
  // if there is no key we just return the input
  if (key == null)
    return input;

  // if we have a parent, get the input from the parent (preceding expr)
  if (parent != null)
    input = parent.apply(scope, input);

  // okay, do the keying
  JsonNode value = input.get(key);
  if (value == null)
    value = NullNode.instance;
  return value;
}
 
Example 8
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  if (arguments[0].isNull())
    return NullNode.instance;

  String urlString = arguments[0].asText();

  try {
    URL aURL = new URL(arguments[0].asText());
    final ObjectNode objectNode = NodeUtils.mapper.createObjectNode();
    if (aURL.getHost() != null && !aURL.getHost().isEmpty())
      objectNode.put("host", aURL.getHost());
    if (aURL.getPort() != -1)
      objectNode.put("port", aURL.getPort());
    if (!aURL.getPath().isEmpty())
      objectNode.put("path", aURL.getPath());
    if (aURL.getProtocol() != null && !aURL.getProtocol().isEmpty())
      objectNode.put("scheme", aURL.getProtocol());
    if (aURL.getQuery() != null && !aURL.getQuery().isEmpty()) {
      objectNode.put("query", aURL.getQuery());
      final ObjectNode queryParamsNode = NodeUtils.mapper.createObjectNode();
      objectNode.set("parameters", queryParamsNode);
      final String[] pairs = aURL.getQuery().split("&");
      for (String pair : pairs) {
        final int idx = pair.indexOf("=");
        final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
        if (!queryParamsNode.has(key)) queryParamsNode.set(key, NodeUtils.mapper.createArrayNode());
        final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8") : null;
        final ArrayNode valuesNode = (ArrayNode) queryParamsNode.get(key);
        valuesNode.add(value);
      }
    }
    if(aURL.getRef() != null)
      objectNode.put("fragment", aURL.getRef());
    if(aURL.getUserInfo() != null && !aURL.getUserInfo().isEmpty())
      objectNode.put("userinfo", aURL.getUserInfo());
    return objectNode;
  } catch (MalformedURLException | UnsupportedEncodingException e) {
    throw new JsltException("Can't parse " + urlString, e);
  }
}
 
Example 9
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  if (arguments[0].isNull() || arguments[1].isNull())
    return NullNode.instance;
  else if (ComparisonOperator.compare(arguments[0], arguments[1], null) > 0)
    return arguments[0];
  else
    return arguments[1];
}
 
Example 10
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode number = NodeUtils.number(arguments[0], null);
  if (number == null || number.isNull())
    return NullNode.instance;

  double timestamp = number.asDouble();

  String formatstr = NodeUtils.toString(arguments[1], false);

  TimeZone zone = new SimpleTimeZone(0, "UTC");
  if (arguments.length == 3) {
    String zonename = NodeUtils.toString(arguments[2], false);
    if (!zonenames.contains(zonename))
      throw new JsltException("format-time: Unknown timezone " + zonename);
    zone = TimeZone.getTimeZone(zonename);
  }

  // the performance of this could be better, but it's not so easy
  // to fix that when SimpleDateFormat isn't thread-safe, so we
  // can't safely share it between threads

  try {
    SimpleDateFormat format = new SimpleDateFormat(formatstr);
    format.setTimeZone(zone);
    String formatted = format.format(Math.round(timestamp * 1000));
    return new TextNode(formatted);
  } catch (IllegalArgumentException e) {
    // thrown if format is bad
    throw new JsltException("format-time: Couldn't parse format '" + formatstr + "': " + e.getMessage());
  }
}
 
Example 11
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  ArrayNode array = NodeUtils.toArray(arguments[0], true);
  if (array == null)
    return NullNode.instance;

  String sep = NodeUtils.toString(arguments[1], false);

  StringBuilder buf = new StringBuilder();
  for (int ix = 0; ix < array.size(); ix++) {
    if (ix > 0)
      buf.append(sep);
    buf.append(NodeUtils.toString(array.get(ix), false));
  }
  return new TextNode(buf.toString());
}
 
Example 12
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode number = arguments[0];
  if (number.isNull())
    return NullNode.instance;
  else if (!number.isNumber())
    throw new JsltException("round() cannot round a non-number: " + number);

  return new LongNode(Math.round(number.doubleValue()));
}
 
Example 13
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  String string = NodeUtils.toString(arguments[0], true);
  if (string == null)
    return NullNode.instance;

  String regexp = NodeUtils.toString(arguments[1], false);
  String sep = NodeUtils.toString(arguments[2], false);

  Pattern p = getRegexp(regexp);
  Matcher m = p.matcher(string);
  char[] buf = new char[string.length() * Math.max(sep.length(), 1)];
  int pos = 0; // next untouched character in input
  int bufix = 0; // next unwritten character in buf

  while (m.find(pos)) {
    // we found another match, and now matcher state has been updated
    if (m.start() == m.end())
      throw new JsltException("Regexp " + regexp + " in replace() matched empty string in '" + arguments[0] + "'");

    // if there was text between pos and start of match, copy to output
    if (pos < m.start())
      bufix = copy(string, buf, bufix, pos, m.start());

    // copy sep to output (corresponds with the match)
    bufix = copy(sep, buf, bufix, 0, sep.length());

    // step over match
    pos = m.end();
  }

  if (pos == 0 && arguments[0].isTextual())
    // there were matches, so the string hasn't changed
    return arguments[0];
  else if (pos < string.length())
    // there was text remaining after the end of the last match. must copy
    bufix = copy(string, buf, bufix, pos, string.length());

  return new TextNode(new String(buf, 0, bufix));
}
 
Example 14
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode node = arguments[0];
  if (node.isNull())
    return NullNode.instance;
  try {
    // https://stackoverflow.com/a/18993481/90580
    final Object obj = mapper.treeToValue(node, Object.class);
    String jsonString = writer.writeValueAsString(obj);
    return new IntNode(jsonString.hashCode());
  } catch (JsonProcessingException e) {
    throw new JsltException("hash-int: can't process json" + e);
  }
}
 
Example 15
Source File: BuiltinFunctions.java    From jslt with Apache License 2.0 5 votes vote down vote up
public JsonNode call(JsonNode input, JsonNode[] arguments) {
  JsonNode number = arguments[0];
  if (number.isNull())
    return NullNode.instance;
  else if (!number.isNumber())
    throw new JsltException("ceiling() cannot round a non-number: " + number);

  return new LongNode((long) Math.ceil(number.doubleValue()));
}
 
Example 16
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 4 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else
    return new TextNode((String) node);
}
 
Example 17
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 4 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else
    return new FloatNode((Float) node);
}
 
Example 18
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 4 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else
    return new DoubleNode((Double) node);
}
 
Example 19
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 4 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else
    return new IntNode((Integer) node);
}
 
Example 20
Source File: FunctionWrapper.java    From jslt with Apache License 2.0 4 votes vote down vote up
public JsonNode convert(Object node) {
  if (node == null)
    return NullNode.instance;
  else
    return new LongNode((Long) node);
}