- Json to model android
- About
- Учебник по JSON для Android: создание и анализ данных JSON
- Введение в JSON
- Moshi — JSON library for Android
- Why we need a library for serializing and deserializing in android?
- How do we use Moshi?
- Features of Moshi
- Using Moshi with List
- Using Moshi with Retrofit
- Steps 01.
- Step 02.
- Step 03.
- Step 04.
- Step 05.
- Extra
- Miscellaneous
Json to model android
Command line tool for generating Dart models (json_serializable) from Json file.
Feature | Status |
---|---|
Null safety | ✅ |
toJson/fromJson | ✅ |
immutable classes | ✅ |
copyWith generation | ✅ |
clone and deepclone | ✅ |
nested json classes | ✅ |
enum support | ✅ |
install using pub get command or if you using dart vscode/android studio, you can use install option.
What does this library do
Command line tool to convert .json files into immutable .dart models.
The command will run through your json files and find possible type, variable name, import uri, decorator and class name, and will write it into the templates.
Create/copy .json files into ./jsons/ (default) on root of your project, and run flutter pub run json_to_model .
Input Consider this files named product.json and employee.json
Output This will generate this product.dart and employee.dart
? products >) => Employee( id: id ?? this.id, displayName: displayName ?? this.displayName, products: products ?? this.products, ); @override bool operator ==(Object other) => identical(this, other) || other is Employee && other.id && displayName == other.displayName && products == other.products; @override int get hashCode => id.hashCode ^ displayName.hashCode ^ products.hashCode; > «>
Input Consider this file named location.json
Output This will generate this location.dart
- Create a directory jsons (default) at root of your project
- Put all or Create json files inside jsons directory
- run pub run json_to_model . or flutter packages pub run json_to_model flutter project
this package will read .json file, and generate .dart file, asign the type of the value as variable type and key as the variable name .
Description | Expression | Input (Example) | Output(declaration) | Output(import) |
---|---|---|---|---|
declare type depends on the json value | <"id": 1, "message":"hello world">, | int id; String message; | ||
import model and asign type | User auth; | import ‘user.dart’ | ||
import from path | < . : "$pathto/value" > | <"price":"$product/price"> | Price price; | import ‘../product/price.dart’ |
asign list of type and import (can also be recursive) | List addreses; | import ‘address.dart’ | ||
import other library(input value can be array) | <"@import":"package:otherlibrary/otherlibrary.dart"> | import ‘package:otherlibrary/otherlibrary.dart’ | ||
Datetime type | DateTime createdAt; | |||
Enum type | enum UserTypeEnum | |||
Enum type with values | enum UserTypeEnum |
About
Command line tool for generating Dart models (json_serializable) from Json file. partly inspired by json_model
Источник
Учебник по JSON для Android: создание и анализ данных JSON
В этом руководстве описывается, как использовать JSON с Android . JSON расшифровывается как (Java Script Object Notation). Это простой и легкий формат обмена данными, который может быть легко прочитан людьми и машинами. JSON — это текстовый формат, не зависящий от языка. Он представляет данные в текстовом формате, чтобы их можно было легко проанализировать.
Введение в JSON
JSON использует две разные структуры:
- Коллекция пары имя / значение
- массив
Первую структуру можно использовать для моделирования объекта, поскольку объект представляет собой набор атрибутов, которые содержат некоторые значения. Массив может использоваться для моделирования списка, массива объектов и так далее. Таким образом, используя эти две структуры, мы можем передавать данные между двумя машинами простым и эффективным способом. В последнее время JSON пользуется большим успехом, и большинство доступных API поддерживает формат JSON. Давайте посмотрим, как мы можем представлять данные в формате JSON.
Объект в JSON моделируется с помощью , а его атрибуты можно моделировать с помощью name: value pair.Value, в свою очередь, может быть объектом, массивом или «простым» значением, например, примитивным значением (int, Строка, логическое значение и т. Д.).
Так что если у нас есть, например, класс Java, как:
Источник
Moshi — JSON library for Android
In this blog, we are going to talk about the JSON library by Square called Moshi. Moshi helps us to serialize and deserialize the JSON in a better and simpler way.
So, before starting let us divide the blog into the following sections,
- Why we need a library for serializing and deserializing in android?
- How do we use Moshi?
- Features of Moshi
- Using Moshi with List.
- Using Moshi with Retrofit
Why we need a library for serializing and deserializing in android?
In Android, when we do an API call we get JSON as a response majority of times. We can call that JSON and parse it manually and work with it or we can just use a library like Moshi to serialize and deserialize. Using Moshi can help us reduce the number of lines we write and reduce the possibility of getting errors.
How do we use Moshi?
In this section, we are going to understand how we would work with Moshi.
Let say we have a data class, User like
and consider we have a variable called user and is defined as,
Now, with the help of Moshi, we will convert this as a JSON structure. To work with Moshi we have JsonAdapter class and builder pattern.
So, we build Moshi using,
and we will also create a variable of type JsonAdapter, which will help us work to and fro in converting JSON to Object class and vice-a-versa.
Here, we have passed the User data class as a structure on which we want to perform the actions. Now, to convert the object of the User class to JSON we use,
Here, we are using toJson to get JSON from the object of the User and if we print this, we get
Similarly, we can map a JSON to a class using Moshi. Let’s say we want to convert the above JSON to User class, we will use,
If you see, we are using fromJson here which helps us to convert the JSON which we have passed as a string. Now, if we print this we will see,
This is how we convert JSON to object and object to JSON using Moshi.
Features of Moshi
Moshi supports almost all the data type by default like,
- Integer, Float, etc
- Arrays and Collections
- Strings
- Enums
In Moshi, we can create our own type apart from the mentioned ones above. Let us understand this by an example,
Let’s update the User class like,
Here, we have added a new data class called Name, which will look like,
Here, in Name class, we take two parameters called firstName and lastName.
Now, when getting a JSON from this class, I want the full name of the user like firstName + lastName. Either we can do it manually every time we parse the JSON or we can add our own adapter using Moshi to do it for us.
So, we will create a class called NameAdapter,
And inside this class, we are going to do our conversion. We will add two functions named as fun fullName() and fun getIndividualNames().
The class now looks like,
Here, you can see we have annotated fullName with ToJson and getIndividualNames with FromJson.
It means, that when using this adapter with Moshi, Moshi will look for the annotations.
Let’s say we want to concatenate both first and last names to return the full name and return it in JSON, we will do it inside the fullName function which is annotated with ToJson . Now, the fullName function will look like,
Similarly, since we added the ToJson conversion for the JSON parsing, we would also need to update the getIndividualNames function which is annotated with FromJson which will convert the JSON values to individual elements in Name data class when mapping the JSON to class.
So, the getIndividaulNames will look like,
Here, we have splitter the string fullName from the first empty space and got the two strings that are first name and last name in a list of string.
And finally, to use this Adapter we add it in Moshi object while building it like,
Note: Moshi’s adapters are ordered by precedence, so you always want to add the Kotlin adapter after your own custom adapters. Otherwise the KotlinJsonAdapterFactory will take precedence and your custom adapters will not be called.
The JsonAdapter would be the same as above like,
and now when we print the toJson and FromJson in logcat like,
We get the following output,
This is how you can create your own conversion adapter.
Now, let’s say we want to put some condition check on only one field in the class or different fields of the same type. We can perform that as well where we create an adapter and that will only affect the only field mentioned. We would do it using annotation.
First, we will create an annotation like,
Here, the Email check is annotated with JsonQualifier which would work with specific fields.
Now, we will update the User class like,
Here, you can see we have annotated the email with EmailCheck, which means that the check with all the EmailCheck annotations will work with only the email field. The Name class remains the same.
Now, we will create an adapter which would have two functions, namely toJson and fromJson and will look like,
Here, in the EmailAdapter we have annotated the fromJson function to EmailCheck and here we will check if the JSON we are parsing to map it to the class is not a valid email then, we will return No Email Found in the email field else we return the email itself.
Similarly, we also annotated the email parameter in toJson with EmailCheck, which would mean only keys with EmailCheck annotation are allowed.
Finally, we will also update the Moshi builder with a new adapter factory called KotlinJsonAdapterFactory like,
And now, let’s create a user object-like,
and the jsonAdapter remains the same as how we have used it above. Now, if we log the jsonAdapter.toJson(user), we get the following as response,
Here, you can see we are not passing a valid email and is just an integer value.
Finally, when we parse this JSON using fromJson() then we get the output,
You can see in the email field we get No Email Found because the email was not a valid email. This is how we can create adapters for individual fields and put some condition-specific to it.
Using Moshi with List
In this section, we will learn how to convert a List to String and then String to a list.
Consider an example, where we get a list of objects from our server and we need to store it in shared preference of our app. We would like to convert the list of objects to string as it only saves a primitive data type to save it and when we need to use it we will convert it back to a list.
Now, let’s say we have a List of String like,
and now we need to set type to map the raw data like,
Here, We have list of String data, so it takes the String::class as class to identify what are the type of elements and List is the type of data which has to be converted.
Then to use this type, we will use it with Moshi adapter like,
where moshi looks like,
Now, to convert the list of data to string we will use the toJson like,
This would map the list data to string and now if we want we can store in sharedpreference.
Now, if we want to reverse the mapping from string to list again, we will use the same adapter which we created and by using fromJson of moshi we will convert it like,
Here we need to pass the string which we need to convert back to a List.
Using Moshi with Retrofit
In this section, we are going to talk about how we can do an API call using Moshi as the converter in Android. We are going to call,
To get a single post using Retrofit. So, first, let’s break it down in steps.
Steps 01.
We will first setup the dependency on the build.gradle like,
Step 02.
We will not create a data class called PostsResponse for the JSON response we will get from the API. The JSON looks like,
So, the data class mapping to this JSON would look like,
Here, you can see we have used @Json to all the fields in the data class.
@Json takes the key name in the original JSON response, and just because we are using @Json we can give any name to the data class constructor variables and the JSON would still be mapped to the data class because the mapping here is happening because of the @Json annotation.
Step 03.
Now, we will create the interface to hold the API call. We will call it as APIService and will look like,
It will have only one function called getSinglePost and will return the only post from the URL.
Step 04.
Now, to setup Retrofit, we will first create an instance of Moshi like,
and then we setup Retrofit like,
Here, we have passed the based URL and the converter factory for Moshi. We also passed the Moshi as a parameter which we created earlier.
And, to support KotlinJsonAdapterFactory additionally, needs the following dependency,
Note: MoshiConverterFactory.create(moshi) is the converter factory.
Step 05.
We will do the API Call now like,
And before running the app, do not forget to add the Internet permission in the Manifest file.
Now, if we run the app, we get,
So, the API call was successful.
Extra
Let’s say if we want to ignore title in the API response, we have updated the model like,
Here, you can see we have added Transient annotation which will ignore the field title and print the output as,
Here, the title field is coming empty.
Miscellaneous
- Moshi is very light weighted.
- It uses the same mechanisms of GSON.
- We can add custom field names using @Json annotation.
This is how we can use Moshi in our Android project.
Источник