Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will need to implement a JSON parser in C++ to read JSON from a file. Please

ID: 3836599 • Letter: Y

Question

You will need to implement a JSON parser in C++ to read JSON from a file. Please see PlayerStatistics.json below for an example of the expected format. Simulate a player winning 5 out of 5 One Suit games, 2 out of 5 Two Suits games, and 0 out of 5 Four Suits games. Don't forget to handle updating the statistics for Fastest Win and Fewest Moves. When done, your program should save the updated information to the PlayerStatistics.json file.

[
{
"Player Name": "Me",
"Overview": {
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
"Suits":
[
{
"Type": "One Suit",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Two Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Four Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
}
]
},
{
   "Player Name": "You",
"Overview":
{
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
"Suits":
[
{
"Type": "One Suit",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Two Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Four Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
}
]
}]

Explanation / Answer

root.h

#ifndef ROOT_H
#define ROOT_H

// Should header files always be included in root.h as opposed to root.cpp?
#include <fstream>
#include <string> // Seems like I do not need to include this string container, why?
//is it because json/json.h contains it?


#include "json/json.h" // Would I ever create a class with a dependency like this one?
// jsoncpp.sourceforge.net/annotated.html

class Root
{
private:
std::ifstream m_json;

public:
Json::Value m_root;
Json::Value m_query;

Root(const std::string&);
~Root();
};

#endif // ROOT_H

root.cpp

#include "root.h"

Root::Root(const std::string & filename) : m_json(filename, std::ifstream::binary)
// Is std::ifstream::binary ok to put it in here like this ^?
// It's working, but would that be good practice?
{
m_json >> m_root;
m_json.close(); // Do I need .close() here?

}
Root::~Root(){}

main.cpp

#include <iostream>
#include "root.h"

int main()
{

Root rate("test.json");
rate.m_query = rate.m_root["query"]["items"]; // Is it ok to assign member to a member like so,
// as opposed to just a variable?

// How can I instantiate my object like the line below?
// Root rate("test.json", ["query"]["results"]["rate"]);
// Syntax does not have to match precisely?


for(const auto & it : rate.m_query)
{
std::cout << it << std::endl;
}
}

test.json

[
{
"Player Name": "Me",
"Overview": {
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
"Suits":
[
{
"Type": "One Suit",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Two Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Four Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
}
]
},
{
   "Player Name": "You",
"Overview":
{
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
"Suits":
[
{
"Type": "One Suit",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Two Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
},
{
"Type": "Four Suits",
"Games Won": 0,
"Win Rate": 0,
"Games Played": 0,
"Fastest Win": 0,
"Fewest Moves": 0,
"Top Score": 0
}
]
}]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote