Loki-Astari / ThorsSerializer

C++ Serialization library for JSON

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Different OpenAPI Discriminator

opened this issue · comments

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.

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 .

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

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.

Fixed it

Unit tests: https://github.com/Loki-Astari/ThorsSerializer/blob/master/src/Serialize/test/Issue50Test.cpp
Building on home brew Homebrew/homebrew-core#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")); 

Closing.