Loki-Astari / ThorsMongo

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

Different OpenAPI Discriminator #50

Closed ghost closed 5 years ago

ghost commented 5 years ago

Is your feature request related to a problem? Please describe. I am re-implementing an external API, they use $type instead of __type as the discriminator for polymorphic types.

Describe the solution you'd like Make it possible to change the discriminator.

Describe alternatives you've considered Maybe in a Header-only version of the lib would be enough if it requires too many changes otherwise.

Thank you.

Loki-Astari commented 5 years ago

That sounds like an interesting feature.

I am thinking something along the lines:

      JsonProperties     property{{"polymorphicMarker", "$type"}};
      stream >> jsonImport(object, property);

The JsonProperties allows you to specify one or more properties about how to serialize/de-serialize .

Loki-Astari commented 5 years ago

As a short term fix for you I can update so that it depends on macro defined in the config file.

ghost commented 5 years ago

It is not super urgent. I could wait for some days. But if you have other points on your agenda or not so much time I'd prefer short-term now and JsonProperties later.

Loki-Astari commented 5 years ago

Fixed it

Unit tests: https://github.com/Loki-Astari/ThorsSerializer/blob/master/src/Serialize/test/Issue50Test.cpp Building on home brew https://github.com/Homebrew/homebrew-core/pull/42160

Simply add the polymorphic marker to jsonImport() or jsonExport().

using namespace std::string_literals;
stream >> jsonImport(object, "$type"s); // you do need the s otherwise you need to explicitly create the config Object.
stream << jsonExport(object, "$type"s); 
stream >> jsonImport(object, ParserInterface::ParserConfig("$type"));
stream << jsonExport(object, PrinterInterface::PrinterConfig("$type")); 
Loki-Astari commented 5 years ago

Closing.