bluelinelabs / bluelinelabs/LoganSquare

[Feature Suggestion]: Path expression for fields.

Open
#44 4 comments 2 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
3.2k
Forks
303
PR merge metrics
No merged PRs in 30d

Description

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:

``` json
{
"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.

``` java
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.

``` java
@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:
- [JsonPath](https://github.com/jayway/JsonPath): Also has providers for Jackson and GSON.
- [jsonselect](http://jsonselect.org/)

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reviewing the existing field annotations and the JSON-to-POJO mapping flow in LoganSquare. Compare the proposed root and nested path forms with the JsonPath prior art mentioned in the issue. Done should include a settled path syntax, support for the requested flattened mapping, and tests covering the example JSON.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.