From d0f560799820df4237c33417ce7765eafc9556d4 Mon Sep 17 00:00:00 2001 From: Tomas Tulka Date: Thu, 8 Jun 2017 08:03:14 +0200 Subject: [PATCH 1/2] a comment added to explain the use of HashMap to avoid misconception of contributors about using HashMap to implement a JSON object as a unordered collection by the definition --- JSONObject.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index ff4acfa..f974430 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -164,6 +164,7 @@ public class JSONObject { * Construct an empty JSONObject. */ public JSONObject() { + // HashMap is used on purpose to ensure that elements are unordered this.map = new HashMap(); } @@ -216,7 +217,7 @@ public class JSONObject { key = x.nextValue().toString(); } -// The key is followed by ':'. + // The key is followed by ':'. c = x.nextClean(); if (c != ':') { @@ -224,7 +225,7 @@ public class JSONObject { } this.putOnce(key, x.nextValue()); -// Pairs are separated by ','. + // Pairs are separated by ','. switch (x.nextClean()) { case ';': From 246350bbcdf88e6f473bdaaed2f30e50932c931d Mon Sep 17 00:00:00 2001 From: Tomas Tulka Date: Fri, 9 Jun 2017 09:00:17 +0200 Subject: [PATCH 2/2] comment added to explain the reason that JSON object is unordered to avoid implementators' misconceptions and tries to reimplement the JSON object to keep the elements order --- JSONObject.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index f974430..e4b0bfa 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -164,7 +164,12 @@ public class JSONObject { * Construct an empty JSONObject. */ public JSONObject() { - // HashMap is used on purpose to ensure that elements are unordered + // HashMap is used on purpose to ensure that elements are unordered by + // the specification. + // JSON tends to be a portable transfer format to allows the container + // implementations to rearrange their items for a faster element + // retrieval based on associative access. + // Therefore, an implementation mustn't rely on the order of the item. this.map = new HashMap(); }