agronholm / cbor2

Python CBOR (de)serializer with extensive tag support
MIT License
226 stars 59 forks source link

Annotated hex callback #237

Open davaya opened 5 months ago

davaya commented 5 months ago

Things to check first

Feature description

Please add the ability to register a visitor callback for each invocation and return of the parser or serializer.

Use case

This would allow the callback function to record the type, its stack depth, and its position in the byte stream, enabling generation of the "Annotated hex" view of a CBOR blob:

Hex: 82A1011901F4C077313938352D30342D31325432333A32303A35302E35325A

Annotated Hex:

82                                      # array(2)
   A1                                   # map(1)
      01                                # unsigned(1)
      19 01F4                           # unsigned(500)
   C0                                   # tag(0)
      77                                # text(23)
         313938352D30342D31325432333A32303A35302E35325A # "1985-04-12T23:20:50.52Z"