Skip to content

Commit 6e481b5

Browse files
committed
Allow deserialization of product to take care of different format in the product extension and custom attributes
1 parent 79e5e06 commit 6e481b5

File tree

5 files changed

+269
-1
lines changed

5 files changed

+269
-1
lines changed

src/main/java/com/github/chen0040/magento/models/Product.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7+
import com.alibaba.fastjson.annotation.JSONField;
78
import lombok.Getter;
89
import lombok.Setter;
910

@@ -34,9 +35,15 @@ public class Product {
3435
private String created_at = "2017-05-03 03:46:13";
3536
private String updated_at = "2017-05-03 03:46:13";
3637
private double weight = 1;
38+
39+
@JSONField(deserializeUsing = ProductAttributeValueDeserializer.class)
3740
private List<MagentoAttribute> extension_attributes = new ArrayList<>();
3841
private List<String> product_links = new ArrayList<>();
3942
private List<TierPrices> tier_prices = new ArrayList<>();
43+
44+
@JSONField(deserializeUsing = ProductAttributeValueDeserializer.class)
4045
private List<MagentoAttribute> custom_attributes = new ArrayList<>();
4146

4247
}
48+
49+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.chen0040.magento.models;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import com.alibaba.fastjson.annotation.JSONField;
6+
import com.alibaba.fastjson.parser.DefaultJSONParser;
7+
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
8+
import com.alibaba.fastjson.serializer.JSONSerializer;
9+
import com.alibaba.fastjson.serializer.ObjectSerializer;
10+
11+
import java.io.IOException;
12+
import java.lang.reflect.Type;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class ProductAttributeValueDeserializer implements ObjectDeserializer {
17+
@Override
18+
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
19+
Object obj = parser.parse();
20+
21+
List<MagentoAttribute> result = new ArrayList<>();
22+
23+
if(obj instanceof JSONArray) {
24+
JSONArray ja = (JSONArray)obj;
25+
for(int i=0; i < ja.size(); ++i) {
26+
Object jaObj = ja.get(i);
27+
if(jaObj instanceof JSONObject){
28+
JSONObject jasObj = (JSONObject)jaObj;
29+
30+
String key = null;
31+
String value = null;
32+
if(jasObj.containsKey("attribute_code")){
33+
key = jasObj.get("attribute_code").toString();
34+
}
35+
if(jasObj.containsKey("value")) {
36+
value = jasObj.get("value").toString();
37+
}
38+
if(key != null && value != null) {
39+
MagentoAttribute ma = new MagentoAttribute();
40+
ma.setAttribute_code(key);
41+
ma.setValue(value);
42+
result.add(ma);
43+
}
44+
}
45+
}
46+
} else if(obj instanceof JSONObject) {
47+
JSONObject jo = (JSONObject)obj;
48+
for(String key : jo.keySet()) {
49+
MagentoAttribute ma = new MagentoAttribute();
50+
ma.setAttribute_code(key);
51+
Object joObj = jo.get(key);
52+
if(joObj instanceof JSONObject) {
53+
ma.setValue(((JSONObject)joObj).toJSONString());
54+
} else if(joObj instanceof JSONArray) {
55+
ma.setValue(((JSONArray)joObj).toJSONString());
56+
} else {
57+
ma.setValue(joObj.toString());
58+
}
59+
result.add(ma);
60+
}
61+
}
62+
63+
64+
65+
return (T)result;
66+
}
67+
68+
@Override
69+
public int getFastMatchToken() {
70+
return 0;
71+
}
72+
73+
/*
74+
@Override
75+
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
76+
int features) throws IOException {
77+
Integer value = (Integer) object;
78+
String text = value + "元";
79+
serializer.write(text);
80+
}*/
81+
82+
83+
}

src/main/java/com/github/chen0040/magento/services/MagentoProductManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public Product getProductBySku(String sku) {
6464
return null;
6565
}
6666

67-
System.out.println(json);
67+
System.out.println("output: " + json);
6868

6969
return JSON.parseObject(json, Product.class);
7070
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.chen0040.magento.models;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.BufferedReader;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
13+
public class ProductJsonUnitTest {
14+
15+
private static final Logger logger = LoggerFactory.getLogger(ProductJsonUnitTest.class);
16+
17+
@Test
18+
public void testJsonDeserialization() {
19+
String json = readStream(ProductJsonUnitTest.class.getClassLoader().getResourceAsStream("product.json"));
20+
logger.info("json: {}", json);
21+
Product product = JSON.parseObject(json, Product.class);
22+
23+
logger.info("sku: {}", product.getSku());
24+
for(MagentoAttribute ma : product.getCustom_attributes()){
25+
logger.info("custom attribute: key = {}, value = {}", ma.getAttribute_code(), ma.getValue());
26+
}
27+
for(MagentoAttribute ma : product.getExtension_attributes()){
28+
logger.info("extension attribute: key = {}, value = {}", ma.getAttribute_code(), ma.getValue());
29+
}
30+
}
31+
32+
private String readStream(InputStream is) {
33+
StringBuilder sb = new StringBuilder();
34+
try(BufferedReader reader = new BufferedReader(new InputStreamReader(is))){
35+
String line;
36+
while((line = reader.readLine()) != null) {
37+
sb.append(line);
38+
}
39+
}catch(IOException ioex){
40+
logger.error("Failed to read stream", ioex);
41+
}
42+
return sb.toString();
43+
}
44+
45+
}

src/test/resources/product.json

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
{
2+
"id": 188,
3+
"sku": "8544596",
4+
"name": "BATA-8544596",
5+
"attribute_set_id": 4,
6+
"price": 0,
7+
"status": 1,
8+
"visibility": 4,
9+
"type_id": "configurable",
10+
"created_at": "2018-01-23 12:25:49",
11+
"updated_at": "2018-01-23 12:25:49",
12+
"weight": 0.5,
13+
"extension_attributes": {
14+
"configurable_product_options": [{
15+
"id": 77,
16+
"attribute_id": "93",
17+
"label": "Color",
18+
"position": 0,
19+
"values": [{
20+
"value_index": 10
21+
}],
22+
"product_id": 188
23+
}, {
24+
"id": 78,
25+
"attribute_id": "135",
26+
"label": "Size",
27+
"position": 1,
28+
"values": [{
29+
"value_index": 4
30+
}, {
31+
"value_index": 5
32+
}, {
33+
"value_index": 6
34+
}, {
35+
"value_index": 7
36+
}, {
37+
"value_index": 8
38+
}],
39+
"product_id": 188
40+
}],
41+
"configurable_product_links": [183, 184, 185, 186, 187]
42+
},
43+
"product_links": [],
44+
"options": [],
45+
"media_gallery_entries": [{
46+
"id": 305,
47+
"media_type": "image",
48+
"label": null,
49+
"position": 1,
50+
"disabled": false,
51+
"types": ["image", "small_image", "thumbnail", "swatch_image"],
52+
"file": "\/8\/5\/854-4596-a_1_2.jpg"
53+
}, {
54+
"id": 306,
55+
"media_type": "image",
56+
"label": null,
57+
"position": 2,
58+
"disabled": false,
59+
"types": [],
60+
"file": "\/8\/5\/854-4596-b_2.jpg"
61+
}, {
62+
"id": 307,
63+
"media_type": "image",
64+
"label": null,
65+
"position": 3,
66+
"disabled": false,
67+
"types": [],
68+
"file": "\/8\/5\/854-4596-c_2.jpg"
69+
}, {
70+
"id": 308,
71+
"media_type": "image",
72+
"label": null,
73+
"position": 4,
74+
"disabled": false,
75+
"types": [],
76+
"file": "\/8\/5\/854-4596-d_2.jpg"
77+
}, {
78+
"id": 309,
79+
"media_type": "image",
80+
"label": null,
81+
"position": 5,
82+
"disabled": false,
83+
"types": [],
84+
"file": "\/8\/5\/854-4596-e_2.jpg"
85+
}],
86+
"tier_prices": [],
87+
"custom_attributes": [{
88+
"attribute_code": "meta_title",
89+
"value": "BATA-8544596"
90+
}, {
91+
"attribute_code": "meta_keyword",
92+
"value": "BATA-8544596"
93+
}, {
94+
"attribute_code": "meta_description",
95+
"value": " "
96+
}, {
97+
"attribute_code": "image",
98+
"value": "\/8\/5\/854-4596-a_1_2.jpg"
99+
}, {
100+
"attribute_code": "small_image",
101+
"value": "\/8\/5\/854-4596-a_1_2.jpg"
102+
}, {
103+
"attribute_code": "thumbnail",
104+
"value": "\/8\/5\/854-4596-a_1_2.jpg"
105+
}, {
106+
"attribute_code": "category_ids",
107+
"value": ["83", "97", "121"]
108+
}, {
109+
"attribute_code": "options_container",
110+
"value": "container2"
111+
}, {
112+
"attribute_code": "required_options",
113+
"value": "1"
114+
}, {
115+
"attribute_code": "has_options",
116+
"value": "1"
117+
}, {
118+
"attribute_code": "url_key",
119+
"value": "bata-8544596"
120+
}, {
121+
"attribute_code": "swatch_image",
122+
"value": "\/8\/5\/854-4596-a_1_2.jpg"
123+
}, {
124+
"attribute_code": "tax_class_id",
125+
"value": "2"
126+
}, {
127+
"attribute_code": "gift_message_available",
128+
"value": "2"
129+
}, {
130+
"attribute_code": "size",
131+
"value": "4"
132+
}]
133+
}

0 commit comments

Comments
 (0)