-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Picojson] Let the key of objects in json be ordered by default
Previously picojson define `object` as an alias of `std::unordered_map`. That means when parsing json, the order of keys in objects are uncertain and dependent on implementation. This makes it inconvenient for certain applications, e.g. in LLM generation output, we wish the order of keys the same as the order in the json file. This PR implements a ordered hashmap `ordered_hashmap` that 1) maintains the order in which the elements are inserted, and 2) have the same interface as `std::unordered_map`. Picojson will define object as an alias of `ordered_hashmap`, so the order of the input json is maintained when parsing. Macro `PICOJSON_USE_ORDERED_OBJECT` controls whether object uses the ordered version or the unordered version. It is set by default.
- Loading branch information
Showing
2 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#include <cassert> | ||
#include <sstream> | ||
|
||
#include "picojson.h" | ||
|
||
using picojson::object_with_ordered_keys; | ||
|
||
void test_constructor() { | ||
object_with_ordered_keys obj; | ||
obj["foo"] = picojson::value(true); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo"})); | ||
|
||
object_with_ordered_keys obj1{{"foo", picojson::value(true)}, {"bar", picojson::value(false)}}; | ||
assert((obj1.ordered_keys() == std::vector<std::string>{"foo", "bar"})); | ||
|
||
object_with_ordered_keys obj2(obj1); | ||
assert((obj2.ordered_keys() == std::vector<std::string>{"foo", "bar"})); | ||
|
||
object_with_ordered_keys obj3(std::move(obj2)); | ||
assert((obj3.ordered_keys() == std::vector<std::string>{"foo", "bar"})); | ||
|
||
obj = obj3; | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo", "bar"})); | ||
} | ||
|
||
void test_modifier() { | ||
object_with_ordered_keys obj{{"foo", picojson::value(true)}, {"bar", picojson::value(false)}}; | ||
obj.insert({"abc", picojson::value(false)}); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo", "bar", "abc"})); | ||
obj.emplace("def", picojson::value(true)); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo", "bar", "abc", "def"})); | ||
obj.insert({"abc", picojson::value(true)}); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo", "bar", "abc", "def"})); | ||
auto it = obj.find("abc"); | ||
it = obj.erase(it); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"foo", "bar", "def"})); | ||
obj.erase("foo"); | ||
assert((obj.ordered_keys() == std::vector<std::string>{"bar", "def"})); | ||
obj.clear(); | ||
assert((obj.ordered_keys() == std::vector<std::string>{})); | ||
} | ||
|
||
int main() { | ||
test_constructor(); | ||
test_modifier(); | ||
return 0; | ||
} |