10Duke Enterprise C++ SDK
Loading...
Searching...
No Matches
SimpleJSONNumber.h
1#ifndef TENDUKE_JSON_SIMPLEJSONNUMBER_H
2#define TENDUKE_JSON_SIMPLEJSONNUMBER_H
3
4#include "./SimpleJSONElement.h"
5#include "json/JSONNumber.h"
6
7#include <cmath>
8#include <cstdint>
9#include <locale>
10#include <sstream>
11
12namespace tenduke { namespace json {
13
19class SimpleJSONNumber : public virtual ::tenduke::json::JSONNumber, public ::tenduke::json::SimpleJSONElement
20{
21public:
22 explicit SimpleJSONNumber(double value)
23 : SimpleJSONElement(::tenduke::json::JSONElement::NUMBER),
24 kind(Kind::FLOATING_POINT), value(value), signedValue(0), unsignedValue(0)
25 {
26 }
27
28 explicit SimpleJSONNumber(std::int64_t value)
29 : SimpleJSONElement(::tenduke::json::JSONElement::NUMBER),
30 kind(Kind::SIGNED_INTEGER), value(static_cast<double>(value)), signedValue(value), unsignedValue(0)
31 {
32 }
33
34 explicit SimpleJSONNumber(std::uint64_t value)
35 : SimpleJSONElement(::tenduke::json::JSONElement::NUMBER),
36 kind(Kind::UNSIGNED_INTEGER), value(static_cast<double>(value)), signedValue(0), unsignedValue(value)
37 {
38 }
39
40 double getValue() const override
41 {
42 return this->value;
43 }
44
45 std::string asJSON() const override
46 {
47 switch (kind) {
48 case Kind::SIGNED_INTEGER:
49 return std::to_string(signedValue);
50 case Kind::UNSIGNED_INTEGER:
51 return std::to_string(unsignedValue);
52 case Kind::FLOATING_POINT:
53 default:
54 return formatDouble(value);
55 }
56 }
57
58private:
59 enum class Kind {FLOATING_POINT, SIGNED_INTEGER, UNSIGNED_INTEGER};
60
65 static std::string formatDouble(const double value)
66 {
67 if (! std::isfinite(value)) {
68 return "null";
69 }
70 // Compare as double before converting: converting a value out of the range of std::int64_t is undefined
71 if (std::trunc(value) == value && value >= -9223372036854775808.0 && value < 9223372036854775808.0) {
72 return std::to_string(static_cast<std::int64_t>(value));
73 }
74 std::string formatted;
75 for (int precision = 15; precision <= 17; ++precision) {
76 std::ostringstream stream;
77 stream.imbue(std::locale::classic());
78 stream.precision(precision);
79 stream << value;
80 formatted = stream.str();
81
82 std::istringstream parsed(formatted);
83 parsed.imbue(std::locale::classic());
84 double parsedValue = 0;
85 parsed >> parsedValue;
86 if (parsedValue == value) {
87 break;
88 }
89 }
90 return formatted;
91 }
92
93 const Kind kind;
94 const double value;
95 const std::int64_t signedValue;
96 const std::uint64_t unsignedValue;
97};
98
99}} // tenduke
100
101#endif //TENDUKE_JSON_SIMPLEJSONNUMBER_H
Represents JSON number.
Definition JSONNumber.h:12
Definition SimpleJSONElement.h:11
std::string asJSON() const override
Serializes the element as JSON.
Definition SimpleJSONNumber.h:45
double getValue() const override
Returns the value.
Definition SimpleJSONNumber.h:40
JSON support.
Definition JSONArray.h:10
Root for classes, functions and globals of 10Duke C++ Client.
Definition APIRequest.h:4