Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A ArduinoJson
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 24
    • Issues 24
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Benoît Blanchon
  • ArduinoJson
  • Issues
  • #846
Closed
Open
Issue created Nov 08, 2018 by Administrator@rootContributor

[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 :
                     ^
[...]
Assignee
Assign to
Time tracking