Loki-Astari / ThorsMongo

C++ MongoDB API and BSON/JSON Serialization library
GNU General Public License v3.0
316 stars 71 forks source link

Add support to change the json property names when serializing/deserializing #56

Closed fabio-costa-souza closed 3 years ago

fabio-costa-souza commented 4 years ago

Is it possible to have one field in the class, but different names for it during serialization/deserialization?

Something like this:

public class Videogame { private:

  [JsonProperty("name")]
   string Name;

   [JsonProperty("release_date")]
   private DateTime ReleaseDate;

}

Videogame starcraft = new Videogame { Name = "Starcraft", ReleaseDate = new DateTime(1998, 1, 1) };

string json = ThorsAnvil::Serialize::jsonExport(starcraft);

Console.WriteLine(json); // { // "name": "Starcraft", // "release_date": "1998-01-01T00:00:00" // }

JakkuSakura commented 4 years ago

You can do it by adding your traits. Actually they are poorly documented(but still mentioned on another site) but the framework is not hard to understand while not documented at all

Loki-Astari commented 3 years ago

Done: Use the macros:

Check Examples here: https://github.com/Loki-Astari/ThorsSerializer/blob/master/src/Serialize/test/OverrideIdentiferToKeyMapTest.cpp