From 7d5c863e01b4a70d5ef3614b91694cf080a420c9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 22 Jan 2024 12:02:54 +0200 Subject: Add comparison --- libbuild2/json.hxx | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'libbuild2/json.hxx') diff --git a/libbuild2/json.hxx b/libbuild2/json.hxx index 816b55c..6fc633c 100644 --- a/libbuild2/json.hxx +++ b/libbuild2/json.hxx @@ -26,9 +26,14 @@ namespace build2 // string and value. The latter allows us to use the JSON value itself as an // element of a container. // - enum class json_type + // Note also that we don't assume that object members are in a sorted order + // (but do assume there are no duplicates). However, we could add an + // argument to signal that this is the case to speed up some functions, for + // example, compare(). + // + enum class json_type: uint8_t { - null, + null, // Note: keep first for comparison. boolean, signed_number, unsigned_number, @@ -222,6 +227,21 @@ namespace build2 case json_type::object: container.~container_type (); break; } } + + // Note that values of different types are never equal, except for + // signed/unsigned numbers. Null is equal to null and is less than any + // other value. Arrays are compared lexicographically. Object members are + // considered in the lexicographically-compared name-ascending order (see + // RFC8785). An absent member is less than a present member (even if it's + // null). + // + // Note that while it doesn't make much sense to compare members to + // non-members, we allow it with a non-member always being less than a + // member (even if null), unless ignore_name is true, in which case member + // names are ignored. + // + int + compare (const json_value&, bool ignore_name = false) const; }; // Throws invalid_json_output. -- cgit v1.1