-
Notifications
You must be signed in to change notification settings - Fork 3
reboot - Map and key insertion order #8
Comments
Maybe the term I was looking for is referential transparency. Random ordering is a side effect. |
See Argonaut's |
@rossabaker yea. I didn't know about their implementation, but it's what I had in mind last night. private[argonaut] case class JsonObjectInstance(
fieldsMap: Map[JsonField, Json] = Map.empty,
orderedFields: Vector[JsonField] = Vector.empty
) |
Are you depending on this to get a round trip from It seems that one could devise a hash that operates on JSON structure rather than a hashing a serialized form. I'm not up on my pickling, but would that work better for you than depending on order? |
For the record this is the discussion we've so far had on this topic in spray-json: Still, for me one of the core design goals of a common AST is perf potential. |
I want
We could implement special hashing and equality, etc but eventually the random ordering becomes observable via |
I like that idea. |
I agree that maintaining member order has all sorts of practical advantages. |
Ok looks we will go for 2, although ideally we would have to provide our own One of the reasons for using a Will have a look at argonaut tonight |
@eed3si9n The obvious side effect of storing another internal structure is memory usage. I am not sure how significant this is, but I still have a nagging thought in the back of my head regarding this. A really large It also seems that from the comments mentioned in spray/spray-json#119, most people argued to just use a EDIT: My comment in gitter
|
There is now an implementation ( If this is still a problem (i.e. in regards to using |
Stackoverflow question is here http://stackoverflow.com/questions/31635076/scala-immutable-map-like-datastructure-that-has-constant-effective-constant-look
@eed3si9n raised a potential issue, and that is the fact that
JObject
uses aMap
, which means that there is no specified ordering on keys. TheJSON
specification says that you shouldn't rely on the object having any specific order, however this does have practical implications.Quoting what @eed3si9n said in gitter
The problem is, that
Scala
stdlib
does not have any immutable map like data structure which preserves key ordering that also hasc
oreC
lookup time. There isLinkedMap
, which is aMap
backed by aLinkedList
, however that has Linear lookup time. There isLinkedHashMap
which has a constant lookup time, however itsmutable
. There are some solutionsScala
's collection library, so it can be treated to be exactly the same as a normalMap
) which has all of the properties we needVector
as well as aMap
so we can have some concept of orderingSortedMap
. We still can't guarantee that that the key order will be the same according to insertion, however we can guarantee that the keys will beSorted
, which means that if youSHA-1
twoJObject
's that have the same contents, they will be equal. It also means that when you serialize, we can guarantee that it will always be ordered. EDIT: Default implementation ofSortedMap
is aTreeMap
, which haslog
lookup time.Map
to aSortedMap
by someOrdering
, which means that if you do something like what @eed3si9n above, you would convert yourJObject
'sMap
to aSortedMap
before doing aSHA-1
. The nicest way to implement this is by having somesorted
method on aJValue
, which will replace all internalJObject
s to something likeJObjectSorted
and return aJValueSorted
. You can then serialize aJValueSorted
if you need this sought of functionality.Taking into account all theoretical/practical/time considerations, I think 4 is most realistic. It fixes @eed3si9n issue indirectly, while still maintaining immutability and
c
/eC
lookup time.EDIT: The implementation for 3 (which uses a
SortedMap
), has only one implementation inScala
'sstdlib
, which is aTreeMap
.TreeMap
has alog
lookup time, which is definitely is not as bad asn
, but its not ac
/eC
. Looks like we may need to roll our own data structure, if possible.The text was updated successfully, but these errors were encountered: