Coder Social home page Coder Social logo

joypoint / qtpropertyserializer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcel-goldschen-ohm/qtpropertyserializer

0.0 0.0 0.0 38 KB

Serialization of QObject property tree <==> QVariantMap <==> {Json, XML, ...}

License: MIT License

C++ 87.60% QMake 1.20% CMake 11.19%

qtpropertyserializer's Introduction

QtPropertySerializer

Simple C++ serialization for a QObject property tree.

QObject <==> QVariantMap <==> {Json, XML, ...}
  • Recursively serializes child objects.
  • ONLY serializes properties (ignores other class members).
  • Dynamic runtime creation of child objects during deserialization (requires factory).

Author: Marcel Goldschen-Ohm
Email: [email protected]
License: MIT
Copyright (c) 2015 Marcel Goldschen-Ohm

Alternatives

For JSON serialization, also check out qjson.

INSTALL

Everything is in:

  • QtPropertySerializer.h
  • QtPropertySerializer.cpp

CMake:

See CMakeLists.txt for example build as a static library.

๐Ÿ‘‰ This is most likely what you want: See test/CMakeLists.txt for example build of an app that uses QtPropertySerializer. This build uses CMake to automatically download QtPropertySerializer files directly from this GitHub repository, builds QtPropertySerializer as a static library and links it to the app executable. This way you can use QtPropertySerializer in your project without downloading or managing the QtPropertySerializer repository manually.

Requires:

QObject <==> QVariantMap

QVariantMap (key, value) pairs are either:

  1. (property name, property value)
  2. (child object class name, child QVariantMap)
  3. (child object class name, QVariantList of QVariantMaps for multiple children of the same type)

Quick start code snippets

See test.h/cpp for complete example code.

#include "QtPropertySerializer.h"

A QObject tree.

jane
|-- john
|-- josephine
    |-- spot
class Person : public QObject { ... };
class Pet : public QObject { ... };
Person jane;
Person *john = new Person;
Person *josephine = new Person;
Pet *spot = new Pet;
john->setParent(&jane);
josephine->setParent(&jane);
spot->setParent(josephine);

Serialize QObject ==> QVariantMap.

QVariantMap janePropertyTree = QtPropertySerializer::serialize(&jane);
  • Access child QVariantMaps in parent QVariantMap by class name.
  • Multiple children of the same type are placed in a QVariantList.
QVariantList janePersonList = janePropertyTree["Person"].toList();
QVariantMap johnPropertyTree = janePersonList[0].toMap();
QVariantMap josephinePropertyTree = janePersonList[1].toMap();
QVariantMap spotPropertyTree = josephinePropertyTree["Pet"].toMap();
  • Access properties in QVariantMap by property name.
qDebug() << janePropertyTree["objectName"];
qDebug() << johnPropertyTree["objectName"];
qDebug() << josephinePropertyTree["objectName"];
qDebug() << spotPropertyTree["objectName"];

Deserialize QVariantMap ==> QObject.

Deserialize into a pre-existing object tree.

QtPropertySerializer::deserialize(&jane, janePropertyTree);

Deserialize into an object without pre-existing child objects (requires runtime dynamic object creation using a factory).

QtPropertySerializer::ObjectFactory factory;
factory.registerCreator("Person", factory.defaultCreator<Person>);
factory.registerCreator("Pet", factory.defaultCreator<Pet>);

// factory.defaultCreator<T> is a static function taking
// no arguments and returning a QObject* for `new T()`.
// You are also free to use your own custom creator function here.

// Prior to deserialization, bizarroJane has no children.
Person bizarroJane;

// After deserialization, bizarroJane is identical to Jane
// with children John and Josephine, and Josephine's pet Spot.
QtPropertySerializer::deserialize(&bizarroJane, janePropertyTree, &factory);

Serialize QObject <==> JSON file.

QtPropertySerializer::writeJson(&jane, "jane.json");
QtPropertySerializer::readJson(&jane, "jane.json");

Person juniper;
QtPropertySerializer::readJson(&juniper, "jane.json", &factory);

qtpropertyserializer's People

Contributors

marcel-goldschen-ohm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.