Customizing the output of serde-json

I'm subprocessing to the hidutil command in my kb-remap crate. hidutil takes what is maybe JSON5 or some custom superset of JSON that supports hex literals as arguments. For example

hidutil property \
    --matching '{"VendorID":0x5ac,"ProductID":0x0342}' \
    --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x70000002a}]}'

Currently my program constructs these arguments by serializing a struct using serde_json. However, given that my program has functionality that allows you to inspect the command that will be subprocessed I would like to output the numbers in the JSON as hex literals.

Is this possible to do with serde and serde_json?

I believe you can control the format to serialize using a custom Formatter. just be careful, because the default deserializer might not recognize your custom format, you might need to define your custom Visitor for deserialization at the same time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.