Skip to content

Commit d634ed1

Browse files
authored
Merge pull request json-path#370 from jochenberger/fix-jackson-pojo
fix setting a POJO as a value using JacksonJsonNodeJsonProvider
2 parents 260ac60 + 4fece40 commit d634ed1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ private void setValueInObjectNode(ObjectNode objectNode, Object key, Object valu
270270
} else if (value == null) {
271271
objectNode.set(key.toString(), null); // this will create a null-node
272272
} else {
273-
throw new IllegalArgumentException("Cannot handle object type: " + value.getClass().getName());
273+
objectNode.put(key.toString(), createJsonElement(value));
274274
}
275275
}
276276

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.jayway.jsonpath;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
35
import com.fasterxml.jackson.databind.JsonNode;
46
import com.fasterxml.jackson.databind.node.ArrayNode;
57
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
@@ -9,6 +11,7 @@
911

1012
import java.io.IOException;
1113
import java.util.List;
14+
import java.util.UUID;
1215

1316
import static com.jayway.jsonpath.JsonPath.using;
1417
import static org.assertj.core.api.Assertions.assertThat;
@@ -111,6 +114,14 @@ public void test_type_ref_fail() throws IOException {
111114
}
112115

113116
@Test
117+
// https://github.com/json-path/JsonPath/issues/364
118+
public void setPropertyWithPOJO() {
119+
DocumentContext context = JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse("{}");
120+
UUID uuid = UUID.randomUUID();
121+
context.put("$", "data", new Data(uuid));
122+
String id = context.read("$.data.id", String.class);
123+
assertThat(id).isEqualTo(uuid.toString());
124+
}
114125
// https://github.com/json-path/JsonPath/issues/366
115126
public void empty_array_check_works() throws IOException {
116127
String json = "[" +
@@ -150,5 +161,15 @@ public static class FooBarBaz<T> {
150161
public static class Gen {
151162
public String eric;
152163
}
164+
165+
public static final class Data {
166+
@JsonProperty("id")
167+
UUID id;
168+
169+
@JsonCreator
170+
Data(@JsonProperty("id") final UUID id) {
171+
this.id = id;
172+
}
173+
}
153174

154175
}

0 commit comments

Comments
 (0)