diff --git a/json.h b/json.h index aa3a316..aeaa7a6 100644 --- a/json.h +++ b/json.h @@ -41,5 +41,13 @@ void json_add_double(Json* json, double value); void json_add_bool(Json* json, bool value); void json_add_null(Json* json); +#define json_add_number(json, value) \ + _Generic( \ + (value), \ + int: json_add_long((json), (long)value), \ + long: json_add_long((json), value), \ + float: json_add_double((json), (double)value), \ + double: json_add_double((json), value)) + #endif // JSON_H diff --git a/tests/json_tests_simple.c b/tests/json_tests_simple.c index b9b1feb..5d6c02b 100644 --- a/tests/json_tests_simple.c +++ b/tests/json_tests_simple.c @@ -64,6 +64,18 @@ void test_json_add_double() TEST_ASSERT_EQUAL_STRING("3.14", buffer); } +void test_json_add_number() +{ + json_add_number(&json, 3.14f); + fflush(tmp); + rewind(tmp); + + auto n = fread(buffer, 1, sizeof(buffer) - 1, tmp); + buffer[n] = '\0'; + + TEST_ASSERT_EQUAL_STRING("3.14", buffer); +} + void test_json_add_bool() { json_add_bool(&json, true);