Browse Source

Merge pull request #370 from jochenberger/fix-jackson-pojo

fix setting a POJO as a value using JacksonJsonNodeJsonProvider
pull/337/merge
kallestenflo 7 years ago committed by GitHub
parent
commit
d634ed1e6c
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java
  2. 21
      json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

2
json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java

@ -270,7 +270,7 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
} else if (value == null) {
objectNode.set(key.toString(), null); // this will create a null-node
} else {
throw new IllegalArgumentException("Cannot handle object type: " + value.getClass().getName());
objectNode.put(key.toString(), createJsonElement(value));
}
}

21
json-path/src/test/java/com/jayway/jsonpath/JacksonJsonNodeJsonProviderTest.java

@ -1,5 +1,7 @@
package com.jayway.jsonpath;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
@ -9,6 +11,7 @@ import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import static com.jayway.jsonpath.JsonPath.using;
import static org.assertj.core.api.Assertions.assertThat;
@ -111,6 +114,14 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest {
}
@Test
// https://github.com/json-path/JsonPath/issues/364
public void setPropertyWithPOJO() {
DocumentContext context = JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse("{}");
UUID uuid = UUID.randomUUID();
context.put("$", "data", new Data(uuid));
String id = context.read("$.data.id", String.class);
assertThat(id).isEqualTo(uuid.toString());
}
// https://github.com/json-path/JsonPath/issues/366
public void empty_array_check_works() throws IOException {
String json = "[" +
@ -150,5 +161,15 @@ public class JacksonJsonNodeJsonProviderTest extends BaseTest {
public static class Gen {
public String eric;
}
public static final class Data {
@JsonProperty("id")
UUID id;
@JsonCreator
Data(@JsonProperty("id") final UUID id) {
this.id = id;
}
}
}

Loading…
Cancel
Save