json parser
This commit is contained in:
		
							parent
							
								
									3aa3cee61c
								
							
						
					
					
						commit
						de9a3f94f7
					
				
					 4 changed files with 156 additions and 0 deletions
				
			
		
							
								
								
									
										39
									
								
								src/File.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/File.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,39 @@
 | 
			
		|||
/*
 | 
			
		||||
 * File.cpp
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 17.10.2015
 | 
			
		||||
 *      Author: jdi
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "File.h"
 | 
			
		||||
 | 
			
		||||
std::string File::read(std::string path) {
 | 
			
		||||
	if (!fs::exists(home)) {
 | 
			
		||||
		fs::create_directory(home);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fs::ifstream in((home / path), std::ios::in | std::ios::binary);
 | 
			
		||||
	if (in) {
 | 
			
		||||
		std::string contents;
 | 
			
		||||
		in.seekg(0, std::ios::end);
 | 
			
		||||
		contents.resize(in.tellg());
 | 
			
		||||
		in.seekg(0, std::ios::beg);
 | 
			
		||||
		in.read(&contents[0], contents.size());
 | 
			
		||||
		in.close();
 | 
			
		||||
		return (contents);
 | 
			
		||||
	}
 | 
			
		||||
	return "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int File::write(std::string path, std::string content) {
 | 
			
		||||
	if (!fs::exists(home)) {
 | 
			
		||||
		fs::create_directory(home);
 | 
			
		||||
	}
 | 
			
		||||
	fs::path p = home / path;
 | 
			
		||||
	fs::ofstream file(p);
 | 
			
		||||
	file << content;
 | 
			
		||||
	file.close();
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										28
									
								
								src/File.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/File.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,28 @@
 | 
			
		|||
/*
 | 
			
		||||
 * File.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 17.10.2015
 | 
			
		||||
 *      Author: jdi
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef FILE_H_
 | 
			
		||||
#define FILE_H_
 | 
			
		||||
 | 
			
		||||
#include <boost/filesystem.hpp>
 | 
			
		||||
#include <boost/filesystem/fstream.hpp>
 | 
			
		||||
namespace fs = boost::filesystem;
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
class File {
 | 
			
		||||
public:
 | 
			
		||||
	File() {
 | 
			
		||||
		home = fs::path(getenv("HOME")) / ".smrtlink";
 | 
			
		||||
	}
 | 
			
		||||
	std::string read(std::string);
 | 
			
		||||
	int write(std::string, std::string);
 | 
			
		||||
private:
 | 
			
		||||
	fs::path home;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif /* FILE_H_ */
 | 
			
		||||
							
								
								
									
										51
									
								
								src/jsonNode.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/jsonNode.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
/*
 | 
			
		||||
 * jsonNode.cpp
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 17.10.2015
 | 
			
		||||
 *      Author: jdi
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "jsonNode.h"
 | 
			
		||||
 | 
			
		||||
jsonNode::jsonNode(const std::string &x, doc &root) {
 | 
			
		||||
	super(rapidjson::kStringType);
 | 
			
		||||
	char buffer[30];
 | 
			
		||||
	int len = sprintf(buffer, "%s", x.c_str());
 | 
			
		||||
	this->SetString(buffer, static_cast<size_t>(len), root.GetAllocator());
 | 
			
		||||
	memset(buffer, 0, sizeof(buffer));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jsonNode::jsonNode(const ipAddr &x, doc &root) {
 | 
			
		||||
	super(rapidjson::kStringType);
 | 
			
		||||
	char buffer[16];
 | 
			
		||||
	int len = sprintf(buffer, "%d.%d.%d.%d", x[0], x[1],
 | 
			
		||||
			x[2], x[3]);
 | 
			
		||||
	this->SetString(buffer, static_cast<size_t>(len), root.GetAllocator());
 | 
			
		||||
	memset(buffer, 0, sizeof(buffer));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jsonNode::jsonNode(const macAddr &x, doc &root) {
 | 
			
		||||
	super(rapidjson::kStringType);
 | 
			
		||||
	char buffer[18];
 | 
			
		||||
	int len = sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", x[0], x[1],
 | 
			
		||||
			x[2], x[3], x[4], x[5]);
 | 
			
		||||
	this->SetString(buffer, static_cast<size_t>(len), root.GetAllocator());
 | 
			
		||||
	memset(buffer, 0, sizeof(buffer));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jsonNode::jsonNode(const vlan &x, doc &root) {
 | 
			
		||||
	super(rapidjson::kObjectType);
 | 
			
		||||
	AddMember("id", x.vlan_id, root.GetAllocator());
 | 
			
		||||
	AddMember("name", jsonNode(x.name, root), root.GetAllocator());
 | 
			
		||||
	AddMember("tagged-members", jsonNode(x.tagged_member, root),
 | 
			
		||||
			root.GetAllocator());
 | 
			
		||||
	AddMember("untagged-members", jsonNode(x.untagged_member, root),
 | 
			
		||||
			root.GetAllocator());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
jsonNode::jsonNode(const port &x, doc &root) {
 | 
			
		||||
	super(rapidjson::kObjectType);
 | 
			
		||||
	AddMember("id", x.id, root.GetAllocator());
 | 
			
		||||
	AddMember("status", x.status, root.GetAllocator());
 | 
			
		||||
	AddMember("pvid", x.pvid, root.GetAllocator());
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								src/jsonNode.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								src/jsonNode.h
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,38 @@
 | 
			
		|||
/*
 | 
			
		||||
 * jsonNode.h
 | 
			
		||||
 *
 | 
			
		||||
 *  Created on: 17.10.2015
 | 
			
		||||
 *      Author: jdi
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef JSONNODE_H_
 | 
			
		||||
#define JSONNODE_H_
 | 
			
		||||
 | 
			
		||||
#include "../include/rapidjson/document.h"
 | 
			
		||||
#include "../include/rapidjson/prettywriter.h"
 | 
			
		||||
#include "Switch.h"
 | 
			
		||||
 | 
			
		||||
class jsonNode: public rapidjson::Value {
 | 
			
		||||
	typedef rapidjson::Value super;
 | 
			
		||||
	typedef rapidjson::Document doc;
 | 
			
		||||
public:
 | 
			
		||||
	jsonNode(const std::string&, doc&);
 | 
			
		||||
	jsonNode(const macAddr&, doc&);
 | 
			
		||||
	jsonNode(const ipAddr&, doc&);
 | 
			
		||||
	jsonNode(const vlan&, doc&);
 | 
			
		||||
	jsonNode(const port&, doc&);
 | 
			
		||||
 | 
			
		||||
	template<class T>
 | 
			
		||||
	jsonNode(const T &templ_arg, doc &root){
 | 
			
		||||
		std::cerr<<"Not serializable Type: "<<typeid(T).name()<<"\n";
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	template<class T>
 | 
			
		||||
	jsonNode(const std::vector<T> &x, doc &root) {
 | 
			
		||||
		super(rapidjson::kArrayType);
 | 
			
		||||
		for (T y : x)
 | 
			
		||||
			PushBack(jsonNode(y, root), root.GetAllocator());
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif /* JSONNODE_H_ */
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue