JsonPath icon indicating copy to clipboard operation
JsonPath copied to clipboard

Can't compile expression with hyphen.

Open kminder opened this issue 10 years ago • 3 comments

Might be working as designed but for me not as expected. To illustrate, this works JsonPath.compile( "$['field-A']" ); and this doesn't. JsonPath.compile( "$.field-A" ); I more complete test case follows. I think the fails() test should pass.

import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.nebhale.jsonpath.JsonPath; import org.junit.BeforeClass; import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat;

public class JsonPathBug {

private static JsonNode json;

@BeforeClass public static void setupSuite() throws IOException { ObjectMapper mapper = new ObjectMapper(); JsonFactory factory = new JsonFactory(); json = mapper.readTree( "{ "field-A" : "value-A", "field_B" : "value_B" }" ); }

@Test public void passes() { assertJsonPath( json, "$['field-A']", "value-A" ); assertJsonPath( json, "$['field_B']", "value_B" ); assertJsonPath( json, "$.field_B", "value_B" ); }

@Test public void fails() { assertJsonPath( json, "$.field-A", "value-A" ); }

private void assertJsonPath( JsonNode node, String xpath, String expected ) { assertThat( JsonPath.read( xpath, json, String.class ), is( expected ) ); assertThat( JsonPath.compile( xpath ).read( json, String.class ), is( expected ) ); }

}

kminder avatar Jul 17 '13 20:07 kminder