Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artem-zinnatullin/autojackson
Small example of AutoValue/AutoParcel + Jackson
https://github.com/artem-zinnatullin/autojackson
Last synced: 23 days ago
JSON representation
Small example of AutoValue/AutoParcel + Jackson
- Host: GitHub
- URL: https://github.com/artem-zinnatullin/autojackson
- Owner: artem-zinnatullin
- License: apache-2.0
- Created: 2015-08-19T17:49:39.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-07-19T00:49:13.000Z (over 5 years ago)
- Last Synced: 2023-03-22T16:36:38.251Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 62.5 KB
- Stars: 72
- Watchers: 2
- Forks: 12
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AutoJackson
### Why AutoValue is awesome?
Just read [Google's explanation!](https://github.com/google/auto/tree/master/value)TLTR: It gives you immutability (thread safety!), `equals()`, `hashCode()` and `toString()` for free!
------
### Okay, I'm in, show me the code!Simple example of class annotated with [AutoValue](https://github.com/google/auto/tree/master/value) or [AutoParcel](https://github.com/frankiesardo/auto-parcel) + [Jackson](https://github.com/FasterXML/jackson).
It is **serializable** to JSON and **deserializable** from JSON, it's immutable and it has correctly implemented `equals()`, `hashCode()` and `toString()`! Life is better with [AutoValue](https://github.com/google/auto/tree/master/value)!
```java
// Just use @AutoParcel annotations if you need AutoParcel@AutoValue
@JsonSerizalize(as = Tweet.class)
@JsonDeserialize(builder = Tweet.Builder.class)
public abstract class Tweet {@NotNull
public static Builder builder() {
return Builder.builder();
}@NotNull
@JsonProperty("author")
public abstract String author();@NotNull
@JsonProperty("content")
public abstract String content();@AutoValue.Builder
public static abstract class Builder {
@JsonCreator
public static Builder builder() {
return new AutoValue_Tweet.Builder();
}
@NotNull
@JsonProperty("author")
public abstract Builder author(@NotNull String author);@NotNull
@JsonProperty("content")
public abstract Builder content(@NotNull String content);@NotNull
public abstract Tweet build();
}
}
```**Serialization** and **deserialization**:
```java
@Test
public void shouldSerializeToJson() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();Tweet tweet = Tweet.builder()
.author("@artem_zin")
.content("Immutability for everybody!")
.build();assertThatJson(objectMapper.writeValueAsString(tweet))
.isEqualTo("{\"author\":\"@artem_zin\",\"content\":\"Immutability for everybody!\"}");
}@Test
public void shouldDeserializeFromJson() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();String json = "{\"author\":\"@artem_zin\",\"content\":\"Immutability for everybody!\"}";
Tweet tweet = objectMapper.readValue(json, Tweet.class);
assertThat(tweet.author()).isEqualTo("@artem_zin");
assertThat(tweet.content()).isEqualTo("Immutability for everybody!");
}
```#### Build the project
Run `sh ci.sh`.#### Contact me
https://twitter.com/artem_zin