I am executing following code, Observed that values in json are getting stored as there ASCII codes. for a single char and char array, we can convert it to string and store as it is, but not able to find a way for unsigned char. please find the code sample below:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <array>
#include <initializer_list>
#include "nlohmann/json.hpp"
using nlohmann::json;
std::vector<char> CharArrayToVector(std::array<char, 10> number)
{
std::vector<char> vec;
for (const auto &c : number)
{
if (c != '\0')
vec.push_back(c);
}
std::cout << "Element stored in char vector are" << std::endl;
for (const auto &c : vec)
{
std::cout << c << ",";
}
std::cout << std::endl;
return vec;
}
std::vector<unsigned char> UnsignedCharArrayToVector(std::array<unsigned char, 10> number)
{
std::vector<unsigned char> vec;
for (const auto &c : number)
{
if (c != '\0')
vec.push_back(c);
}
std::cout << "Element stored in unsigned char vector are" << std::endl;
for (const auto &c : vec)
{
std::cout << c << ",";
}
std::cout << std::endl;
return vec;
}
class One
{
public:
One(std::array<char, 10> firstName, const char a, std::array<unsigned char, 10> lastName) :
m_firstName(firstName), m_A(a), m_lastName(lastName)
{
}
std::array<char, 10> FirstName()
{
return m_firstName;
}
char A()
{
return m_A;
}
std::array<unsigned char, 10> LastName()
{
return m_lastName;
}
private:
std::array<char, 10> m_firstName;
char m_A;
std::array<unsigned char, 10> m_lastName;
};
std::string ToJsonString(One &one)
{
nlohmann::json j =
{
{ "FirstName", CharArrayToVector(one.FirstName()) },
{ "A", std::string(1, one.A()) },
{ "LastName", UnsignedCharArrayToVector(one.LastName()) }
};
std::cout << j.dump(4) << std::endl;
std::string str = j.dump(4);
return str;
}
void ToObjectsOne(std::string json)
{
nlohmann::json data;
try
{
data = nlohmann::json::parse(json);
}
catch (nlohmann::json::exception& exception)
{
std::cerr << "EPS: Failed to parse line[" << json << "]";
std::cerr << "EPS: Exception[" << exception.what() << "]";
}
std::cout << " +++++++" << std::endl;
std::vector<char> vec1 = data["FirstName"].get<std::vector<char>>();
std::cout << "A: " << (data["A"].get<std::string>()) << std::endl;
std::vector<unsigned char> vec2 = data["LastName"].get<std::vector<unsigned char>>();
std::cout << "Element after deserialization in char vector are" << std::endl;
for (const auto &c : vec1)
{
std::cout << c << ",";
}
std::cout << std::endl;
std::cout << "Element after deserialization in unsigned char vector are" << std::endl;
for (const auto &c : vec2)
{
std::cout << c << ",";
}
std::cout << std::endl;
}
int main()
{
One one({ 'A', 'B', 'C', 'D' }, 'A', { '1', '2', 'A', 'B', 'C' });
std::string jsonStr = ToJsonString(one);
ToObjectsOne(jsonStr);
return 0;
}
I am getting following output for this:
Element stored in char vector are
A,B,C,D,
Element stored in unsigned char vector are
1,2,A,B,C,
{
"A": "A",
"FirstName": [
65,
66,
67,
68
],
"LastName": [
49,
50,
65,
66,
67
]
}
+++++++
A: A
Element after deserialization in char vector are
A,B,C,D,
Element after deserialization in unsigned char vector are
1,2,A,B,C,
Aucun commentaire:
Enregistrer un commentaire