-
Notifications
You must be signed in to change notification settings - Fork 0
/
CartCheckoutResponseJsonAdapter.java
82 lines (76 loc) · 3.59 KB
/
CartCheckoutResponseJsonAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.sinan.javademo.apiapplication.adapter;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.sinan.javademo.apiapplication.contract.CartCheckoutResponse;
import com.sinan.javademo.apiapplication.model.CartDiscount;
import com.sinan.javademo.apiapplication.util.APIHelper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* A custom adapter to serialize/deserialize {@link CartCheckoutResponse} to/from json using Gson library.
*
* @author Sinan Wannous
* @see <a href="https://www.javadoc.io/doc/com.google.code.gson/gson/2.8.1/com/google/gson/TypeAdapter.html">Class TypeAdapter</a>
* @see <a href="https://github.com/google/gson">Google Gson</a>
* @since 1.0
*/
public class CartCheckoutResponseJsonAdapter extends TypeAdapter<CartCheckoutResponse> {
@Override
public void write(JsonWriter jsonWriter, CartCheckoutResponse cartCheckoutResponse) throws IOException {
jsonWriter.beginObject();
jsonWriter.name("subTotalPrice").value(APIHelper.formatDouble(cartCheckoutResponse.getSubTotalPrice()));
jsonWriter.name("offers");
jsonWriter.beginArray();
for (var offer : cartCheckoutResponse.getOffers()) {
jsonWriter.beginObject();
jsonWriter.name("discountDescription").value(offer.getDescription());
jsonWriter.name("discountValue").value(APIHelper.formatDouble(offer.getValue()));
jsonWriter.endObject();
}
jsonWriter.endArray();
jsonWriter.name("totalPrice").value(APIHelper.formatDouble(cartCheckoutResponse.getTotalPrice()));
jsonWriter.name("currency").value(cartCheckoutResponse.getCurrency());
jsonWriter.endObject();
}
@Override
public CartCheckoutResponse read(JsonReader jsonReader) throws IOException {
CartCheckoutResponse cartCheckoutResponse = new CartCheckoutResponse();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
switch (jsonReader.nextName()) {
case "subTotalPrice" -> cartCheckoutResponse.setSubTotalPrice(jsonReader.nextDouble());
case "totalPrice" -> cartCheckoutResponse.setTotalPrice(jsonReader.nextDouble());
case "currency" -> cartCheckoutResponse.setCurrency(jsonReader.nextString());
case "offers" -> cartCheckoutResponse.setOffers(readCartDiscounts(jsonReader));
default -> jsonReader.skipValue();
}
}
jsonReader.endObject();
return cartCheckoutResponse;
}
private List<CartDiscount> readCartDiscounts(JsonReader jsonReader) throws IOException {
List<CartDiscount> cartDiscounts = new ArrayList<>();
jsonReader.beginArray();
while (jsonReader.hasNext() && jsonReader.peek() != JsonToken.END_ARRAY) {
cartDiscounts.add(readCartDiscount(jsonReader));
}
jsonReader.endArray();
return cartDiscounts;
}
private CartDiscount readCartDiscount(JsonReader jsonReader) throws IOException {
CartDiscount cartDiscount = new CartDiscount();
jsonReader.beginObject();
while (jsonReader.hasNext()) {
switch (jsonReader.nextName()) {
case "discountDescription" -> cartDiscount.setDescription(jsonReader.nextString());
case "discountValue" -> cartDiscount.setValue(jsonReader.nextDouble());
default -> jsonReader.skipValue();
}
}
jsonReader.endObject();
return cartDiscount;
}
}