[JAVA] Moshi ~ Json conversion library ~

I used to handle json in java, so when I was looking for a library, I was told how to use it: sunglasses:

What is Moshi

GitHub This is a library published by square on GitHub.

It parses JSON into classes, and conversely serializes classes into JSON. It's easy to use.

Usage

Gradle

compile 'com.squareup.moshi:moshi:1.5.0'

Just add

Parse

Create a class to parse


class PersonList {
    Map<String, Person> list;
}

class Person {
    int age;
    Sex sex;
}

enum Sex {
    MAN,
    WOMAN
}

Click here for the part to parse

String json = "xxxxxxx";

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<PersonList> jsonAdapter = moshi.adapter(PersonList.class);

try {
    PersonList personList = jsonAdapter.fromJson(json);
} catch (Exception e) {
    Log.e("json parse error", "parsePersonList: ", e);
}

That's it (laughs)

Serialize

Click here for the serialize part

PersonList list = /*Create*/

Moshi moshi = new Moshi.Builder().build();
JsonAdapter<PersonList> jsonAdapter = moshi.adapter(PersonList.class);

try {
   String json = jsonAdapter.toJson(list);
} catch (Exception e) {
    Log.e("json serialize error", "serializePersonList: ", e);
}

Almost the same, but fromJson () and toJson () have changed

Caution

Please note that the classes that can be used are limited. ʻArrayList HashMapetc. cannot be used, so let's useList Map`

・ Primitives (int, float, char ...) and their boxed counterparts (Integer, Float, Character ...). ・ Arrays, Collections, Lists, Sets, and Maps ・ Strings ・ Enums

CustomAdapter

class PersonAdapter {
    @ToJson String toJson(Person person) {
      return String.valueOf(person.age) + sex.suit.name().substring(0, 1);
    }

    @FromJson Person fromJson(String json) {
        return //Parse String and create Person class
    }
}

I'm sorry I omitted the String parsing: sob:

By the way, another one

class ForthPerson {
    int forthAge;
    Sex sex;
}

If there is a class of people who are four times as old as ordinary people (laughs), it can be converted at the time of parse

class ForthPersonAdapter {
  @FromJson ForthPerson fromJson(Person person) {
    ForthPerson forthPerson = new ForthPerson();
    forthPerson.age = person.age * 4;
    forthPerson.sex = person.sex;
    return forthPerson;
  }

  @ToJson Person toJson(ForthPerson forthPerson) {
    Person person = new Person();
    person.age = forthPerson.age / 4;
    person.sex = forthPerson.sex;
    return person;
  }
}
Moshi moshi = new Moshi.builder()
        .add(new ForthPersonAdapter())
        .build();

You can use it now!

Get List

You can also get an array of classes

String json = "xxxxxxx";

Type type = Types.newParameterizedType(List.class, Person.class);
JsonAdapter<List<Person>> adapter = moshi.adapter(type);
List<Person> people = adapter.fromJson(json);

You can get an array of Person classes in people

Recommended Posts

Moshi ~ Json conversion library ~
Mutual conversion between Java objects and JSON using Moshi
[Jackson] JSON ⇔ object conversion class