From 54bff682e7b0c855b412e3c097b01b434dd97058 Mon Sep 17 00:00:00 2001 From: MonkeybreadSoftware Date: Sun, 24 Sep 2023 16:32:53 +0200 Subject: [PATCH] Update basic_json.hpp Added compare for bigint/bigdec/bigfloat as double or exact text match. --- include/jsoncons/basic_json.hpp | 47 +++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/include/jsoncons/basic_json.hpp b/include/jsoncons/basic_json.hpp index 24a1049e18..e94c85e83a 100644 --- a/include/jsoncons/basic_json.hpp +++ b/include/jsoncons/basic_json.hpp @@ -3088,17 +3088,42 @@ namespace jsoncons { break; case json_storage_kind::short_string_value: case json_storage_kind::long_string_value: - switch (rhs.storage_kind()) - { - case json_storage_kind::short_string_value: - return as_string_view().compare(rhs.as_string_view()); - case json_storage_kind::long_string_value: - return as_string_view().compare(rhs.as_string_view()); - case json_storage_kind::json_const_pointer: - return compare(*(rhs.cast().value())); - default: - return static_cast(storage_kind()) - static_cast((int)rhs.storage_kind()); - } + switch (tag()) + { + case semantic_tag::bigint: + case semantic_tag::bigdec: + case semantic_tag::bigfloat: + { + // same text -> equal + if (rhs.storage_kind() == json_storage_kind::short_string_value || rhs.storage_kind() == json_storage_kind::long_string_value) + { + int compareString = as_string_view().compare(rhs.as_string_view()); + if (compareString == 0) + { + return 0; + } + } + + // compare big numbers as double + auto r = as() - rhs.as(); + return r == 0 ? 0 : (r < 0.0 ? -1 : 1); + } + default: + { + // compare regular text + switch (rhs.storage_kind()) + { + case json_storage_kind::short_string_value: + return as_string_view().compare(rhs.as_string_view()); + case json_storage_kind::long_string_value: + return as_string_view().compare(rhs.as_string_view()); + case json_storage_kind::json_const_pointer: + return compare(*(rhs.cast().value())); + default: + return static_cast(storage_kind()) - static_cast((int)rhs.storage_kind()); + } + } + } break; case json_storage_kind::byte_string_value: switch (rhs.storage_kind())