fwcd / fwcd/swift-binary-coder
Cannot re-read input or amend output
- Lingua principale
- Swift
- Stelle
- 4
- Fork
- 1
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
The standard encoder/decoder implementations provided by Apple (e.g. JSONDecoder) allow you to take multiple attempts at reading data in different formats. For example, you could try reading a field as a String, and then handle the error if that fails and try again to read it as an Int.
Similarly you can write to the same output object multiple times with difference objects to get an aggregate output, as in this example: https://gist.github.com/nicklockwood/ff51117ac8139248507ecc84a1ed7fed
Unfortunately this doesn't work with your current BinaryEncoder implementation.
For decoding the problem appears to be that the BinaryDecodingState is shared between containers, which means that once one container has read a value, it's permanently popped from the internal Data object and cannot be read again by another container. This seems like it should be straightforward to fix by using a resettable cursor variable for the current data read position, but I've not attempted to work out a detailed implementation.
For encoding I guess the problem is that you write out the data immediately to the output, making it impossible to add additional fields to the same object using another container. I'm not sure what the solution is there, although this seems like a less common requirement than multiple reads (I only just discovered that it was even possible with JSONDecoder today!).
In any case, it would be great to fix the re-reading limitation in BinaryDecoder at least.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.