diff options
author | rtk0c <[email protected]> | 2023-06-13 16:46:47 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2023-06-13 16:46:47 -0700 |
commit | 21192a63e1411134b3096da4e5f9a511f913c9b9 (patch) | |
tree | c19ceb7fbdc9e0ce226e0dfb4d7a1132c52e7854 /source/10-common | |
parent | 32e74b9c81b57a9c0284f5374536439d581bc4b6 (diff) |
Changeset: 95 Change Uid's serialization format from JSON array to just a string
Diffstat (limited to 'source/10-common')
-rw-r--r-- | source/10-common/Uid.cpp | 28 | ||||
-rw-r--r-- | source/10-common/Uid.hpp | 4 |
2 files changed, 24 insertions, 8 deletions
diff --git a/source/10-common/Uid.cpp b/source/10-common/Uid.cpp index 7f8fd9d..58dfffd 100644 --- a/source/10-common/Uid.cpp +++ b/source/10-common/Uid.cpp @@ -34,21 +34,33 @@ std::string Uid::WriteString() { } void Uid::Read(const rapidjson::Value& value) { - assert(value.IsArray()); - assert(value.Size() == 2); - auto& upper = value[0]; - assert(upper.IsUint64()); - auto& lower = value[1]; - assert(lower.IsUint64()); + if (value.IsString()) { + ReadString(rapidjson::AsStringView(value)); + } else if (value.IsArray()) { + // Compatibility support + assert(value.Size() == 2); + auto& upper = value[0]; + assert(upper.IsUint64()); + auto& lower = value[1]; + assert(lower.IsUint64()); - this->upper = upper.GetUint64(); - this->lower = lower.GetUint64(); + this->upper = upper.GetUint64(); + this->lower = lower.GetUint64(); + } else { + assert(false); + } } void Uid::WriteInto(rapidjson::Value& value, rapidjson::Document& root) const { +#if BRUSSEL_Uid_WRITE_USE_ARRAY value.Reserve(2, root.GetAllocator()); value.PushBack((uint64_t)upper, root.GetAllocator()); value.PushBack((uint64_t)lower, root.GetAllocator()); +#else + char buf[256]; + int len = snprintf(buf, sizeof(buf), BRUSSEL_Uid_FORMAT_STR, upper, lower); + value.SetString(buf, len, root.GetAllocator()); +#endif } rapidjson::Value Uid::Write(rapidjson::Document& root) const { diff --git a/source/10-common/Uid.hpp b/source/10-common/Uid.hpp index 539de03..a691911 100644 --- a/source/10-common/Uid.hpp +++ b/source/10-common/Uid.hpp @@ -13,10 +13,14 @@ #define BRUSSEL_Uid_FORMAT_STR "%016" PRIx64 "-%016" PRIx64 #define BRUSSEL_Uid_FORMAT_EXPAND(uid) (uid).upper, (uid).lower +// Serialize Uid object as an array with two elements, instead of the simple string format +#define BRUSSEL_Uid_WRITE_USE_ARRAY 0 + struct Uid { uint64_t upper = 0; uint64_t lower = 0; + // Generate a random Uid static Uid Create(); bool IsNull() const; |