1

I have a list of objects derived from a class named "Campus" which contains two strings, one int and two lists : one for "Students", the other is for "Teachers", before closing the program, I want to save the campus objects and of course the "Student" and "Teachers" objects contained on their lists, I want to serialize those data in an XML or JSON format or even anything else then store the result in a file.

Can somebody give me the fastest way to do the serialization with a library (that is not heavy as boost) in XML or JSON or another solution. When it comes to deal with JSON or XML serialization, I don't know what to do ! EDIT: is this feasible with RapidJSON ?

class Campus
{
private:
    std::string city;
    std::string region;
    int capacity;
    std::list<Student> students;
    std::list<Teacher> teachers;
}

class Student
{
private:
    int ID;
    std::string name;
    std::string surname;
}

class Teacher
{
protected:
    int ID;
    std::string name;
    std::string surname;
};
Aminos
  • 754
  • 1
  • 20
  • 40

2 Answers2

2

You can use this C++ serialization library : Pakal persist

#include "XmlWriter.h"


class Campus
{
private:
    std::string city;
    std::string region;
    int capacity;
    std::list<Student> students;
    std::list<Teacher> teachers;

public:

    void persist(Archive* archive)
    {
        archive->value("city",city);
        archive->value("region",region);
        archive->value("capacity",capacity);

        archive->value("Students","Student",students);
        archive->value("Teachers","Teacher",teachers);
    }

}

class Student
{
private:
    int ID;
    std::string name;
    std::string surname;

public:

    void persist(Archive* archive)
    {
        archive->value("ID",ID);
        archive->value("surname",surname);
        archive->value("name",name);        
    }

}

class Teacher
{
protected:
    int ID;
    std::string name;
    std::string surname;
public:

    void persist(Archive* archive)
    {
        archive->value("ID",ID);
        archive->value("surname",surname);
        archive->value("name",name);
    }
};

Campus c;

XmlWriter writer;
writer.write("campus.xml","Campus",c);
elios264
  • 384
  • 4
  • 19
  • 1
    it's an old question but your thing might be interesting. it appears to be less heavy and more convenient than boost serialization. – Aminos Nov 19 '15 at 08:55
0

Unfortunately C++ doesn't support reflection, so it can't automagically figure out the parameter names.. but check out this answer which looks like it'll be close to what you want: https://stackoverflow.com/a/19974486/1715829

Community
  • 1
  • 1
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • With RapidJSON, for each Campus element on my list, can I generate a JSON containing All my campus objects with their city, region, capacity and the list of the teachers and students that they contain with of course the data related to students and teacher ? I saw that using rapidjson aint hard but I dont know how to use it for a such complex case ?? – Aminos May 09 '15 at 11:05
  • From the looks of ThorsStream you'd just add one line per class, like this: `JsonSerializeTraits_MAKE(void, Campus, city, region, capacity, students, teachers) JsonSerializeTraits_MAKE(void, Student, ID, name, surname) JsonSerializeTraits_MAKE(void, Teacher, ID, name, surname)` – Buddy May 10 '15 at 03:24
  • https://github.com/Loki-Astari/ThorsStream is this the link to download this library ? can you give me please a complete example so I could then implement the thing? – Aminos May 10 '15 at 10:38
  • Otherwise, can I send the objects via a stream to a file ? without using JSON/XML – Aminos May 10 '15 at 10:42
  • I'm trying to use the RapidJSON serialization example into my case, when I finish doing and testing this, I will tell you ;) Have a nice day – Aminos May 10 '15 at 11:08