[Development] [RFO QTBUG-84739] QJsonValue::fromVariant containing QByteArray (Thiago Macieira)

Jason H jhihn at gmx.com
Sat Jul 4 23:52:34 CEST 2020


Just thinking outloud, but would it be possible to have a conversion function that takes a map of lamdas?
Where I am going with this, is that it would be cool to have some serialization function that looks like:

map.convert(QMap<QString,std::function> {
  {"QByteArray": [](QByteArray const& ba) 
    {
      return QString(ba.toHex());
    }
  }
});

It's a little gross, but the goal is to allow the application to provide the conversion code in ambiguous situations. I'm assuming there is another type than QByteArray that has ambiguous conversion, or that we would allow it for any type. If it's just QByteArray, it could be just:

map.convert([](QByteArray const& ba) {
     return QString(ba.toHex());
});

Maybe that's a dumb idea, because you could always just patch it up later, but I am under the impression that that has some quirks like if in map, having to patch the map up once the map is modified. And so the lambdas would end up being cleaner. 

Just a thought.


> Sent: Friday, July 03, 2020 at 2:07 PM
> From: "Thiago Macieira" <thiago.macieira at intel.com>
> To: development at qt-project.org
> Subject: Re: [Development] [RFO QTBUG-84739] QJsonValue::fromVariant containing QByteArray (Thiago Macieira)
>
> On Friday, 3 July 2020 05:16:11 PDT Arnaud Clère wrote:
> > > [1] https://tools.ietf.org/html/rfc7049#section-4.1
> > 
> > Even if non normative, these advices are useful and even more so when
> > implemented in such wide-spread library as Qt, so if it can be opted-in, I
> > would definitely prefer option c)
> 
> That advice applies about converting CBOR to JSON. Ever since I introduced 
> QCborValue, doing:
> 	QCborValue(QByteArray("Hello")).toJsonValue()
> resulted in
> 	QJsonValue("SGVsbG8")
> 
> That will not change. You may call this surprising, but it's in line with 
> standards and is explicitly documented as such.
> 
> It's also the reason this change happened to QJsonValue, because we reused 
> QCborValue::fromVariant and then converted from that to JSON.
> 
> Also note that you can tell CBOR how to encode a byte array to JSON:
> 
>   QByteArray hello("hello");
>   QCborValue(hello).toJsonValue();
> 	// "SGVsbG8"
>   QCborValue(QCborKnownTags::ExpectedBase64, hello).toJsonValue();
> 	// "SGVsbG8="
>   QCborValue(QCborKnownTags::ExpectedBase16, hello).toJsonalue();
> 	// "48656c6c6f"
> 
> -- 
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel System Software Products
> 
> 
> 
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development
>


More information about the Development mailing list