{"action":"created","issue":{"url":"https://api.github.com/repos/jayway/JsonPath/issues/246","repository_url":"https://api.github.com/repos/jayway/JsonPath","labels_url":"https://api.github.com/repos/jayway/JsonPath/issues/246/labels{/name}","comments_url":"https://api.github.com/repos/jayway/JsonPath/issues/246/comments","events_url":"https://api.github.com/repos/jayway/JsonPath/issues/246/events","html_url":"https://github.com/jayway/JsonPath/issues/246","id":166083447,"number":246,"title":"Please provide a way to register custom functions.","user":{"login":"tbarabanov","id":13729010,"avatar_url":"https://avatars.githubusercontent.com/u/13729010?v=3","gravatar_id":"","url":"https://api.github.com/users/tbarabanov","html_url":"https://github.com/tbarabanov","followers_url":"https://api.github.com/users/tbarabanov/followers","following_url":"https://api.github.com/users/tbarabanov/following{/other_user}","gists_url":"https://api.github.com/users/tbarabanov/gists{/gist_id}","starred_url":"https://api.github.com/users/tbarabanov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbarabanov/subscriptions","organizations_url":"https://api.github.com/users/tbarabanov/orgs","repos_url":"https://api.github.com/users/tbarabanov/repos","events_url":"https://api.github.com/users/tbarabanov/events{/privacy}","received_events_url":"https://api.github.com/users/tbarabanov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2016-07-18T12:35:48Z","updated_at":"2016-09-20T19:59:53Z","closed_at":null,"body":"I'm interested in adding my own custom function which I could use in JsonPath expressions. Particularly I'm interested in alphanumeric sort() function. So $.phonebook.phones.sort() should give phone numbers sorted alphanumerically. Could You please give me an advice?"},"comment":{"url":"https://api.github.com/repos/jayway/JsonPath/issues/comments/248416640","html_url":"https://github.com/jayway/JsonPath/issues/246#issuecomment-248416640","issue_url":"https://api.github.com/repos/jayway/JsonPath/issues/246","id":248416640,"user":{"login":"helsing","id":5503021,"avatar_url":"https://avatars.githubusercontent.com/u/5503021?v=3","gravatar_id":"","url":"https://api.github.com/users/helsing","html_url":"https://github.com/helsing","followers_url":"https://api.github.com/users/helsing/followers","following_url":"https://api.github.com/users/helsing/following{/other_user}","gists_url":"https://api.github.com/users/helsing/gists{/gist_id}","starred_url":"https://api.github.com/users/helsing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/helsing/subscriptions","organizations_url":"https://api.github.com/users/helsing/orgs","repos_url":"https://api.github.com/users/helsing/repos","events_url":"https://api.github.com/users/helsing/events{/privacy}","received_events_url":"https://api.github.com/users/helsing/received_events","type":"User","site_admin":false},"created_at":"2016-09-20T19:59:53Z","updated_at":"2016-09-20T19:59:53Z","body":"@adrian-herscu In this particular case, since the json representation _is_ a string and not a number, I think it would make sense to do the actual parsing in the assertion rather than as a customized function of the json-path. For example consider the difference when verifying a numerical json representation:\r\n\r\n    String json = \"{ \\\"price\\\": 34.56 }\";\r\n    assertThat(json, hasJsonPath(\"$.price\", equalTo(34.56)));\r\n\r\nThis json would be different, and you would like your tests to detect if the json representation for some reason accidentally change to a string, right? So in your case, you could do something similar to this:\r\n\r\n    String json = \"{ \\\"price\\\": \\\"34.56\\\" }\";\r\n    assertThat(json, hasJsonPath(\"$.price\", doubleAsString(equalTo(34.56))));\r\n\r\nwith a custom hamcrest-matcher like this:\r\n\r\n    private static Matcher<String> doubleAsString(final Matcher<? extends Number> numberMatcher) {\r\n        return new TypeSafeMatcher<String>() {\r\n            @Override\r\n            protected boolean matchesSafely(String string) {\r\n                try {\r\n                    return numberMatcher.matches(Double.valueOf(string));\r\n                } catch (NumberFormatException e) {\r\n                    return false;\r\n                }\r\n            }\r\n\r\n            @Override\r\n            public void describeTo(Description description) {\r\n                description.appendText(\"double as string \").appendDescriptionOf(numberMatcher);\r\n            }\r\n        };\r\n    }\r\n\r\nThis solution would gracefully catch any non-numeric content of the 'price' attribute, as well."}}
{"actor":{"display_login":"helsing"}}