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:
+22
-36
@@ -56,15 +56,13 @@
|
|||||||
if(!hasInt32(key)) errorThrow("Save JSON missing '%s' key", key)
|
if(!hasInt32(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes an int32_t to the current JSON object, omitting the key entirely
|
* Writes an int32_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeInt32(key, value, def) \
|
#define writeInt32(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_int(doc, object, key, (int64_t)(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
|
* 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)
|
if(!hasUInt32(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a uint32_t to the current JSON object, omitting the key entirely
|
* Writes a uint32_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeUInt32(key, value, def) \
|
#define writeUInt32(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(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
|
* 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)
|
if(!hasUInt8(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a uint8_t to the current JSON object, omitting the key entirely
|
* Writes a uint8_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeUInt8(key, value, def) \
|
#define writeUInt8(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(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
|
* 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)
|
if(!hasInt64(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes an int64_t to the current JSON object, omitting the key entirely
|
* Writes an int64_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeInt64(key, value, def) \
|
#define writeInt64(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_sint(doc, object, key, (int64_t)(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
|
* 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)
|
if(!hasUInt64(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a uint64_t to the current JSON object, omitting the key entirely
|
* Writes a uint64_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeUInt64(key, value, def) \
|
#define writeUInt64(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_uint(doc, object, key, (uint64_t)(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
|
* 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)
|
if(!hasFloat(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a float_t to the current JSON object, omitting the key entirely
|
* Writes a float_t to the current JSON object.
|
||||||
* if it matches the given default.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeFloat(key, value, def) \
|
#define writeFloat(key, value) \
|
||||||
if((value) != (def)) yyjson_mut_obj_add_real(doc, object, key, (double)(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
|
* 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)
|
if(!hasString(key)) errorThrow("Save JSON missing '%s' key", key)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a string to the current JSON object, omitting the key entirely if
|
* Writes a string to the current JSON object. The string is copied, so it
|
||||||
* it matches the given default. The string is copied, so it does not need
|
* does not need to outlive the JSON document.
|
||||||
* to outlive the JSON document.
|
|
||||||
*
|
*
|
||||||
* @param key The key to write to.
|
* @param key The key to write to.
|
||||||
* @param value The value to write.
|
* @param value The value to write.
|
||||||
* @param def The default value; if value equals this, the key is omitted.
|
|
||||||
*/
|
*/
|
||||||
#define writeString(key, value, def) \
|
#define writeString(key, value) \
|
||||||
if(!stringEquals((value), (def))) yyjson_mut_obj_add_strcpy(doc, object, key, value)
|
yyjson_mut_obj_add_strcpy(doc, object, key, value)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a string from the current JSON object into dest, falling back to
|
* Reads a string from the current JSON object into dest, falling back to
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ errorret_t saveSettingsWriteJSON(
|
|||||||
assertNotNull(doc, "Doc cannot be null");
|
assertNotNull(doc, "Doc cannot be null");
|
||||||
assertNotNull(object, "Object cannot be null");
|
assertNotNull(object, "Object cannot be null");
|
||||||
|
|
||||||
writeInt32("someSetting", settings->someSetting, 0);
|
writeInt32("someSetting", settings->someSetting);
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ errorret_t saveSlotWriteJSON(
|
|||||||
assertNotNull(doc, "Doc cannot be null");
|
assertNotNull(doc, "Doc cannot be null");
|
||||||
assertNotNull(object, "Object cannot be null");
|
assertNotNull(object, "Object cannot be null");
|
||||||
|
|
||||||
writeString("name", slot->name, "");
|
writeString("name", slot->name);
|
||||||
writeTime("time", slot->time);
|
writeTime("time", slot->time);
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
|
|||||||
Reference in New Issue
Block a user