Răsfoiți Sursa

Add JSONParser::is_key_unique()

Daniele Bartolini 12 ani în urmă
părinte
comite
22abba338c
2 a modificat fișierele cu 29 adăugiri și 0 ștergeri
  1. 25 0
      engine/core/json/JSONParser.cpp
  2. 4 0
      engine/core/json/JSONParser.h

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

@@ -273,6 +273,31 @@ bool JSONElement::has_key(const char* k) const
 	return false;
 }
 
+//--------------------------------------------------------------------------
+bool JSONElement::is_key_unique(const char* k) const
+{
+	TempAllocator1024 alloc;
+	List<JSONPair> object(alloc);
+	JSONParser::parse_object(m_at, object);
+
+	bool found = false;
+
+	for (uint32_t i = 0; i < object.size(); i++)
+	{
+		if (string::strcmp(k, object[i].key) == 0)
+		{
+			if (found == true)
+			{
+				return false;
+			}
+
+			found = true;
+		}
+	}
+
+	return found;
+}
+
 //--------------------------------------------------------------------------
 bool JSONElement::bool_value() const
 {

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

@@ -80,6 +80,10 @@ public:
 	/// Returns whether the element has the @a k key.
 	bool				has_key(const char* k) const;
 
+	/// Returns whether the @a k key is unique in the object
+	/// element. If no such key is found it returns false.
+	bool				is_key_unique(const char* k) const;
+
 	/// Returns true wheter the element is the JSON nil special value.
 	bool				is_nil() const;