Uploaded image for project: 'Couchbase Server'
  1. Couchbase Server
  2. MB-62469

Disable integer-conversion for enums in nlohmann::json

    XMLWordPrintable

Details

    • Improvement
    • Resolution: Fixed
    • Minor
    • Morpheus
    • Morpheus
    • couchbase-bucket
    • None
    • 0

    Description

      Nlohmann::json automatically serialises enums as integers (https://github.com/nlohmann/json?tab=readme-ov-file#specializing-enum-conversion). This is undesirable, because we often want enum values stored in JSON to be represented as strings.

      To avoid the conversion to integer, we need to define custom to_json for every enum used in this way. Because of the integer-conversion behaviour is the default, it becomes easy to forget to define a to_json method for the enum, and end up with some unexpected JSON.

      We can build nlohmann::json with JSON_DisableEnumSerialization=On. Since there are a few places in the code where this default behaviour is used, we have to make some changes to use an explicit static_cast<int> where the integer conversion is actually desired.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            vesko.karaganev Vesko Karaganev
            vesko.karaganev Vesko Karaganev
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes

                PagerDuty