[Development] Proposal: JSON single value documents

Matt Newell newellm at blur.com
Mon Sep 12 23:40:19 CEST 2016


I would like to propose an addition to the QtJson api to allow single value 
json documents (SVJ for brevity).

There have been various discussions regarding whether or not a SVJ is a valid 
json document, with more recent rfc's saying yes.   Here is one discussion 
which also has links to others:
http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json

Regardless of whether a SJV is a valid document, the fact remains that to be 
fully interoperable with many other existing projects Qt should support them.  
My initial motivation is full support for the json/jsonb column type in 
Postgresql.

My proposed change would add the following methods to QJsonDocument:
  bool isValue() const;
  QJsonValue value() const;
  void setValue(const QJsonValue &value);

The only backwards compatibility issue that i can think of would be that 
fromVariant and fromJson will now return valid values documents.  For that 
reason it may be needed to provide new fromVariant and fromJson overloads.

I have figured out an elegant way to handle SVJ's without changing qt's binary 
format.  Because the binary format in it's current form can only hold a top-
level object or array, and because any old code will not be able to load a 
binary document containing a single value, a version bump is required.  My 
idea is to have a SVJ stored as a single element json array containing the 
value, but have the version set to 2.  This results in no actual changes to 
the binary format at all.  Any non-SVJ document continues to use version 1 
retaining full backwards compatibility for any document that was valid 
previously.

Attached is a preliminary patch that compiles, passes the existing tests in 
tests/auto/corelib/json, and adds a few new tests to exercise the new 
functionality.

If it's determined that this is a reasonable approach I will work on extending 
the tests and updating the documentation.

Thanks,
Matt Newell
-------------- next part --------------
A non-text attachment was scrubbed...
Name: json_svj.diff
Type: text/x-patch
Size: 11868 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20160912/6adb868b/attachment.bin>


More information about the Development mailing list