Open gonciarz opened 8 years ago
Hey @gonciarz
This is interesting, have you trying a newer version of json-path
?
I looked for open issues in the jayway/JsonPath project and I did not find any one that matches this description. The number conversion seems to be done by this method https://github.com/jayway/JsonPath/blob/json-path-2.0.0/json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java#L74 but I am not familiar enough with GSON to know if it is doing something wrong.
This little utility was extracted from codebases that used Jackson and it has been working pretty well. If Jackson is not an option for you then I will investigate a little deeper and probably open an issue for the jayway/JsonPath project. PRs are welcomed.
ClassPath: com.revinate:assertj-json:1.0.1 com.jayway.jsonpath:json-path:2.0.0 com.google.code.gson:gson:2.7
Code to reproduce:
String json = "{\"id\":186,\"areaIds\":[39]}";
Configuration config = Configuration.builder() .jsonProvider(new GsonJsonProvider()) .mappingProvider(new GsonMappingProvider()) .build();
DocumentContext response = JsonPath.using(config).parse(content);
JsonPathAssert.assertThat(response) .jsonPathAsListOf("$.areaIds", Integer.class) .containsExactly(39);
java.lang.AssertionError: Expecting: <[39.0]> to contain exactly (and in same order): <[39]> but some elements were not found: <[39]> and others were not expected: <[39.0]>
After a quick investigation:
com.jayway.jsonpath.spi.mapper.GsonMappingProvider
@Override public T map(Object source, TypeRef targetType, Configuration configuration) { // source "[47]"
try {
return (T) factory.call().getAdapter(TypeToken.get(targetType.getType())).fromJsonTree((JsonElement) source); // result "[47.0]"
} catch (Exception e){
throw new MappingException(e);
}
}
com.google.gson.internal.bind,ObjectTypeAdapter JsonToken token is a NUMBER and ObjectTypeAdapter will always return Double.
case NUMBER: // * A JSON number represented in this API by a Java {@code double}, {@code long}, or {@code int}. return in.nextDouble();