Browse Source

Add string_id_value() and key_value()

Daniele Bartolini 12 năm trước cách đây
mục cha
commit
d3818590ec
2 tập tin đã thay đổi với 29 bổ sung4 xóa
  1. 23 0
      engine/core/json/JSONParser.cpp
  2. 6 4
      engine/core/json/JSONParser.h

+ 23 - 0
engine/core/json/JSONParser.cpp

@@ -236,6 +236,15 @@ void JSONElement::string_value(DynamicString& str) const
 	json::parse_string(m_at, str);
 }
 
+//--------------------------------------------------------------------------
+StringId32 JSONElement::string_id_value() const
+{
+	TempAllocator1024 alloc;
+	DynamicString str(alloc);
+	json::parse_string(m_at, str);
+	return str.to_string_id();
+}
+
 //--------------------------------------------------------------------------
 void JSONElement::array_value(List<bool>& array) const
 {
@@ -329,6 +338,20 @@ void JSONElement::array_value(Vector<DynamicString>& array) const
 	}
 }
 
+//--------------------------------------------------------------------------
+void JSONElement::key_value(Vector<DynamicString>& keys) const
+{
+	List<JSONPair> object(default_allocator());
+	json::parse_object(m_at, object);
+
+	for (uint32_t i = 0; i < object.size(); i++)
+	{
+		DynamicString key;
+		json::parse_string(object[i].key, key);
+		keys.push_back(key);
+	}
+}
+
 //--------------------------------------------------------------------------
 bool JSONElement::is_nil() const
 {

+ 6 - 4
engine/core/json/JSONParser.h

@@ -116,12 +116,11 @@ public:
 	float				float_value() const;
 
 	/// Returns the string value of the element.
-	/// @warning
-	/// The returned string is kept internally until the next call to
-	/// this function, so it is highly unsafe to just keep the pointer
-	/// instead of copying its content somewhere else.
 	void				string_value(DynamicString& str) const;
 
+	/// Returns the string id value hashed to hash::murmur2_32() of the element.
+	StringId32			string_id_value() const;
+
 	/// Returns the array value of the element.
 	/// @note
 	/// Calling this function is way faster than accessing individual
@@ -148,6 +147,9 @@ public:
 	/// @copydoc JSONElement::array_value(List<bool>&)
 	void				array_value(Vector<DynamicString>& array) const;
 
+	/// Returns all the keys of the element.
+	void				key_value(Vector<DynamicString>& keys) const;
+
 private:
 
 	const char*			m_at;