8 Replies - 3549 Views - Last Post: 06 August 2012 - 12:59 PM Rate Topic: -----

#1 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Help with parsing json

Posted 06 August 2012 - 06:52 AM

Hey all,


Hi here is what I'm trying to accomplish within the next few hours, I need to create an executable that meets the following criteria.

1. Passed two arguments at command line (-url <URL>, -p <path>)
2. Using the path provided in the command line, I need to read the the data in the file (will be in json format)
3. I will then need to assign 2 specific values to an element in that file. After which I will need to re-write the file.

That's pretty much the jist of it.

I've done some research on json parsers and I have found boost, jannsson, ccan, and boost spirit to all have a json parsing module built-in within their libraries. The main task however is accessing the values of 2 specific elements within the data and modify them!

Does anyone know of a good parser and can maybe point me in the right directinon? (A mod thought I was just asking for code....)


Best regards,
kyle

Here's what I have so far
#ifndef BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED
#define BOOST_PROPERTY_TREE_JSON_PARSER_HPP_INCLUDED

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/detail/json_parser_read.hpp>
#include <boost/property_tree/detail/json_parser_write.hpp>
#include <boost/property_tree/detail/json_parser_error.hpp>

#include <fstream>
#include <string>
#include <locale>
#include "stdafx.h"
#include <iostream>


int main(int argc, char *argv[])
{
	if (argc < 2) { // Check the value of argc. If not enough parameters have been passed, inform user and exit.
        std::cout << "Usage is  -p <path>\n"; // Inform the user of how to use the program
        std::cin.get();
        exit(0);
	}
	else { // if we have enough parameters
		char* path;
		std::cout << argv[0];
		if (argv[1] == "-p") { // checks for proper syntax
			path = argv[1]; // parse the path as a local variable
		}
		else
		{
			std::cout << "Invalid arguments, please try again.\n";
			exit(0);
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with parsing json

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Help with parsing json

Posted 06 August 2012 - 07:16 AM

I used jsoncpp in a past job with success.
Was This Post Helpful? 1
  • +
  • -

#3 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: Help with parsing json

Posted 06 August 2012 - 07:36 AM

Jsoncpp requires python to be installed though right? I'm trying to find a parser with the least amount of dependencies possible :/
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Help with parsing json

Posted 06 August 2012 - 07:41 AM

Python is only required for building the JsonCpp library. Once it's built, Python is no longer required.
Was This Post Helpful? 1
  • +
  • -

#5 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: Help with parsing json

Posted 06 August 2012 - 07:45 AM

Ahh okay that's not too bad then.
Was This Post Helpful? 0
  • +
  • -

#6 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: Help with parsing json

Posted 06 August 2012 - 08:10 AM

Can you teach me how to use it? I just installed python and I would like to know how to build it

I've been following the instructions in the readme file. I have python installed and I have unzipped scons into the same directory as the readme file
Was This Post Helpful? 0
  • +
  • -

#7 justanub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-May 12

Re: Help with parsing json

Posted 06 August 2012 - 12:10 PM

I actually switched to rapidjson for parsing, however I am having some issues. I can't seem to be able to the inout stream to work.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "rapidjson/document.h"		// rapidjson's DOM-style API
#include "rapidjson/prettywriter.h"	// for stringify JSON
#include "rapidjson/filestream.h"	// wrapper of C stream for prettywriter as output
#include "rapidjson/reader.h"
#include <cstdio>


using namespace std;
using namespace rapidjson;

int main( int argc, char *argv[])
{
	if( argc != 3) 
		cout << "usage: " << argv[0] <<" <filename>, <url>\n";
	else 
	{
		ifstream the_file ( argv[1] );
		if (!the_file.is_open() )
			cout << "Could not open file\n";
		else 
		{
			//Prepare reader for input stream
			Reader reader;
			Document document;
			if (document.ParseStream<0>(the_file).HasParseError())
			{
				return 1;
			}
			else
			{
				char buffer[sizeof(the_file)];
				memcpy(buffer, the_file, sizeof(the_file));
				if (document.ParseInsitu<0>(buffer).HasParseError())
				return 1;
			}
			cout << "\nParsing to document succeeded!\n";
		}
		
	}
	
}



I get error Error

Quote

error C2039: 'Tell' : is not a member of 'std::basic_ifstream<_Elem,_Traits>' c:\users\felipo11.kylemarimon-pc\documents\visual studio 2010\projects\cmdargs_test\cmdargs_test\rapidjson\reader.h 223 1 cmdargs_test


and

Quote

Error 3 error C2039: 'Peek' : is not a member of 'std::basic_ifstream<_Elem,_Traits>' c:\users\felipo11.kylemarimon-pc\documents\visual studio 2010\projects\cmdargs_test\cmdargs_test\rapidjson\reader.h 225 1 cmdargs_test

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4070
  • View blog
  • Posts: 12,557
  • Joined: 25-December 09

Re: Help with parsing json

Posted 06 August 2012 - 12:59 PM

Please explain what you think the following lines:

ifstream the_file ( argv[1] );
.....
char buffer[sizeof(the_file)];
memcpy(buffer, the_file, sizeof(the_file));


Do you know that in C++ the size of an array must be a compile time constant?

Do you think the memcpy routine is copying the file to your buffer?

What value do you think sizeof(the_file) will return?


If you answered yes to question 1 see this link: C++ Arrays And from that link:

Quote

Therefore, in order to declare an array called billy as the one shown in the above diagram it is as simple as:

int billy [5];

NOTE: The elements field within brackets [] which represents the number of elements the array is going to hold, must be a constant value, since arrays are blocks of non-dynamic memory whose size must be determined before execution. In order to create arrays with a variable length dynamic memory is needed, which is explained later in these tutorials.


If you answered yes to question 2, then you should probably study up on how to read files in C/C++. Basic C++ file IO

See this link for question 3, sizeof(). The sizeof() function reports the size of an array, not a C++ class (an ifstream).

Jim
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Help with parsing json

Posted 06 August 2012 - 12:59 PM

No offense, but you might want to consider learning C++ first. Your code is very wrong.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1