Skip to content
This repository has been archived by the owner on Mar 4, 2023. It is now read-only.

Create references to existing object , not duplicates #48

Closed
loopless opened this issue Dec 27, 2021 · 3 comments
Closed

Create references to existing object , not duplicates #48

loopless opened this issue Dec 27, 2021 · 3 comments

Comments

@loopless
Copy link

Assuming I have some code like this
QList<QSharedPointer> aList;
QSharedPointer tmp(new SampleObject());
aList.append(tmp);
aList.append(tmp);
auto jsonarray = serializer.serialize(aList);
qDebug() << "\nSerialize a QList now:";
qDebug() << JSON_PRINT(jsonarray);

Then the generated JSON has duplicates of the object. Clearly this will not restored as intended.
The application code could use "names" or some other "ids" to prevent duplication as a workaround.
Just wondering if this has been thought of/dealt with by others.

@Skycoder42
Copy link
Owner

It works this way on purpose, as QObjects (which are pointers) are serialized from and to JSON. So smart pointer that wrap QObjects do the same, it just exists for convenience. Converting pointers from and to JSON is simply not possible, as you can't transform a reference.

@loopless
Copy link
Author

loopless commented Jan 4, 2022

Firstly, thanks for a great library. It's well written and super useful.
I did not expect the library to magically handle references.
Creating object references rather than duplicate objects then has to be handled at an application level as a post processing step after reading the JSON. For example, have a 64-bit ID in each QObject and do a fix-up to re-use an existing object if the ID has previously been encountered. Or something like that.

@Skycoder42
Copy link
Owner

In that case, you could write a custom converter and register it with a high priority for your pointers. See https://skycoder42.github.io/QtJsonSerializer/class_qt_json_serializer_1_1_type_converter.html

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants