Explorar el Código

Added a new json constructor to read from a file.

Frank Zago hace 14 años
padre
commit
f6c39eed0c
Se han modificado 2 ficheros con 14 adiciones y 2 borrados
  1. 10 0
      lib/JsonNode.cpp
  2. 4 2
      lib/JsonNode.h

+ 10 - 0
lib/JsonNode.cpp

@@ -4,6 +4,7 @@
 #include <assert.h>
 #include <fstream>
 #include <sstream>
+#include <iostream>
 
 const JsonNode JsonNode::nullNode;
 
@@ -19,6 +20,15 @@ JsonNode::JsonNode(std::string input):
 	JsonParser parser(input, *this);
 }
 
+JsonNode::JsonNode(const char *filename)
+{
+	std::ifstream file(filename);
+	std::string str((std::istreambuf_iterator<char>(file)),
+					std::istreambuf_iterator<char>());
+
+	JsonParser parser(str, *this);
+}
+
 JsonNode::JsonNode(const JsonNode &copy):
 	type(DATA_NULL)
 {

+ 4 - 2
lib/JsonNode.h

@@ -42,6 +42,8 @@ public:
 	JsonNode(JsonType Type = DATA_NULL);
 	//Create tree from Json-formatted input
 	explicit JsonNode(std::string input);
+	//Create tree from JSON file
+ 	JsonNode(const char *filename);
 	//Copy c-tor
 	JsonNode(const JsonNode &copy);
 
@@ -56,7 +58,7 @@ public:
 
 	bool isNull() const;
 
-	//non-const acessors, node will change type on type mismatch
+	//non-const accessors, node will change type on type mismatch
 	bool & Bool();
 	int & Int();
 	float & Float();
@@ -64,7 +66,7 @@ public:
 	JsonVector & Vector();
 	JsonMap & Struct();
 
-	//const acessors, will cause assertion failure on type mismatch
+	//const accessors, will cause assertion failure on type mismatch
 	const bool & Bool() const;
 	const int & Int() const;
 	const float & Float() const;