I want to write an encoder for a format that only ...
# serialization
b
I want to write an encoder for a format that only supports lists with elements of a single type. What's a good way to ensure that's the case when encoding collections and their elements? Right now I'm thinking of having a check in
beginCollection
to require
descriptor.elementsCount == 1
, but I'm not sure if that guarantees homogeneity in all cases. (I'm pretty sure it'll work for the standard collection serializers, but besides that I'm not sure)
I ended up recording the type of the first list element, and for subsequent elements, checking that the type is the same as the first. You might have to do some additional work if the list size is 0, figuring out the list type from the descriptor, but my format has a sort of
Nothing
type that can be used for empty lists, so luckily I don't have to bother with that :)