Moshi does not have built-in adapters to process types like java.math.BigDecimal
; we need to manually provide a custom adapter to handle the BigDecimal
type.
Table of contents:
- 1. Download Moshi
- 2. BigDecimal requires explicit JsonAdapter
- 3. Make Moshi supports BigDecimal
- 4. Download Source Code
- 5. References
P.S Tested with Moshi 1.15.1
1. Download Moshi
Declare moshi
in the pom.xml
.
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
2. BigDecimal requires explicit JsonAdapter
The following staff
object contains the type java.math.BigDecimal
.
package com.mkyong.json.model;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Staff {
private String name;
private int age;
private String[] position; // Array
private List<String> skills; // List
private Map<String, BigDecimal> salary; // Map
private boolean active; // boolean
// getters, setters and constructors
}
If we use Moshi to serialize the object into JSON, Moshi will throw the error java.math.BigDecimal requires explicit JsonAdapter to be registered
.
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Staff> jsonAdapter = moshi.adapter(Staff.class);
Staff staff = createStaff();
String json = jsonAdapter.toJson(staff); // throws error
Caused by: java.lang.IllegalArgumentException:
Platform class java.math.BigDecimal requires explicit JsonAdapter to be registered
at com.squareup.moshi.ClassJsonAdapter$1.create(ClassJsonAdapter.java:76)
at com.squareup.moshi.Moshi.adapter(Moshi.java:146)
... 11 more
3. Make Moshi supports BigDecimal
We need to create and register a custom JsonAdapter
to handle the type BigDecimal
in Moshi.
3.1 Define `BigDecimal` Adapter
Create logic to handle the BigDecimal
.
package com.mkyong.json.moshi.adapter;
import com.squareup.moshi.FromJson;
import com.squareup.moshi.ToJson;
import java.math.BigDecimal;
public class BigDecimalJsonAdapter {
// Converts BigDecimal to its string representation
@ToJson
String toJson(BigDecimal value) {
return value.toString();
}
// Constructs a BigDecimal from the string representation
@FromJson
BigDecimal fromJson(String value) {
return new BigDecimal(value);
}
}
3.2 Register the Adapter with Moshi
The following example will register a BigDecimalJsonAdapter
and convert the staff
object that contains the type BigDecimal
into JSON.
package com.mkyong.json.moshi;
import com.mkyong.json.model.Staff;
import com.mkyong.json.moshi.adapter.BigDecimalJsonAdapter;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class ObjectToJsonExample2 {
public static void main(String[] args) {
Moshi moshi = new Moshi.Builder()
.add(new BigDecimalJsonAdapter()) // Register the BigDecimal adapter
.build();
JsonAdapter<Staff> jsonAdapter = moshi.adapter(Staff.class).indent(" ");
Staff staff = createStaff();
try {
String json = jsonAdapter.toJson(staff);
System.out.println(json);
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
}
}
private static Staff createStaff() {
Staff staff = new Staff();
staff.setName("mkyong");
staff.setAge(42);
staff.setPosition(new String[]{"Founder", "CTO", "Writer", "Minimalists"});
Map<String, BigDecimal> salary = new HashMap<>();
salary.put("2010", new BigDecimal(10000));
salary.put("2012", new BigDecimal(12000));
salary.put("2018", new BigDecimal(14000));
staff.setSalary(salary);
staff.setSkills(Arrays.asList("java", "python", "node", "kotlin"));
return staff;
}
}
Output
{
"active": false,
"age": 42,
"name": "mkyong",
"position": [
"Founder",
"CTO",
"Writer",
"Minimalists"
],
"salary": {
"2018": "14000",
"2012": "12000",
"2010": "10000"
},
"skills": [
"java",
"python",
"node",
"kotlin"
]
}
4. Download Source Code
$ git clone https://github.com/mkyong/java-json
$ cd moshi
5. References
- Moshi Github
- Parse JSON using Moshi
- Write JSON to a file using Moshi
- Parse JSON string with Jackson
- Convert Java Objects to JSON with Jackson
The post Moshi – java.math.BigDecimal requires explicit JsonAdapter to be registered appeared first on Mkyong.com.