Proposal for better JSON serialization control
-
Hello all.
Been a Qt user/fan for almost two decades, and it's time to give back a little. This is my first foray into contribution.
First the problem statement:
More and more, JSON is being used for human-readable files (think almost all Visual Studio Code configuration files).
It is understood that the JSON format does not specify the order of objects serialized into output. However, for human-readable JSON, better control of object serialization order is often desirable. Related object types may be better suited to be near each other in the output.
Additionally, the order of properties in a JSON object can make a huge difference in (human) readability.
The output of objects and values produced by QJsonDocument::toJson() is serialized in alphabetical order, and there are no controls over output serialization other than Indented or Compact.
Proposal:
Create a QJsonSerializer class which will provide finer grained control over serialization. This would be an optional parameter to QJsonDocument::toJson().
The default QJsonSerializer would behave exactly as the current serialization, providing full backwards compatibility. However, the developer could subclass QJsonSerializer and override protected methods within to provide feedback/hints about serialization order, indentation, brace formatting and other common serialization properties.
I'd love to get some feedback on this idea. Looking around the interwebs, this feature is commonly asked about. If it would be useful, I'd be happy to implement it.
Regards,
Stephen Punak
Principal Engineer
Lordstown Motors Corporation -
@Plastic-Jesus This is user forum. If you want to propose something you should create a change request in Qt bug tracker (https://bugreports.qt.io/secure/Dashboard.jspa).
-
I recommend you subscribe to the Development mailing list (https://lists.qt-project.org/listinfo/development ) and post your proposal there. Qt engineers are active on that list, and you should be able to get a decent discussion going if they are interested in your proposal.
All the best!
-
@Plastic-Jesus
Can you name another toolkit/library which does offer the controls on serialization you mention? I have used various ones outside of Qt's (e.g. JavaScript, Python) and am not aware of one which does. -
I think this is a great idea. It would be great to have more control over the serialization of JSON data. It would make it easier to customize the output and make sure that the data is properly formatted. It would also make it easier to debug any issues that may arise. I think this would be a great addition to the Qt library.