浏览代码

JSONParser APIs improvement

mikymod 12 年之前
父节点
当前提交
89d6ed3ad0
共有 3 个文件被更改,包括 98 次插入34 次删除
  1. 5 1
      src/Device.cpp
  2. 67 28
      src/JSONParser.cpp
  3. 26 5
      src/JSONParser.h

+ 5 - 1
src/Device.cpp

@@ -536,7 +536,11 @@ void Device::read_engine_settings()
 		tokens[i].print();
 	}
 
-	json.get_array("crown").get_object("boot").get_string("file");
+	
+	const char* value = json.get_array("crown").get_object("utils").get_string("file").to_string();
+
+	os::printf("Value:\t%s\n", value);
+
 }
 
 

+ 67 - 28
src/JSONParser.cpp

@@ -12,8 +12,7 @@ JSONParser::JSONParser(File* file, size_t size) :
 	m_file(file),
 	m_next_token(0),
 	m_prev_token(-1),
-	m_fetching_node(0),
-	m_fetching_step(0)
+	m_nodes_count(0)
 {
 	if (size > 1024)
 	{
@@ -26,13 +25,8 @@ JSONParser::JSONParser(File* file, size_t size) :
 
 	m_size = size;
 
-	// m_file = file;
-
 	m_pos = m_file->position();
 
-	// m_next_token = 0;
-
-	// m_prev_token = -1;
 }
 
 //--------------------------------------------------------------------------
@@ -42,12 +36,10 @@ JSONParser::~JSONParser()
 	{
 		delete m_tokens;
 	}
-
 }
 
 //--------------------------------------------------------------------------
-JSONError 
-JSONParser::parse()
+JSONError JSONParser::parse()
 {
 	JSONError error;
 	JSONToken* token;
@@ -179,8 +171,7 @@ JSONParser::parse()
 }
 
 //--------------------------------------------------------------------------
-JSONError
-JSONParser::parse_string()
+JSONError JSONParser::parse_string()
 {
 	JSONToken* token;
 
@@ -241,8 +232,7 @@ JSONParser::parse_string()
 }
 
 //--------------------------------------------------------------------------
-JSONError
-JSONParser::parse_primitive()
+JSONError JSONParser::parse_primitive()
 {
 	JSONToken* token;
 
@@ -290,8 +280,7 @@ JSONParser::parse_primitive()
 }
 
 //--------------------------------------------------------------------------
-JSONToken* 
-JSONParser::allocate_token()
+JSONToken* JSONParser::allocate_token()
 {
 	JSONToken* token;
 
@@ -345,19 +334,33 @@ int32_t JSONParser::get_tokens_number()
 //--------------------------------------------------------------------------
 JSONParser&	JSONParser::get_object(const char* key)
 {
-	for (int i = m_fetching_node; i < m_next_token; i++)
+	int32_t begin = m_nodes_count != 0 ? m_nodes[m_nodes_count-1].m_id : 0;
+	// For each token
+	for (int i = begin; i < m_next_token; i++)
 	{
+		// Check key and type
 		if ((string::strcmp(m_tokens[i].m_value, key) == 0)	&& m_tokens[i].m_type == JSON_STRING)
 		{
+			// Check if the successive token is an array
 			assert(m_tokens[i+1].m_type == JSON_OBJECT);
-			
-			m_fetching_node = m_tokens[i+1].m_id;	
 
-			os::printf("FOUND IT! node:%d\n", m_fetching_node);
+			// Store token's id in a json node
+			m_nodes[m_nodes_count].m_id = m_tokens[i+1].m_id;	
+			m_nodes[m_nodes_count].m_type = JSON_OBJECT;
+			m_nodes[m_nodes_count].print();
+
+			// If token stored has parent
+			if (m_tokens[i+1].has_parent())
+			{
+				// Check if precedent token stored is the parent of current token
+				assert(m_nodes_count && m_nodes[m_nodes_count-1].m_id == m_tokens[i+1].m_parent);
+			}
 
 			break;
 		}
 	}
+	// Incremente nodes count for the next token
+	m_nodes_count++;
 
 	return *this;
 }
@@ -365,19 +368,34 @@ JSONParser&	JSONParser::get_object(const char* key)
 //--------------------------------------------------------------------------
 JSONParser& JSONParser::get_array(const char* key)
 {
-	for (int i = m_fetching_node; i < m_next_token; i++)
+	int32_t begin = m_nodes_count != 0 ? m_nodes[m_nodes_count-1].m_id : 0;
+
+	// For each token
+	for (int i = begin; i < m_next_token; i++)
 	{
+		// Check key and type
 		if ((string::strcmp(m_tokens[i].m_value, key) == 0)	&& m_tokens[i].m_type == JSON_STRING)
 		{
+			// Check if the successive token is an array
 			assert(m_tokens[i+1].m_type == JSON_ARRAY);
-			
-			m_fetching_node = m_tokens[i+1].m_id;	
 
-			os::printf("FOUND IT! node:%d\n", m_fetching_node);
+			// Store token's id in a json node
+			m_nodes[m_nodes_count].m_id = m_tokens[i+1].m_id;	
+			m_nodes[m_nodes_count].m_type = JSON_ARRAY;
+			m_nodes[m_nodes_count].print();
+
+			// If token stored has parent
+			if (m_tokens[i+1].has_parent())
+			{
+				// Check if precedent token stored is the parent of current token
+				assert(m_nodes_count && m_nodes[m_nodes_count-1].m_id == m_tokens[i+1].m_parent);
+			}
 
 			break;
 		}
 	}
+	// Incremente nodes count for the next token
+	m_nodes_count++;
 
 	return *this;
 }
@@ -385,19 +403,34 @@ JSONParser& JSONParser::get_array(const char* key)
 //--------------------------------------------------------------------------
 JSONParser&	JSONParser::get_string(const char* key)
 {
-	for (int i = m_fetching_node; i < m_next_token; i++)
+	int32_t begin = m_nodes_count != 0 ? m_nodes[m_nodes_count-1].m_id : 0;
+
+	// For each token
+	for (int i = begin; i < m_next_token; i++)
 	{
+		// Check key and type
 		if ((string::strcmp(m_tokens[i].m_value, key) == 0)	&& m_tokens[i].m_type == JSON_STRING)
 		{
+			// Check if the successive token is an array
 			assert(m_tokens[i+1].m_type == JSON_STRING);
-			
-			m_fetching_node = m_tokens[i+1].m_id;	
 
-			os::printf("FOUND IT! node:%d\n", m_fetching_node);
+			// Store token's id in a json node
+			m_nodes[m_nodes_count].m_id = m_tokens[i+1].m_id;	
+			m_nodes[m_nodes_count].m_type = JSON_STRING;
+			m_nodes[m_nodes_count].print();
+
+			// If token stored has parent
+			if (m_tokens[i+1].has_parent())
+			{
+				// Check if precedent token stored is the parent of current token
+				assert(m_nodes_count && m_nodes[m_nodes_count-1].m_id == m_tokens[i+1].m_parent);
+			}
 
 			break;
 		}
 	}
+	// Incremente nodes count for the next token
+	m_nodes_count++;
 
 	return *this;
 }
@@ -420,4 +453,10 @@ JSONParser& JSONParser::get_bool(const char* key)
 
 }
 
+const char* JSONParser::to_string()
+{
+	return m_tokens[m_nodes[m_nodes_count-1].m_id].m_value;
+}
+
+
 } //namespace crown

+ 26 - 5
src/JSONParser.h

@@ -53,6 +53,24 @@ struct JSONToken
 		os::printf("Size:\t%d\n", m_size);
 		os::printf("\n");		
 	}
+
+	inline bool has_parent()
+	{
+		return m_parent != -1;
+	}
+};
+
+struct JSONNode
+{
+	int32_t 	m_id;
+	JSONType 	m_type;
+
+	inline void print()
+	{
+		os::printf("----------------\n");
+		os::printf("Id:\t%d\n", m_id);
+		os::printf("----------------\n");
+	}
 };
 
 /// JSONParser parses JSON file and stores all relative tokens.
@@ -84,6 +102,9 @@ public:
 
 	JSONParser&		get_bool(const char* key);
 
+	const char*		to_string();
+
+
 private:
 	/// Parse string in JSON data
 	JSONError		parse_string();
@@ -102,7 +123,6 @@ private:
 	int32_t			m_next_token;
 	/// Previous token e.g parent or array		
 	int32_t			m_prev_token;
-
 	/// JSON tokens list, used as default
 	JSONToken		m_tokens_list[1024];
 	/// JSON tokens ptr (used only if we need more then 1024 tokens)
@@ -110,10 +130,11 @@ private:
 	/// m_tokens default size, default 1024
 	size_t			m_size;
 
-	/// Stores the id of current node while fetching data
-	uint32_t		m_fetching_node;
-	/// Stores the number of steps while fetching data
-	uint32_t		m_fetching_step;
+	/// 
+	JSONNode		m_nodes[128];
+	///
+	uint32_t 		m_nodes_count;
+
 };
 
 } // namespace crown