Skip to main content

Generate Patch JSON

Description

The generate_patch_json function takes a list of patch operations and serializes them into a JSON string, converting all values to strings in the process.

Function Signature:

def generate_patch_json(operations: list) -> str:

Parameters

  • operations (list): A list of patch operation objects (e.g., instances of PatchOperation) that should be converted to JSON format.

Returns

  • str: A JSON-formatted string representing the patch operations with all values cast to strings.

Example Usage

operations = []
add_patch_operation(operations, "firmwareVersion", "1.2.3")
add_patch_operation(operations, "deviceID", 101)

json_string = generate_patch_json(operations)
print(json_string)

Behavior

  • Uses vars(op) to convert each operation object to a dictionary.
  • Converts all values in each operation to strings.
  • Serializes the entire list of dictionaries into a JSON-formatted string.

Error Handling

  • This function assumes valid input and does not include exception handling internally.
  • If any object in operations is not a class instance with accessible attributes (i.e., does not work with vars()), it may raise a TypeError.