* Encodes a double value into JSON format and writes it to the output stream. * * This function checks if the double value can be safely cast to an integer or unsigned integer type * without loss of precision. If it can, it will serialize it as such; otherwise, it will serialize * it as a double. This is particularly useful for ensuring that values like 0.0 are serialized as 0, * which can be
| 204 | * @param value The double value to encode as JSON. |
| 205 | */ |
| 206 | void JsonEncoder::EncodeNumber(double value) const |
| 207 | { |
| 208 | try { |
| 209 | if (value < 0) { |
| 210 | if (auto ll(boost::numeric_cast<nlohmann::json::number_integer_t>(value)); ll == value) { |
| 211 | EncodeNlohmannJson(ll); |
| 212 | return; |
| 213 | } |
| 214 | } else if (auto ull(boost::numeric_cast<nlohmann::json::number_unsigned_t>(value)); ull == value) { |
| 215 | EncodeNlohmannJson(ull); |
| 216 | return; |
| 217 | } |
| 218 | // If we reach this point, the value cannot be safely cast to a signed or unsigned integer |
| 219 | // type because it would otherwise lose its precision. If the value was just too large to fit |
| 220 | // into the above types, then boost will throw an exception and end up in the below catch block. |
| 221 | // So, in either case, serialize the number as-is without any casting. |
| 222 | } catch (const boost::bad_numeric_cast&) {} |
| 223 | |
| 224 | EncodeNlohmannJson(value); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Writes a string to the underlying output stream. |
nothing calls this directly
no outgoing calls
no test coverage detected