Hacker News new | ask | show | jobs
by peterloveslucy 3563 days ago
40 lines is excessive, I prefer simple methods that take care of the if's and link them together serially. AFAICS this is the way the brain works.

  /**
   * get the leaf node as a string
   * @param obj the json object to operate on
   * @param path the dot path to use
   * @return the leaf node if present <b>and textual</b>, otherwise null
   */
  public static String leafString(ObjectNode obj, String path) {
    JsonNode leaf = leaf(obj, path);
    return leaf != null && leaf.isTextual()
        ? leaf.asText()
        : null;
  }