Drop default-skip behavior from savejson write macros

Only reads should fall back to a default when a key is missing; writes
should always emit the field so the saved JSON is a complete record.
This commit is contained in:
2026-08-16 20:20:34 -05:00
parent 674f86b18a
commit 560c51cf27
3 changed files with 24 additions and 38 deletions
+22 -36
View File
@@ -56,15 +56,13 @@
if(!hasInt32(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes an int32_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes an int32_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeInt32(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_int(doc, object, key, (int64_t)(value))
#define writeInt32(key, value) \
yyjson_mut_obj_add_int(doc, object, key, (int64_t)(value))
/**
* Reads an int32_t from the current JSON object, falling back to the given
@@ -94,15 +92,13 @@
if(!hasUInt32(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes a uint32_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes a uint32_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeUInt32(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
#define writeUInt32(key, value) \
yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
/**
* Reads a uint32_t from the current JSON object, falling back to the given
@@ -132,15 +128,13 @@
if(!hasUInt8(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes a uint8_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes a uint8_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeUInt8(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
#define writeUInt8(key, value) \
yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
/**
* Reads a uint8_t from the current JSON object, falling back to the given
@@ -170,15 +164,13 @@
if(!hasInt64(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes an int64_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes an int64_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeInt64(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_sint(doc, object, key, (int64_t)(value))
#define writeInt64(key, value) \
yyjson_mut_obj_add_sint(doc, object, key, (int64_t)(value))
/**
* Reads an int64_t from the current JSON object, falling back to the given
@@ -208,15 +200,13 @@
if(!hasUInt64(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes a uint64_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes a uint64_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeUInt64(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
#define writeUInt64(key, value) \
yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(value))
/**
* Reads a uint64_t from the current JSON object, falling back to the given
@@ -246,15 +236,13 @@
if(!hasFloat(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes a float_t to the current JSON object, omitting the key entirely
* if it matches the given default.
* Writes a float_t to the current JSON object.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeFloat(key, value, def) \
if((value) != (def)) yyjson_mut_obj_add_real(doc, object, key, (double)(value))
#define writeFloat(key, value) \
yyjson_mut_obj_add_real(doc, object, key, (double)(value))
/**
* Reads a float_t from the current JSON object, falling back to the given
@@ -284,16 +272,14 @@
if(!hasString(key)) errorThrow("Save JSON missing '%s' key", key)
/**
* Writes a string to the current JSON object, omitting the key entirely if
* it matches the given default. The string is copied, so it does not need
* to outlive the JSON document.
* Writes a string to the current JSON object. The string is copied, so it
* does not need to outlive the JSON document.
*
* @param key The key to write to.
* @param value The value to write.
* @param def The default value; if value equals this, the key is omitted.
*/
#define writeString(key, value, def) \
if(!stringEquals((value), (def))) yyjson_mut_obj_add_strcpy(doc, object, key, value)
#define writeString(key, value) \
yyjson_mut_obj_add_strcpy(doc, object, key, value)
/**
* Reads a string from the current JSON object into dest, falling back to
+1 -1
View File
@@ -25,7 +25,7 @@ errorret_t saveSettingsWriteJSON(
assertNotNull(doc, "Doc cannot be null");
assertNotNull(object, "Object cannot be null");
writeInt32("someSetting", settings->someSetting, 0);
writeInt32("someSetting", settings->someSetting);
errorOk();
}
+1 -1
View File
@@ -31,7 +31,7 @@ errorret_t saveSlotWriteJSON(
assertNotNull(doc, "Doc cannot be null");
assertNotNull(object, "Object cannot be null");
writeString("name", slot->name, "");
writeString("name", slot->name);
writeTime("time", slot->time);
errorOk();