0

I'm currently learning c++ and a tried something with JSON.

Here’s my code :

#include "main.hpp"

#define MAX_USERS_COUNT 5000

using json = nlohmann::json;

namespace user {
    struct datas {
        int ID;
        std::string PIN;
        std::string name;
        std::string email;
        int age;
    };

    // convert datas to JSON
    void to_json(nlohmann::json& json, const user::datas& datas) {
        json = nlohmann::json { {"ID", datas.ID}, {"PIN", datas.PIN}, /*{"name", datas.name},*/ {"email", datas.email}, {"age", datas.age} };
    }

    // convert JSON to datas
    void from_json(const nlohmann::json& json, user::datas& datas) {
        json.at("ID").get_to(datas.ID);
        json.at("PIN").get_to(datas.PIN);
        /*json.at("name").get_to(datas.name);*/
        json.at("email").get_to(datas.email);
        json.at("age").get_to(datas.age);
    }
}

nlohmann::json convertDatasToJSON(nlohmann::json&, user::datas, std::string);

int main(int argc, char *argv[]) {
    user::datas prototype[MAX_USERS_COUNT]; // create an array of users with a max count
    nlohmann::json jsonDatas;

    std::ifstream namesFile("./res/files/learnjson/names.txt");
    std::string line;
    std::vector<std::string> namesList = {};
    while(getline(namesFile, line)) {
        namesList.push_back(line);
    }

    for(int i = 0; i < 5; i++) {
        int index = rand() % namesList.size() + 1;

        prototype[i] = {
            i,
            std::to_string(rand() % 999999999 + 100000000), // generate a number from 100000000 to 999999999
            namesList[index], // ex: "name"
            namesList[index] + "@e-mail.com",
            rand() % 100 + 1 // generate a number from 1 to 100
        };

        convertDatasToJSON(jsonDatas, prototype[i], "users");
    }

    std::cout << std::setw(4) << jsonDatas << std::endl;

    namesFile.close();
    return 0;
}

// convert user datas to JSON
nlohmann::json convertDatasToJSON(nlohmann::json& json, user::datas datas, std::string jsonLocation) {
    json[jsonLocation] [datas.name] = datas;
    return json;
}

The file of names list i'm using is made like that:

Kikelia
Kiker
Kiki
Kila
Kilah
Kilan
Kilar
Kilbride
Kilby
Kile
Kiley
Kilgore
Kilian
Kilk
Killam
Killarney
Killen

Everything works perfectly instead of that (this is how my json is generated):

{
    "users": {
        "Elayne\r": {
            "ID": 4,
            "PIN": "174243042",
            "age": 88,
            "email": "Elayne\r@e-mail.com"
        },
        "Essie\r": {
            "ID": 2,
            "PIN": "557850879",
            "age": 24,
            "email": "Essie\r@e-mail.com"
        },
        "Fowler\r": {
            "ID": 3,
            "PIN": "923564440",
            "age": 66,
            "email": "Fowler\r@e-mail.com"
        },
        "O'Rourke\r": {
            "ID": 1,
            "PIN": "244108931",
            "age": 73,
            "email": "O'Rourke\r@e-mail.com"
        },
        "Rasmussen\r": {
            "ID": 0,
            "PIN": "382475249",
            "age": 74,
            "email": "Rasmussen\r@e-mail.com"
        }
    }
}

Can someone help me and explain why those "\r" appear please ? Thanks in advance !! Have a nice day !!

miishuriinu
  • 117
  • 1
  • 9

0 Answers0