Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Suggestion]: Path expression for fields. #44

Open
curioustechizen opened this issue Mar 5, 2015 · 4 comments
Open

[Feature Suggestion]: Path expression for fields. #44

curioustechizen opened this issue Mar 5, 2015 · 4 comments

Comments

@curioustechizen
Copy link

It would be great to be able to annotate a field with an XPath-like expression in addition to just a field name. The use case for this is to have a flatter POJO hierarchy than the JSON.

Example:

{
  "some-meta-data": "foobar",
  "comment": 
    {
      "user": "Me",
      "timestamp": 12345678,
      "text": "WooHoo what an awesome comment"
    }
}

Currently, if I am not interested in the metadata, I am still forced to create a top level Java object.

class Response {

  @JsonField
  Comment comment;

  static class Comment {
    @JsonField String user;
    @JsonField long timestamp;
    @JsonField String text;
  }
}

Using an XPath-like expression, I could maybe even eliminate the need to create the top-level Response class altogether.

@JsonPathRoot("$.comment")
class Comment {
  @JsonField String user;
  @JsonField long timestamp;
  @JsonField String text;
}

Or perhaps, using a XPath expression, that might be @JsonPathRoot("//comment").

Prior art:

IMO these libraries serve a different purpose - querying large JSON data to extract certain information. For POJO-JSON mapping, a far simpler syntax would suffice.

None of the popular JSON mapping libraries for Java support this currently. GSON does provide a way to parse a JsonPath expression, but I did not find a way to specify the Path in place of a field name as an annotation.

@yongjhih
Copy link

👍

@ppamorim
Copy link
Contributor

ppamorim commented Jan 9, 2016

  • 1

@jasonni
Copy link

jasonni commented Dec 5, 2016

👍

@curioustechizen
Copy link
Author

There is now a library to do this for GSON. It is actually an annotation processor - https://github.com/LachlanMcKee/gsonpath

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants