Răsfoiți Sursa

Add index_or_nil() and key_or_nil()

Daniele Bartolini 12 ani în urmă
părinte
comite
e9e45c6cd6
2 a modificat fișierele cu 71 adăugiri și 5 ștergeri
  1. 59 0
      engine/core/json/JSONParser.cpp
  2. 12 5
      engine/core/json/JSONParser.h

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

@@ -242,6 +242,28 @@ JSONElement& JSONElement::index(uint32_t i)
 	return this->operator[](i);
 }
 
+//--------------------------------------------------------------------------
+JSONElement JSONElement::index_or_nil(uint32_t i)
+{
+	if (m_at != NULL)
+	{
+		TempAllocator1024 alloc;
+		List<const char*> array(alloc);
+
+		JSONParser::parse_array(m_begin, array);
+
+		if (i >= array.size())
+		{
+			return JSONElement();
+		}
+
+		m_at = array[i];
+		return *this;
+	}
+
+	return JSONElement();
+}
+
 //--------------------------------------------------------------------------
 JSONElement& JSONElement::key(const char* k)
 {
@@ -271,6 +293,43 @@ JSONElement& JSONElement::key(const char* k)
 	return *this;
 }
 
+//--------------------------------------------------------------------------
+JSONElement JSONElement::key_or_nil(const char* k)
+{
+	if (m_at != NULL)
+	{
+		TempAllocator1024 alloc;
+		List<JSONPair> object(alloc);
+
+		JSONParser::parse_object(m_begin, object);
+
+		bool found = false;
+
+		for (uint32_t i = 0; i < object.size(); i++)
+		{
+			TempAllocator256 key_alloc;
+			List<char> key(key_alloc);
+
+			JSONParser::parse_string(object[i].key, key);
+
+			if (string::strcmp(k, key.begin()) == 0)
+			{
+				m_at = object[i].val;
+				found = true;
+			}
+		}
+
+		if (!found)
+		{
+			return JSONElement();
+		}
+
+		return *this;
+	}
+
+	return JSONElement();
+}
+
 //--------------------------------------------------------------------------
 bool JSONElement::has_key(const char* k) const
 {

+ 12 - 5
engine/core/json/JSONParser.h

@@ -58,10 +58,9 @@ class JSONElement
 {
 public:
 
-	/// Used only to forward-instantiate elements.
-	/// In order to be able to use the element, it must be
-	/// obtained from JSONParser::root() or copied from an
-	/// already existent and valid element.
+	/// Construct the nil JSONElement.
+	/// Used to forward-instantiate elements or as a special
+	/// nil element.
 						JSONElement();
 						JSONElement(const JSONElement& other);
 
@@ -70,9 +69,13 @@ public:
 	/// Returns the @a i -th item of the current array.
 	JSONElement&		operator[](uint32_t i);
 
-	/// @copydoc JSONParser::operator[]
+	/// @copydoc JSONElement::operator[]
 	JSONElement&		index(uint32_t i);
 
+	/// Returns the @a i -th item of the current array or
+	/// the special nil JSONElement() if the index does not exist.
+	JSONElement			index_or_nil(uint32_t i);
+
 	/// Returns the element corresponding to key @a k of the
 	/// current object.
 	/// @note
@@ -80,6 +83,10 @@ public:
 	/// key in order of appearance will be selected.
 	JSONElement&		key(const char* k);
 
+	/// Returns the element corresponding to key @a k of the current
+	/// object, or the special nil JSONElement() if the key does not exist.
+	JSONElement			key_or_nil(const char* k);
+
 	/// Returns whether the element has the @a k key.
 	bool				has_key(const char* k) const;