JsonPath仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Patrik Helsing 27137211a6 [#119] Added README with usage examples 9 years ago
..
src [#119] Some minor refactoring to reduce amount of code, and collect the factory methods in JsonPathMatchers 9 years ago
README.md [#119] Added README with usage examples 9 years ago
build.gradle Gradle build cleanup. 10 years ago

README.md

json-path-assert

A library with hamcrest-matchers for JsonPath.

Getting started

This library is available at the Central Maven Repository. Maven users add this to your POM.

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path-assert</artifactId>
    <version>2.1.0</version>
</dependency>

Usage

Statically import the library entry point:

import static com.jayway.jsonpath.matchers.JsonPathMatchers.*;

Example usage:

// NOTE: The actual evaluation will depend on the current JsonPath configuration
Configuration.setDefaults(...);

// The json to be examined could be represented as a String...
String json = ...;

// a file...
File json = ...;

// or an already parsed json object...
Object json = Configuration.defaultConfiguration().jsonProvider().parse(content);

// Verify validity of JSON
assertThat(json, isJson());

// Verify existence of JSON path
assertThat(json, hasJsonPath("$.message"));

// Verify evaluation of JSON path
assertThat(json, hasJsonPath("$.message", equalTo("Hi there")));
assertThat(json, hasJsonPath("$.quantity", equalTo(5)));
assertThat(json, hasJsonPath("$.price", equalTo(34.56)));
assertThat(json, hasJsonPath("$.store.book[*].author", hasSize(4)));
assertThat(json, hasJsonPath("$.store.book[*].author", hasItem("Evelyn Waugh")));

// Combine several JSON path evaluations
assertThat(json, isJson(allOf(
    withJsonPath("$.store.name", equalTo("Little Shop")),
    withJsonPath("$..title", hasSize(4)))));
    
// Use typed matchers for increased clarity
String json = ...
assertThat(json, isJsonString(withJsonPath("$..author")));

File json = ...
assertThat(json, isJsonFile(withJsonPath("$..author")));