Skip to content

Commit 330b176

Browse files
committed
Resolve matomo-org#13. ClassCastException when trying to send PiwikRequest with EcommeceItem
1 parent c3f1ac2 commit 330b176

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/main/java/org/piwik/java/tracking/EcommerceItem.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.piwik.java.tracking;
88

99
import javax.json.Json;
10+
import javax.json.JsonArray;
1011
import javax.json.JsonArrayBuilder;
1112
import javax.json.JsonValue;
1213

@@ -129,18 +130,25 @@ public ValueType getValueType(){
129130
}
130131

131132
/**
132-
* Returns the value of this EcommerceItem as a JSON Object string.
133-
* @return JSON object string
133+
* Returns the value of this EcommerceItem as a JSON Array string.
134+
* @return this as a JSON array string
134135
*/
135136
@Override
136137
public String toString(){
138+
return toJsonArray().toString();
139+
}
140+
/**
141+
* Returns the value of this EcommerceItem as a JsonArray.
142+
* @return this as a JsonArray
143+
*/
144+
protected JsonArray toJsonArray(){
137145
JsonArrayBuilder ab = Json.createArrayBuilder();
138146
ab.add(sku);
139147
ab.add(name);
140148
ab.add(category);
141149
ab.add(price);
142150
ab.add(quantity);
143151

144-
return ab.build().toString();
152+
return ab.build();
145153
}
146154
}

src/main/java/org/piwik/java/tracking/PiwikJsonArray.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public String toString(){
5555
JsonArrayBuilder ab = Json.createArrayBuilder();
5656

5757
for (int x = 0; x < list.size(); ++x){
58-
ab.add(list.get(x));
58+
JsonValue value = list.get(x);
59+
if (value instanceof EcommerceItem){
60+
ab.add(((EcommerceItem)value).toJsonArray());
61+
}
62+
else{
63+
ab.add(value);
64+
}
5965
}
6066

6167
return ab.build().toString();

src/test/java/org/piwik/java/tracking/PiwikJsonArrayTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public void testAddGetSet(){
6363
@Test
6464
public void testToString(){
6565
array.add(JsonValue.TRUE);
66+
array.add(new EcommerceItem("a", "b", "c", 1.0, 2));
6667
array.add(JsonValue.FALSE);
6768

68-
assertEquals("[true,false]", array.toString());
69+
assertEquals("[true,[\"a\",\"b\",\"c\",1.0,2],false]", array.toString());
6970
}
7071

7172
}

0 commit comments

Comments
 (0)