Examples for Moshi Json (#8753)

* Examples for Moshi Json

* Renamed Moshi tests to BDD style

* Updated some indents

* Minor code tweak to prefer ternary over if/else
This commit is contained in:
Graham Cox 2020-02-21 20:07:58 +00:00 committed by GitHub
parent a73581fbaf
commit b609d50214
9 changed files with 654 additions and 1 deletions

View File

@ -73,6 +73,16 @@
<version>${cache2k.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>${moshi.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi-adapters</artifactId>
<version>${moshi.version}</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
@ -135,7 +145,7 @@
<cactoos.version>0.43</cactoos.version>
<airline.version>2.7.2</airline.version>
<cache2k.version>1.2.3.Final</cache2k.version>
<moshi.version>1.9.2</moshi.version>
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
<aspectjrt.version>1.9.2</aspectjrt.version>
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>

View File

@ -0,0 +1,105 @@
package com.baeldung.moshi;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.Instant;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonQualifier;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.ToJson;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.Test;
public class AlternativeAdapterUnitTest {
@Test
public void whenSerializing_thenAlternativeAdapterUsed() {
Moshi moshi = new Moshi.Builder()
.add(new EpochMillisAdapter())
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = jsonAdapter.toJson(new Post("Introduction to Moshi Json", "Baeldung", Instant.now()));
System.out.println(json);
}
@Test
public void whenDeserializing_thenAlternativeAdapterUsed() throws IOException {
Moshi moshi = new Moshi.Builder()
.add(new EpochMillisAdapter())
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"author\":\"Baeldung\",\"posted\":1582095269204,\"title\":\"Introduction to Moshi Json\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class Post {
String title;
String author;
@EpochMillis Instant posted;
public Post() {
}
public Post(String title, String author, Instant posted) {
this.title = title;
this.author = author;
this.posted = posted;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Instant getPosted() {
return posted;
}
public void setPosted(Instant posted) {
this.posted = posted;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted)
.toString();
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@JsonQualifier
public @interface EpochMillis {
}
public static class EpochMillisAdapter {
@ToJson
public Long toJson(@EpochMillis Instant input) {
return input.toEpochMilli();
}
@FromJson
@EpochMillis
public Instant fromJson(Long input) {
return Instant.ofEpochMilli(input);
}
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.moshi;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import org.junit.Test;
public class ArrayUnitTest {
@Test
public void whenSerializingList_thenJsonArrayProduced() {
Moshi moshi = new Moshi.Builder()
.build();
Type type = Types.newParameterizedType(List.class, String.class);
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);
String json = jsonAdapter.toJson(Arrays.asList("One", "Two", "Three"));
System.out.println(json);
}
@Test
public void whenDeserializingJsonArray_thenListProduced() throws IOException {
Moshi moshi = new Moshi.Builder()
.build();
Type type = Types.newParameterizedType(List.class, String.class);
JsonAdapter<List<String>> jsonAdapter = moshi.adapter(type);
String json = "[\"One\",\"Two\",\"Three\"]";
List<String> result = jsonAdapter.fromJson(json);
System.out.println(result);
}
}

View File

@ -0,0 +1,94 @@
package com.baeldung.moshi;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.ToJson;
import org.junit.Test;
public class ComplexAdapterUnitTest {
@Test
public void whenSerializing_thenCorrectJsonProduced() {
Moshi moshi = new Moshi.Builder()
.add(new JsonDateTimeAdapter())
.build();
JsonAdapter<ZonedDateTime> jsonAdapter = moshi.adapter(ZonedDateTime.class);
String json = jsonAdapter.toJson(ZonedDateTime.now());
System.out.println(json);
}
@Test
public void whenDeserializing_thenCorrectJsonConsumed() throws IOException {
Moshi moshi = new Moshi.Builder()
.add(new JsonDateTimeAdapter())
.build();
JsonAdapter<ZonedDateTime> jsonAdapter = moshi.adapter(ZonedDateTime.class);
String json = "{\"date\":\"2020-02-17\",\"time\":\"07:53:27.064\",\"timezone\":\"Europe/London\"}";
ZonedDateTime now = jsonAdapter.fromJson(json);
System.out.println(now);
}
public static class JsonDateTimeAdapter {
@ToJson
public JsonDateTime toJson(ZonedDateTime input) {
String date = input.toLocalDate().toString();
String time = input.toLocalTime().toString();
String timezone = input.getZone().toString();
return new JsonDateTime(date, time, timezone);
}
@FromJson
public ZonedDateTime fromJson(JsonDateTime input) {
LocalDate date = LocalDate.parse(input.getDate());
LocalTime time = LocalTime.parse(input.getTime());
ZoneId timezone = ZoneId.of(input.getTimezone());
return ZonedDateTime.of(date, time, timezone);
}
}
public static class JsonDateTime {
private String date;
private String time;
private String timezone;
public JsonDateTime() {
}
public JsonDateTime(String date, String time, String timezone) {
this.date = date;
this.time = time;
this.timezone = timezone;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTimezone() {
return timezone;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.moshi;
import java.io.IOException;
import java.time.Instant;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.Test;
public class DefaultUnitTest {
@Test
public void whenDeserializing_thenFieldsGetDefaultValues() throws IOException {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"title\":\"My Post\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class Post {
private String title;
private String author;
private String posted;
public Post() {
posted = Instant.now().toString();
}
public Post(String title, String author, String posted) {
this.title = title;
this.author = author;
this.posted = posted;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPosted() {
return posted;
}
public void setPosted(String posted) {
this.posted = posted;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).append("posted", posted)
.toString();
}
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.moshi;
import java.io.IOException;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.Test;
public class PrimitiveUnitTest {
@Test
public void whenSerializing_thenCorrectJsonProduced() {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
Post post = new Post("My Post", "Baeldung", "This is my post");
String json = jsonAdapter.toJson(post);
System.out.println(json);
}
@Test
public void whenDeserializing_thenCorrectJsonConsumed() throws IOException {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"author\":\"Baeldung\",\"text\":\"This is my post\",\"title\":\"My Post\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class Post {
private String title;
private String author;
private String text;
public Post() {
}
public Post(String title, String author, String text) {
this.title = title;
this.author = author;
this.text = text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text)
.toString();
}
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.moshi;
import java.io.IOException;
import com.squareup.moshi.Json;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.jupiter.api.Test;
public class RenameUnitTest {
@Test
public void whenSerializing_thenFieldsGetRenamed() {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
Post post = new Post("My Post", "Baeldung");
String json = jsonAdapter.toJson(post);
System.out.println(json);
}
@Test
public void whenSerializing_thenRenamedFieldsGetConsumed() throws IOException {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class Post {
private String title;
@Json(name = "authored_by")
private String author;
public Post() {
}
public Post(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).toString();
}
}
}

View File

@ -0,0 +1,129 @@
package com.baeldung.moshi;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.ToJson;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.Test;
public class SimpleAdapterUnitTest {
@Test
public void whenSerializing_thenAdapterUsed() {
Moshi moshi = new Moshi.Builder()
.add(new AuthorAdapter())
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
Post post = new Post("My Post", new Author("Baeldung", "baeldung@example.com"), "This is my post");
String json = jsonAdapter.toJson(post);
System.out.println(json);
}
@Test
public void whenDeserializing_thenAdapterUsed() throws IOException {
Moshi moshi = new Moshi.Builder()
.add(new AuthorAdapter())
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"author\":\"Baeldung <baeldung@example.com>\",\"text\":\"This is my post\",\"title\":\"My Post\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class AuthorAdapter {
private Pattern pattern = Pattern.compile("^(.*) <(.*)>$");
@ToJson
public String toJson(Author author) {
return author.name + " <" + author.email + ">";
}
@FromJson
public Author fromJson(String author) {
Matcher matcher = pattern.matcher(author);
return matcher.find() ? new Author(matcher.group(1), matcher.group(2)) : null;
}
}
public static class Author {
private String name;
private String email;
public Author() {
}
public Author(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("name", name).append("email", email).toString();
}
}
public static class Post {
private String title;
private Author author;
private String text;
public Post() {
}
public Post(String title, Author author, String text) {
this.title = title;
this.author = author;
this.text = text;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).append("text", text)
.toString();
}
}
}

View File

@ -0,0 +1,66 @@
package com.baeldung.moshi;
import java.io.IOException;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.junit.jupiter.api.Test;
public class TransientUnitTest {
@Test
public void whenSerializing_thenTransientFieldIgnored() {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
Post post = new Post("My Post", "Baeldung");
String json = jsonAdapter.toJson(post);
System.out.println(json);
}
@Test
public void whenDeserializing_thenTransientFieldIgnored() throws IOException {
Moshi moshi = new Moshi.Builder()
.build();
JsonAdapter<Post> jsonAdapter = moshi.adapter(Post.class);
String json = "{\"authored_by\":\"Baeldung\",\"title\":\"My Post\"}";
Post post = jsonAdapter.fromJson(json);
System.out.println(post);
}
public static class Post {
private String title;
private transient String author;
public Post() {
}
public Post(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return new ToStringBuilder(this).append("title", title).append("author", author).toString();
}
}
}