コード例 #1
0
  /**
   * Retrieve all messages as a JSON object
   *
   * <p>The retrieved JSON document is an object where:
   *
   * <ul>
   *   <li>keys are string representations of {@link JsonPointer}s,
   *   <li>values are arrays of objects where each individual object is the JSON representation of
   *       one message.
   * </ul>
   *
   * <p>Note: the returned {@link JsonNode} is mutable.
   *
   * @see Message#toJsonNode()
   * @return a JSON object with all validation messages
   */
  public JsonNode asJsonObject() {
    final ObjectNode ret = JsonNodeFactory.instance.objectNode();

    ArrayNode node;
    List<Message> messages;

    for (final JsonPointer ptr : msgMap.keySet()) {
      node = JsonNodeFactory.instance.arrayNode();
      messages = MESSAGE_ORDER.sortedCopy(msgMap.get(ptr));
      for (final Message message : messages) node.add(message.toJsonNode());
      ret.put(ptr.toString(), node);
    }

    return ret;
  }
コード例 #2
0
  /**
   * Return the list of validation messages as a JSON array
   *
   * <p>This method makes its best to order validation messages correctly.
   *
   * <p>Each message in the resulting array is a JSON object, with the contents of the {@link
   * Message} and with an added member named {@code path}, which contains the path into the instance
   * where the error has occurred (as a {@link JsonPointer}).
   *
   * @see Message#toJsonNode()
   * @return a JSON array with all validation messages
   */
  public JsonNode asJsonArray() {
    final ArrayNode ret = JsonNodeFactory.instance.arrayNode();
    ObjectNode node;

    final Iterable<JsonPointer> paths = Ordering.natural().sortedCopy(msgMap.keySet());

    List<Message> messages;

    for (final JsonPointer ptr : paths) {
      messages = MESSAGE_ORDER.sortedCopy(msgMap.get(ptr));
      for (final Message msg : messages) {
        node = JsonNodeFactory.instance.objectNode().put("path", ptr.toString());
        // I hate to do that...
        node.putAll((ObjectNode) msg.toJsonNode());
        ret.add(node);
      }
    }

    return ret;
  }