예제 #1
0
 private BufferedReader toReader(InputStream in) {
   final JsonPathConfig cfg = getJsonPathConfig();
   try {
     return new BufferedReader(new InputStreamReader(in, cfg.charset()));
   } catch (UnsupportedEncodingException e) {
     throw new IllegalArgumentException("Charset is invalid", e);
   }
 }
예제 #2
0
  /**
   * Get the result of a Object path expression as a java Object. E.g. given the following Object
   * document:
   *
   * <pre>
   * { "store": {
   *   "book": [
   *    { "category": "reference",
   *      "author": "Nigel Rees",
   *      "title": "Sayings of the Century",
   *      "price": 8.95
   *    },
   *    { "category": "fiction",
   *      "author": "Evelyn Waugh",
   *      "title": "Sword of Honour",
   *      "price": 12.99
   *    },
   *    { "category": "fiction",
   *      "author": "Herman Melville",
   *      "title": "Moby Dick",
   *      "isbn": "0-553-21311-3",
   *      "price": 8.99
   *    },
   *    { "category": "fiction",
   *      "author": "J. R. R. Tolkien",
   *      "title": "The Lord of the Rings",
   *      "isbn": "0-395-19395-8",
   *      "price": 22.99
   *    }
   *  ],
   *    "bicycle": {
   *      "color": "red",
   *      "price": 19.95
   *    }
   *  }
   * }
   * </pre>
   *
   * And a Java object like this:
   *
   * <p>
   *
   * <pre>
   * public class Book {
   *      private String category;
   *      private String author;
   *      private String title;
   *      private String isbn;
   *      private float price;
   *
   *      public String getCategory() {
   *         return category;
   *      }
   *
   *     public void setCategory(String category) {
   *         this.category = category;
   *     }
   *
   *    public String getAuthor() {
   *          return author;
   *     }
   *
   *    public void setAuthor(String author) {
   *         this.author = author;
   *    }
   *
   *    public String getTitle() {
   *         return title;
   *    }
   *
   *    public void setTitle(String title) {
   *        this.title = title;
   *    }
   *
   *    public String getIsbn() {
   *             return isbn;
   *    }
   *
   *    public void setIsbn(String isbn) {
   *          this.isbn = isbn;
   *    }
   *
   *    public float getPrice() {
   *        return price;
   *    }
   *
   *    public void setPrice(float price) {
   *             this.price = price;
   *   }
   * }
   * </pre>
   *
   * <p>Then
   *
   * <pre>
   * Book book = from(Object).getObject("store.book[2]", Book.class);
   * </pre>
   *
   * <p>maps the second book to a Book instance.
   *
   * @param path The path to the object to map
   * @param objectType The class type of the expected object
   * @param <T> The type of the expected object
   * @return The object
   */
  public <T> T getObject(String path, Class<T> objectType) {
    Object object = getJsonObject(path);
    if (object == null) {
      return null;
    } else if (object instanceof List || object instanceof Map) {
      // TODO Avoid double parsing
      object = new JsonBuilder(object).toString();
    } else {
      return ObjectConverter.convertObjectTo(object, objectType);
    }

    JsonPathConfig cfg = new JsonPathConfig(getJsonPathConfig());
    if (cfg.hasCustomJackson10ObjectMapperFactory()) {
      cfg = cfg.defaultParserType(JsonParserType.JACKSON_1);
    } else if (cfg.hasCustomGsonObjectMapperFactory()) {
      cfg = cfg.defaultParserType(JsonParserType.GSON);
    } else if (cfg.hasCustomJackson20ObjectMapperFactory()) {
      cfg = cfg.defaultParserType(JsonParserType.JACKSON_2);
    }

    if (!(object instanceof String)) {
      throw new IllegalStateException(
          "Internal error: Json object was not an instance of String, please report to the REST Assured mailing-list.");
    }

    return JsonObjectDeserializer.deserialize((String) object, objectType, cfg);
  }
예제 #3
0
 private ConfigurableJsonSlurper createConfigurableJsonSlurper() {
   JsonPathConfig cfg = getJsonPathConfig();
   return new ConfigurableJsonSlurper(cfg.shouldRepresentJsonNumbersAsBigDecimal());
 }
예제 #4
0
 /**
  * Configure JsonPath to use a specific Gson object mapper factory
  *
  * @param factory The gson object mapper factory instance
  * @return a new JsonPath instance
  */
 public JsonPath using(GsonObjectMapperFactory factory) {
   return new JsonPath(this, jsonPathConfig.gsonObjectMapperFactory(factory));
 }