json c++

Started by
11 comments, last by Misantes 9 years, 6 months ago


This is a reason why tutorials exist. A (good) tutorial explains how to use what in which situations, while a documentation enumerates all possibilities without regarding use cases much. Tutorials are much more suitable for learning, and documentation is good for looking up details or things learned earlier but forgotten for now.

Yeah, I've been unable to find any jsoncpp tutorials though. The couple I've come across have been for rather specific things within jsoncpp and weren't terribly useful. I've come across plenty for writing JSON and c++ IO functions in general, and they've been quite useful the last couple days.

Though, I just realized I'd only been searching Duckduckgo.com, which wasn't returning anything really useful, but I just tried google and it turned up a few tutorials I'll take a look at later. Kind of a common problem with duckduckgo...cest la vie.

Beginner here <- please take any opinions with grain of salt

Advertisement

Maybe this function will benefit you:


#include <fstream>
#include <stringstream>

//Loads the entire file, newlines and all, and returns its contents as a string.
//Returns an empty string if the file doesn't exist.
std::string LoadFileAsString(const std::string &filename, bool *successful = nullptr)
{
	if(successful) *successful = false;

	//Open the file for reading.
	std::ifstream file(filename.c_str());
	if(!file)
	{
		//Insert your own error handling here.
                /*
		Log::Message(MSG_SOURCE("FileFunctions", Log::Severity::Error))
                            << "Failed to load '" << Log_HighlightCyan(GetFilenameFromPath(filename)) << "' at " << Log_DisplayPath(filename)
			    << "\nDo I have sufficient privileges? Does the file even exist?" << Log::FlushStream;
                */

		return "";
	}

	//Read the file into the stringStream.
	std::ostringstream stringStream;
	stringStream << file.rdbuf();

	if(successful) *successful = true;

        //Convert the stringStream into a regular string.
	return stringStream.str();
}

It's just a helper function I use alot in my own code. You can then take the string and pass it to whatever parsing function you want.

You can then go:


bool successful = false;
std::string json_example = LoadFileAsString("../file.txt", &successful);

if(!successful)
{
   //Insert proper error handling here.
   return;
}

Json::Value root;
Json::Reader reader;
bool parsedSuccess = reader.parse(json_example, root, false);

if(!parsedSuccess)
{
    //Insert proper error handling here.
    return;
}

Don't use "not", use '!'. While 'not' and 'and','or', and things like that are all standard C++, the vast majority of C++ programmers use ! && || and so on, so that's the way you need to learn it.

The reason why jsoncpp doesn't have file loading functions (or, seems to not - I haven't used that library before), is because it doesn't make sense for every library everywhere to re-invent the wheel with file-loading functions, when instead you can just use standard file-loading functions, and pass the loaded results to the 3rd-party libraries as needed. Part of programming is getting different functions from different libraries, and using them together like lego bricks to build more complex programs.


Don't use "not", use '!'. While 'not' and 'and','or', and things like that are all standard C++, the vast majority of C++ programmers use ! && || and so on, so that's the way you need to learn it.

I'll admit to some copy/pasting here tongue.png Generally I use !, &&, ||. However, In my test files, it's just quicker to copy/paste things from a tutorial and then play with them until I understand them, see how things are working. Though, copy/pasting isn't something I do in my programs typically, unless it's from another program of mine.

Servant, while I have your ear, and you seem to be familiar with jsoncpp, the example you gave uses the string parse function. Is there a way to use the istream parsing function without first getting strings of all the lines beforehand? In my example, I'm trying to use the istream parse function, however, unless I manually getline() on each line, it returns NULL (in which case, I may as well just use the string parse, per your example). It's not a giant deal, but it could cut down on some unnecessary code for me.

It's possible that that function doesn't actually do what i'm wanting it to do, which is parse the file without converting it to a string first. And it's a little of a moot point, since it's not a lot of extra code to convert to a string and I'll be running this at startup/shutdown.

edit* just re-read your comment, and you mention you're actually not familiar with it, so no worries. However, if anyone IS familiar with jsoncpp and knows the answer to this, I'm all ears smile.png

edit** Ugh, actually the istream parse function works exactly like expected, I just had my json file formatted incorrectly. Good times :P Anyhow, I think for the moment, I have everything more or less sorted out to move forward. Thanks for the help and advice everyone. I appreciate it :)

Beginner here <- please take any opinions with grain of salt

This topic is closed to new replies.

Advertisement