diff options
| author | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2026-04-23 18:16:57 +0200 |
| commit | 0232b991cd7d8e3a2114ea30e4591dd3e7b65c36 (patch) | |
| tree | 94730e7594fd09ae1fa820391ce311f6daf13905 /src/zencore/compactbinaryjson.cpp | |
| parent | Fix forward declaration order for s_GotSigWinch and SigWinchHandler (diff) | |
| parent | trace: declare Region event name fields as AnsiString (#1012) (diff) | |
| download | archived-zen-sb/zen-help.tar.xz archived-zen-sb/zen-help.zip | |
Merge branch 'main' into sb/zen-helpsb/zen-help
- Combine HelpCommand (this branch) with HistoryCommand (main) in zen CLI dispatcher
- Keep filter-aware TuiPickOne rewrite; adopt main's ASCII arrow glyphs in doc comment
Diffstat (limited to 'src/zencore/compactbinaryjson.cpp')
| -rw-r--r-- | src/zencore/compactbinaryjson.cpp | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/zencore/compactbinaryjson.cpp b/src/zencore/compactbinaryjson.cpp index da560a449..5bfbd5e3e 100644 --- a/src/zencore/compactbinaryjson.cpp +++ b/src/zencore/compactbinaryjson.cpp @@ -11,6 +11,8 @@ #include <zencore/testing.h> #include <fmt/format.h> +#include <cmath> +#include <limits> #include <vector> ZEN_THIRD_PARTY_INCLUDES_START @@ -570,13 +572,37 @@ private: break; case Json::Type::NUMBER: { - if (FieldName.empty()) - { - Writer.AddFloat(Json.number_value()); + // If the JSON number has no fractional part and fits in an int64, + // store it as an integer so that AsInt32/AsInt64 work without + // requiring callers to go through AsFloat. + double Value = Json.number_value(); + double IntPart; + bool IsIntegral = (std::modf(Value, &IntPart) == 0.0) && + Value >= static_cast<double>(std::numeric_limits<int64_t>::min()) && + Value <= static_cast<double>(std::numeric_limits<int64_t>::max()); + + if (IsIntegral) + { + int64_t IntValue = static_cast<int64_t>(Value); + if (FieldName.empty()) + { + Writer.AddInteger(IntValue); + } + else + { + Writer.AddInteger(FieldName, IntValue); + } } else { - Writer.AddFloat(FieldName, Json.number_value()); + if (FieldName.empty()) + { + Writer.AddFloat(Value); + } + else + { + Writer.AddFloat(FieldName, Value); + } } } break; |