I am using PHP, but question is not entirely PHP specific.
I've got a need to serialize object, current I am using JSON format and this could be example of serialized object: {"22":{"val":"e","gIds":["48"]},"23":{"val":"e","gIds":["235"]}}
Given this format I can simply use PHP's : $array = json_decode($string, true)
, then I can use either isset($array[22])
or array_key_exists(22, $array)
for quick lookups, which I think would be equivalent to hashmap
/ dictionary
in different languages.
Unfortunately the JSON format by default doesn't keep the order of properties within the object (https://stackoverflow.com/a/7214312/2263395). The order might be consistent in the same version of PHP, but it's not guaranteed behavior according to JSON standard.
I could make it a list of nested objects: [{"22":{"val":"e","gIds":["48"]}},{"23":{"val":"e","gIds":["235"]}}]
or even move the id
inside: [{"id": "22","val":"e","gIds":["48"]},{"id": "23","val":"e","gIds":["235"]}]
That way I can preserve order, however each I can no longer use the quick lookup functions like: isset
or array_key_exists
before I create a hashmap. For JSONs having thousands of items, I would have to iterate that many times in order to prepare a hashmap, so I can use the quick lookup function.
Can I have the both order
and speed
, is there any format that could do that?
Speaking of PHP options is there any performant competitor?