Skip to content

Support unordered_map (and std containers?) with enum class as key #241

@Alexandre-Dhalenne

Description

@Alexandre-Dhalenne

Hey,

I am trying to use your library to build a specific serialization.
Here is what I have :

template <typename E>
class TCriterion
{
public:
	TCriterion() = default;
	TCriterion(E iType, std::string iVal) : _type(iType), _value(iVal) {}

	const std::string& getValue() const {
		return _value;
	}

	const E getType() const {
		return _type;
	}

protected:
	JSONCONS_TYPE_TRAITS_FRIEND;
	E _type;
	std::string _value;
};

template <typename E>
class TCriteria
{
public:
	TCriteria() = default;

	const std::unordered_map <E, TCriterion<E>>& getCriteriaMap() const {
		return _criteriaMap;
	}

protected:
	JSONCONS_TYPE_TRAITS_FRIEND;
	std::unordered_map<E, TCriterion<E>, EnumClassHash> _criteriaMap;
};

So two templates for a Criterion and a Criteria.
But I specify them in a class :

class MyCriterion : public TCriterion<MyCriterionType>{
public:
	MyCriterion () = default;
	MyCriterion (MyCriterionType iType, std::string iVal) : TCriterion<MyCriterionType>(iType,iVal){}
private:
	JSONCONS_TYPE_TRAITS_FRIEND;
};

class MyCriteria : public TCriteria<MyCriterionType> {
public:
	MyCriteria () = default;
};

I use these macros :

JSONCONS_TPL_ALL_MEMBER_NAME_TRAITS(1,online::service::TCriterion, (_type,"name"), (_value,"value"))
JSONCONS_TPL_ALL_MEMBER_NAME_TRAITS(1, online::service::TCriteria, (_criteriaMap, "criteriaList"))
JSONCONS_ALL_MEMBER_NAME_TRAITS(online::service::MyCriterion, (_type, "name"), (_value, "value"))
JSONCONS_ALL_MEMBER_NAME_TRAITS(online::service::MyCriteria , (_criteriaMap, "criteriaList"))

I defined an enum class MyCriterionType like this :

	enum class MyCriterionType{
		First,
		Presentation = First,
		MessageType,
		Version,
		Release,
		Last
	};

	struct EnumClassHash {
		template<typename T>
		std::size_t operator()(T t) const{
			return static_cast<std::size_t>(t);
		}
	};//To be able to use the enum class as key

Everything works for MyCriterion. But When I try to decode MyCriteria, I have an error at compilation time :

invalid explicit template argument(s) for std::enable_if<jsoncons::is_json_type_traits_specialized<jsoncons::basic_json<char,jsoncons::sorted_policy,std::allocator<char>>,T,void>::value,T>::type jsoncons::basic_json<char,jsoncons::sorted_policy,std::allocator<char>>::as(void) const'	

After somme debugging, I found that it comes from the enum as key. When I replace it by int for instance, it works.
Everything is well declared and in order for jsoncons.
Should I specify my own type_traits for this case ?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions