Handling enum fields in Spotify's JSON library

Hey guys, I’m stuck with a JSON serialization issue. I’m using this cool library from Spotify for JSON stuff, but I can’t figure out how to deal with an enum class field in one of my objects.

The library has some kind of codec for enums, but the docs say it doesn’t work with the default codec. I’m lost on how to make it work with my enum field.

Here’s a simplified version of what I’m trying to do:

enum class LogSeverity {...};

struct LogItem
{
    std::string server;
    LogSeverity severity;
    std::string content;
    int64_t timestamp;
};

// Spotify JSON stuff
namespace spotify {
namespace json {

using LogItemType = LogItem;

template <>
struct default_codec_t<LogItemType>
{
    static codec::object_t<LogItemType> codec()
    {
        auto c = codec::object<LogItemType>();
        c.required("server", &LogItemType::server);
        c.required("severity", &LogItemType::severity); // How to handle this?
        c.required("content", &LogItemType::content);
        c.required("timestamp", &LogItemType::timestamp);
        return c;
    }
};

} // namespace json
} // namespace spotify

Any ideas on how to make this work? I’m really scratching my head here!

I encountered a similar issue with Spotify’s JSON library recently. The solution that worked for me was to implement a custom codec for the enum. Here’s what I did:

First, I created a conversion function for the enum:

std::string logSeverityToString(LogSeverity severity) {
    switch (severity) {
        case LogSeverity::Info: return "INFO";
        case LogSeverity::Warning: return "WARNING";
        case LogSeverity::Error: return "ERROR";
        default: throw std::runtime_error("Unknown severity");
    }
}

LogSeverity stringToLogSeverity(const std::string& str) {
    if (str == "INFO") return LogSeverity::Info;
    if (str == "WARNING") return LogSeverity::Warning;
    if (str == "ERROR") return LogSeverity::Error;
    throw std::runtime_error("Invalid severity string");
}

Then, I defined a custom codec for LogSeverity:

template <>
struct default_codec_t<LogSeverity> {
    static auto codec() {
        return transform(string_codec(), logSeverityToString, stringToLogSeverity);
    }
};

This approach allowed the enum to work seamlessly with the default codec in the library. Hope this helps!

I’ve faced a similar issue with Spotify’s JSON library. The key is to create a custom codec for your enum. Here’s what worked for me:

First, define a conversion function for your enum:

std::string logSeverityToString(LogSeverity severity) {
    // Implement conversion logic here
}

LogSeverity stringToLogSeverity(const std::string& str) {
    // Implement conversion logic here
}

Then, create a custom codec for LogSeverity:

template <>
struct default_codec_t<LogSeverity> {
    static auto codec() {
        return transform(string_codec(), logSeverityToString, stringToLogSeverity);
    }
};

Now, your enum field should work with the default codec. This approach lets you handle the serialization and deserialization of the enum without modifying the library’s core functionality.

yo, had the same prob with spotify json. try this:

make a custom codec for ur enum. like:

template <>
struct default_codec_t<LogSeverity> {
  static auto codec() {
    return enum_codec<LogSeverity>();
  }
};

shud work wit the default codec then. good luck!

I’ve worked with Spotify’s JSON library before, and handling enums can be tricky. One approach that’s worked well for me is to create a custom codec specifically for your LogSeverity enum. Here’s what I’d suggest:

First, define a map to convert between your enum values and strings:

const std::map<LogSeverity, std::string> severityMap = {
    {LogSeverity::Info, "INFO"},
    {LogSeverity::Warning, "WARNING"},
    {LogSeverity::Error, "ERROR"}
};

Then, implement conversion functions:

std::string logSeverityToString(LogSeverity severity) {
    return severityMap.at(severity);
}

LogSeverity stringToLogSeverity(const std::string& str) {
    for (const auto& pair : severityMap) {
        if (pair.second == str) return pair.first;
    }
    throw std::runtime_error("Invalid severity string");
}

Finally, create a custom codec for LogSeverity:

template <>
struct default_codec_t<LogSeverity> {
    static auto codec() {
        return transform(string_codec(), logSeverityToString, stringToLogSeverity);
    }
};

This should allow your enum to work seamlessly with the default codec in the library.