diff --git a/src/main/java/org/piwik/java/tracking/PiwikJsonObject.java b/src/main/java/org/piwik/java/tracking/PiwikJsonObject.java index 0106641..caeae4b 100644 --- a/src/main/java/org/piwik/java/tracking/PiwikJsonObject.java +++ b/src/main/java/org/piwik/java/tracking/PiwikJsonObject.java @@ -6,7 +6,7 @@ */ package org.piwik.java.tracking; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import javax.json.Json; @@ -22,7 +22,7 @@ * @author brettcsorba */ public class PiwikJsonObject{ - Map map = new HashMap<>(); + Map map = new LinkedHashMap<>(); /** * Gets the custom value stored at this custom key. @@ -77,11 +77,20 @@ public int size(){ *
* {@code {"1": ["key1", "value1"], "2": ["key2": "value2"]} }
*
- * could be produced. Note that there is no guarantee about the ordering of - * the produced JSON based. The following JSON is also valid and could also - * be produced:
+ * would be produced. The produced JSON will be ordered according to the + * order the values were put in. Removing an object will cause the values + * to backfill accordingly.
+ *
+ * For example, if the following values were put into the object
+ *
+ * {@code ("key1", "value1")}, {@code ("key2", "value2")}, and {@code ("key3", "value3")}
+ *
+ * and {@code ("key2", "value2") } was then removed, then
+ *
+ * {@code {"1": ["key1", "value1"], "2": ["key3": "value3"]} }
+ *
+ * would be produced. *
- * {@code {"1": ["key2", "value2"], "2": ["key1": "value1"]} }. * @return the JSON string representation of this object */ @Override diff --git a/src/test/java/org/piwik/java/tracking/PiwikJsonObjectTest.java b/src/test/java/org/piwik/java/tracking/PiwikJsonObjectTest.java index aed0fa2..0a840fc 100644 --- a/src/test/java/org/piwik/java/tracking/PiwikJsonObjectTest.java +++ b/src/test/java/org/piwik/java/tracking/PiwikJsonObjectTest.java @@ -67,8 +67,10 @@ public void testMethods(){ public void testToString(){ obj.put("key", "value"); obj.put("key2", "value2"); + obj.put("key3", "value3"); + obj.remove("key2"); - assertEquals("{\"1\":[\"key2\",\"value2\"],\"2\":[\"key\",\"value\"]}", obj.toString()); + assertEquals("{\"1\":[\"key\",\"value\"],\"2\":[\"key3\",\"value3\"]}", obj.toString()); } }