class JsonRequestBodyParser implements RequestBodyParser {

  @Override
  public Set<RequestBody> parse(String body) throws BodyParseException {

    Set<RequestBody> requestBodySet = new HashSet<>();
    RequestBody      rootBody       = new RequestBody();
    rootBody.setBody(body);

    if (body != null && body.length() != 0) {
      try {
        JsonNode root = mapper.readTree(ensureArrayFormat(body));

        Iterator<JsonNode> iterator = root.getElements();
        while (iterator.hasNext()) {
          JsonNode            node             = iterator.next();
          Map<String, Object> mapProperties    = new HashMap<>();
          Map<String, String> requestInfoProps = new HashMap<>();
          NamedPropertySet    propertySet      = new NamedPropertySet("", mapProperties);

          processNode(node, "", propertySet, requestInfoProps);

          if (!requestInfoProps.isEmpty()) {
            // If this node has request info properties then add it as a
            // separate request body
            RequestBody requestBody = new RequestBody();
            requestBody.setBody(body);

            for (Map.Entry<String, String> entry : requestInfoProps.entrySet()) {
              String key   = entry.getKey();
              String value = entry.getValue();

              requestBody.addRequestInfoProperty(key, value);

              if (key.equals(QUERY_FIELD_NAME)) {
                requestBody.setQueryString(value);
              }
            }
            if (!propertySet.getProperties().isEmpty()) {
              requestBody.addPropertySet(propertySet);
            }
            requestBodySet.add(requestBody);
          } else {
            // If this node does not have request info properties then add it
            // as a new property set to the root request body
            if (!propertySet.getProperties().isEmpty()) {
              rootBody.addPropertySet(propertySet);
            }
            requestBodySet.add(rootBody);
          }
        }
      } catch (IOException e) {
        if (LOG.isDebugEnabled()) {
          LOG.debug("Caught exception parsing msg body.");
          LOG.debug("Message Body: {}", body, e);
        }
        throw new BodyParseException(e);
      }
    }
    if (requestBodySet.isEmpty()) {
      requestBodySet.add(rootBody);
    }
    return requestBodySet;
  }

}