vendredi 8 janvier 2021

How to return a string instead of an enum when serializing

I would like to serialize a struct that has an enum in it. I read this post that post is using a different mechanism for serializing while here I am using mystruct.serialize();

 enum class extEnum 
 {
   mytypeA,
   mytypeB,
 };

struct extStruct
{
   extEnum ta;
   int phone;

   template<class Archive>
   void serialize(Archive& ar)
   {
           ar(CEREAL_NVP(ta),
             CEREAL_NVP(phone)
           );
   }
}

I am serializing this using this code

extStruct mystruct;
//....
std::stringstream os;
{
    cereal::JSONOutputArchive ar(os);
    mystruct.serialize(ar);
}

Now when I serialize this to json the parameter comes out as int. Is there any way for me to specify that the field ar should be returned as a string. I have a function that will return string of the enum. I am just not sure how to use it

Aucun commentaire:

Enregistrer un commentaire