[v6][Enhancement] DeserializationError is not switch() Friendly
Created by: jeffsf
Using a DeserializationError
as a switch condition results in compiler warnings that the "switch condition has boolean value" and subsequent reduction to "odds/evens" of the cases. Looking at the class definition, I am guessing that at least clang
selects operator bool()
rather than the Code
as I had expected.
One work-around is a functionally equivalent if
tree.
To answer the "obvious" line of questions around the necessity or purpose for the code segment below, I'm fighting for bytes of RAM in an AVR device and put all my strings into program memory. The UnifiedErrors
class lets me manage all error reporting in one place, with the related strings stored in program memory. The 65 bytes of strings in DeserializationError::c_str()
are "huge" compared to the free RAM I have available.
DeserializationError derr = deserializeJson(doc, str.c_str());
if ( derr ) {
switch ( derr ) {
case DeserializationError::IncompleteInput :
parsedDto.error = UnifiedErrors::kJsonParseIncompleteInput;
break;
case DeserializationError::InvalidInput :
parsedDto.error = UnifiedErrors::kJsonParseInvalidInput;
break;
case DeserializationError::NoMemory :
parsedDto.error = UnifiedErrors::kJsonParseNoMemory;
break;
case DeserializationError::NotSupported :
parsedDto.error = UnifiedErrors::kJsonParseNotSupported;
break;
case DeserializationError::TooDeep :
parsedDto.error = UnifiedErrors::kJsonParseTooDeep;
break;
}
warning: switch condition has boolean value [-Wswitch-bool]
switch ( derr ) {
^ ~~~~
warning: overflow converting case value to switch condition type (5 to 1) [-Wswitch]
case DeserializationError::TooDeep :
^
warning: overflow converting case value to switch condition type (4 to 0) [-Wswitch]
case DeserializationError::NotSupported :
^
[...]