title: Boost Serialize data in a custom way
body: <p>Suppose that I've a list of objects stored somewhere in a map . My goal is to serialize all shared pointers through a key like this:</p>
  
  <pre><code>namespace boost { namespace serialization {
  
  template&lt;class Archive, class T&gt;
  inline void serialize(Archive&amp; ar, boost::shared_ptr&lt;T&gt;&amp; b, const unsigned int file_version)
  {
      std::string key_of_b = // get a key of object b;
      ar &lt;&lt; key_of_b;
  }
  
  } }
  </code></pre>
  
  <p>Then, during the serialization through the key serialized, get back the object in the map. Is it possible to serialize-deserialize the object by using only the boost::shared_ptr? Like:</p>
  
  <pre><code>int main() {
  
          std::stringstream ss;
  
          // serialize
          boost::shared_ptr&lt;int&gt; p{new int{1123}};
          boost::archive::text_oarchive oa(ss);
          oa &lt;&lt; p1;
  
          // deserialize
          boost::archive::text_iarchive ia(ss);
          boost::shared_ptr&lt;int&gt; p_i{new int{1123}};
          ia &gt;&gt; p_i;
  
          return 0;
      }
  </code></pre>
  
  <p>I know that the code cannot work in this way. I thought to handle the serialization/deserialiazion in the <code>serialize</code> method:</p>
  
  <pre><code>template&lt;class Archive, class T&gt;
      inline void serialize(Archive&amp; ar, boost::shared_ptr&lt;T&gt;&amp; b, const unsigned int file_version)
      {
          if (boost::archive::text_oarchive::is_saving::value) {
            std::string key_of_b = // get a key of object b;
            ar &lt;&lt; key_of_b;
          } else {
            std::string key;
            ar &gt;&gt; key;
            // get the T
          }
      }
  </code></pre>
  
  <p>Is that correct or there is another better way?</p>