Comments (10)
Does this cover the issue?
Pointer (and std::unique_ptr<>
) are covered automatically for normal simple types. If you have polymorphic types then you need to do a bit more work. See here: https://github.com/Loki-Astari/ThorsSerializer/blob/master/doc/example3.md
If this dies not cover your situation then please provide an example.
from thorsserializer.
for example
i have 2 object.
static int ID = 0;
struct Object
{
int id;
Object()
{
this->id = ID++;
};
};
struct Student :public Object
{
Student():Object(){ }
};
struct ClassRoom :public Object
{
ClassRoom() :Object()
{
}
void addStudent(Student& pStudent)
{
this->students.push_back(pStudent.id);
}
std::vector<int> students;
};
void main()
{
Student obj;
obj.age = 20;
obj.height = 180;
obj.name = "xia";
ClassRoom room;
room.addStudent(obj);
}
and the obj and room 's id is diffrent when recreate,
this is normally remapping the relation of room and obj.
how to do it when i use ThorsSerializer
from thorsserializer.
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/Traits.h"
// Your class definitions from above.
// You should fix Student it is missing some fields.
ThorsAnvil_MakeTrait(Object, id);
ThorsAnvil_ExpandTrait(Student, Object, age, height, name);
ThorsAnvil_ExpandTrait(ClassRoom, Object, students);
int main()
{
Student obj;
obj.age = 20;
obj.height = 180;
obj.name = "xia";
ClassRoom room;
room.addStudent(obj);
// Save state of program
using ThorsAnvil::Serialize::jsonExport;
std::cout << jsonExport(room) << "\n";
std::cout << jsonExport(obj) << "\n";
std::cout << jsonExport(ID) << "\n";
// Load State of program
using ThorsAnvil::Serialize::jsonImport;
std::cin >> jsonImport(room);
std::cin >> jsonImport(obj);
std::cin >> jsonImport(ID);
}
from thorsserializer.
the point of the problem is "the relation between obj and room",
- if obj.id==1 and room.id ==2 and room.students[0].id==1.
- and when i load the Serialized text from jsonImport(),the ids mybe
obj.id==5 and room.id ==6 and room.students[0].id==1.
the variable id will auto gernerate when create Object every time.
it is a really familiar situation in game engine.the id cant be same when recreate.
so when i recreate Object,i need keep the realation of Objects.
like obj.id==5 and room.id ==6 and room.students[0].id==5.
from thorsserializer.
I solved that problem in the example above.
If you serialize the ID as part of the object it will be restored correctly and thus the reference from room to object will be maintained. The side affect of this is that you need to serialize the global ID
so that when you restart the ID is also updated from a consistent state.
Are you asking to run arbitrary user code to re-calculate id
?
from thorsserializer.
it is a really familiar situation in game engine
Sure. But it is only useful if you can use the id
to find the object. Which then implies that all the object are stored in a container and the id
is used to index into the container to retrieve the object.
In this case you would serialize the container containing all the objects and when you load them back in the correct id will still be correct.
#include "ThorSerialize/JsonThor.h"
#include "ThorSerialize/SerUtil.h"
#include "ThorSerialize/Traits.h"
struct Object;
static std::vector<Object*> store;
struct Object
{
int id;
virtual ~Object()
{
store[id] = nullptr;
}
Object()
{
this->id = store.size();
store.push_back(this);
}
};
ThorsAnvil_MakeTrait(Object, id);
int main()
{
// Load previous state
std::cin >> jsonImport(store);
// Save State on exit
std::cout << jsonExport(store);
}
from thorsserializer.
Are you asking to run arbitrary user code to re-calculate id?
yes
from thorsserializer.
You need to make the id
object a special type. You can then define the normal stream operators for that type to serialize the object and use the macro ThorsAnvil_MakeTraitCustom()
to mark it as a normal serialization object.
#include "ThorSerialize/Traits.h"
#include "ThorSerialize/JsonThor.h"
#include <iostream>
int ID = 0;
struct IdObj
{
IdObj(int id)
: id(id)
{}
int id;
friend std::ostream& operator<<(std::ostream& str, IdObj const& data) {
// When streaming to output you must make sure that␣
// this is valid JSON.
return str << data.id;
}
friend std::istream& operator>>(std::istream& str, IdObj& data) {
// When reading the value you must make sure you read everything that
// you wrote out during streaming.
str >> data.id;
// Do the action you want to update the ID to something unique to you
return str;
}
};
struct Object
{
IdObj id;
Object()
: id(ID++)
{}
};
ThorsAnvil_MakeTraitCustom(IdObj);
ThorsAnvil_MakeTrait(Object, id);
using ThorsAnvil::Serialize::jsonImport;
using ThorsAnvil::Serialize::jsonExport;
int main()
{
Object id;
std::cout << jsonExport(id) << "\n";
std::cin >> jsonImport(id);
}
Since you have defined the serialization functions you can execute any arbitory code when you read the values in.
from thorsserializer.
in this case,i find that ,it is hard to find a common way to serialization when i need add logic code when deserialization .
from thorsserializer.
Not sure what you are saying.
It is your type that has an asymmetric concept of serialization that is strange. If you look at my example above it solves all the problems (where you have a vector of objects) without the need for extra code. Also this would be the way a standard application would implement the "all objects have id" concept.
Unless you have anything specific you are trying to solve please close the issue as I don't see anything generic that a serialization library should be solving for you here.
from thorsserializer.
Related Issues (20)
- Maximum number of levels/depth? HOT 5
- No compile enum with 8 and more element HOT 6
- Deserializing to existing object with vectors/pointers? HOT 8
- Std:array not working on header only version HOT 6
- Make under Homebrew results in template error HOT 1
- Make Trait does not work for types with static members with non-standard types HOT 1
- Compilation issue with VisualStudio 2019 on Windows HOT 4
- my compile problem of example1 HOT 1
- Redefine traits at runtime HOT 6
- MakeTrait: error: unable to deduce 'std::initializer_list<auto>' HOT 9
- [Linuxbrew] Trying to prepare v2.2.0 bottle but hitting compilation issues (tried GCC-11 and LLVM) HOT 10
- Tags with header only HOT 7
- Linker error on basic example, I am missing something? HOT 4
- Linker Error? HOT 5
- Invalid enum values causes undefined behavior HOT 1
- missing include file magic_enum.hpp HOT 3
- Linker fails with header-only build HOT 11
- Why there is no CMAKE support? HOT 5
- error compiling with std::shared_ptr<const Type> HOT 5
- read problem with map of pointers HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from thorsserializer.