Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to serialise whole std::list with custom structure type? #4

Closed
akshar001 opened this issue Jul 11, 2018 · 3 comments
Closed

How to serialise whole std::list with custom structure type? #4

akshar001 opened this issue Jul 11, 2018 · 3 comments
Labels

Comments

@akshar001
Copy link

Hello,
You have made a very great and simple library but can you just help me with this?

@fraillt
Copy link
Owner

fraillt commented Jul 12, 2018

Hello,
First of all congratulations, you're the first to submit the ticket!
I believe the problem you encounter is that quickSerialization/Deserialization doesn't work, right?

The idea behind quickSerialization/Deserialization is that it would simplify the general case, e.g. serialize struct (object). If you look at the implementation of quickSerialization you would see that it calls ser.object(value), all you need to do is to use the same code except in your case call ser.container(value, maxSize) where maxSize is your defined max allowable container size.

Hope that helps :)

@Rinkss
Copy link

Rinkss commented Aug 2, 2018

Is this library suitable for maps and nested map?

@fraillt
Copy link
Owner

fraillt commented Aug 6, 2018

yep
Lets take this example

using NestedType = std::unordered_map<int, std::vector<float>>;
using MyType = std::map<std::string, NestedType>;

There are two ways to do it. using verbose syntax (full serialization control), and flexible syntax.

In verbose syntax you need to use StdMap extensions <bitsery/ext/std_map.h> and
define serialize function in bitsery namespace for ADL (polluting std namespace is not recommended)
e.g.

namespace bitsery {
    template <typename S>
    void serialize(S& ser, MyType& data) {
        ser.ext(data, ext::StdMap{10}, [&ser](std::string& key, NestedType & value) {
            ser.text1b(key, 10);
            ser.ext(value, ext::StdMap{10}, [&ser](int& key, std::vector<float>& value) {
                ser.value4b(key);
                ser.container4b(value, 10);
            });
        });
    }
}

or create your own function anywhere you like, e.g.

template <typename S>
void serializeAndDeserializeMyType(S& ser, MyType& data) {
    ser.ext(data, ext::StdMap{10}, [&ser](std::string& key, NestedType & value) {
        ser.text1b(key, 10);
        ser.ext(value, ext::StdMap{10}, [&ser](int& key, std::vector<float>& value) {
            ser.value4b(key);
            ser.container4b(value, 10);
        });
    });
}

In flexible syntax you don't need to define serialize function, just include other headers and you're done!

#include <bitsery/flexible.h>
#include <bitsery/flexible/map.h>
#include <bitsery/flexible/unordered_map.h>
#include <bitsery/flexible/string.h>
#include <bitsery/flexible/vector.h>

just instead of calling ser.object(data), call ser.archive(data) see this example for more details.

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

No branches or pull requests

3 participants