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 custom serialize same data type of vector? #2215

Closed
gzliudan opened this issue Jun 24, 2020 · 1 comment
Closed

How to custom serialize same data type of vector? #2215

gzliudan opened this issue Jun 24, 2020 · 1 comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@gzliudan
Copy link

gzliudan commented Jun 24, 2020

for example

struct Item {
    std::vector<int> array1;
    std::vector<int> array2;
};

Item  {
    .array1 = {123};
    .array2 = {123};
};

std::string func1(int i) { return std::string("a") + std::to_string(i + i); } 
std::string func2(int i) { return std::string("b") + std::to_string(i * i); } 

during serialize, I want to apply func1 to each element of array1, apply func2 to each element of array2, so the json will become

{
    "array1": [ "a2", "a4", "a6" ], 
    "array2": [ "b1", "b4", "b9" ]
}
@nlohmann
Copy link
Owner

You need to define a to_json function as described in https://github.com/nlohmann/json#arbitrary-types-conversions

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants