瀏覽代碼

Delete JSONParser

Daniele Bartolini 10 年之前
父節點
當前提交
a087f7d19d
共有 2 個文件被更改,包括 0 次插入679 次删除
  1. 0 481
      src/core/json/json_parser.cpp
  2. 0 198
      src/core/json/json_parser.h

+ 0 - 481
src/core/json/json_parser.cpp

@@ -1,481 +0,0 @@
-/*
- * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "json_parser.h"
-#include "sjson.h"
-#include "temp_allocator.h"
-#include "string_utils.h"
-#include "vector.h"
-#include "map.h"
-#include "vector2.h"
-#include "vector3.h"
-#include "vector4.h"
-#include "quaternion.h"
-#include "matrix4x4.h"
-#include "file.h"
-
-namespace crown
-{
-
-JSONElement::JSONElement()
-	: _at(NULL)
-{
-}
-
-JSONElement::JSONElement(const char* at)
-	: _at(at)
-{
-}
-
-JSONElement::JSONElement(const JSONElement& other)
-	: _at(other._at)
-{
-}
-
-JSONElement& JSONElement::operator=(const JSONElement& other)
-{
-	// Our begin is the other's at
-	_at = other._at;
-	return *this;
-}
-
-JSONElement JSONElement::operator[](uint32_t i)
-{
-	Array<const char*> array(default_allocator());
-
-	sjson::parse_array(_at, array);
-
-	CE_ASSERT(i < array::size(array), "Index out of bounds");
-
-	return JSONElement(array[i]);
-}
-
-JSONElement JSONElement::index(uint32_t i)
-{
-	return this->operator[](i);
-}
-
-JSONElement JSONElement::index_or_nil(uint32_t i)
-{
-	if (_at != NULL)
-	{
-		Array<const char*> array(default_allocator());
-
-		sjson::parse_array(_at, array);
-
-		if (i >= array::size(array))
-		{
-			return JSONElement();
-		}
-
-		return JSONElement(array[i]);
-	}
-
-	return JSONElement();
-}
-
-JSONElement JSONElement::key(const char* k)
-{
-	Map<DynamicString, const char*> object(default_allocator());
-	sjson::parse(_at, object);
-
-	const char* value = map::get(object, DynamicString(k), (const char*) NULL);
-	CE_ASSERT(value != NULL, "Key not found: '%s'", k);
-
-	return JSONElement(value);
-}
-
-JSONElement JSONElement::key_or_nil(const char* k)
-{
-	if (_at != NULL)
-	{
-		Map<DynamicString, const char*> object(default_allocator());
-		sjson::parse(_at, object);
-
-		const char* value = map::get(object, DynamicString(k), (const char*) NULL);
-
-		if (value)
-			return JSONElement(value);
-	}
-
-	return JSONElement();
-}
-
-bool JSONElement::has_key(const char* k) const
-{
-	Map<DynamicString, const char*> object(default_allocator());
-	sjson::parse(_at, object);
-
-	return map::has(object, DynamicString(k));
-}
-
-bool JSONElement::to_bool(bool def) const
-{
-	return is_nil() ? def : sjson::parse_bool(_at);
-}
-
-int32_t JSONElement::to_int(int32_t def) const
-{
-	return is_nil() ? def : sjson::parse_int(_at);
-}
-
-float JSONElement::to_float(float def) const
-{
-	return is_nil() ? def : sjson::parse_float(_at);
-}
-
-DynamicString JSONElement::to_string(const char* def) const
-{
-	DynamicString str(default_allocator());
-
-	if (is_nil())
-		str = def;
-	else
-		sjson::parse_string(_at, str);
-
-	return str;
-}
-
-Vector2 JSONElement::to_vector2(const Vector2& def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator64 alloc;
-	Array<const char*> array(alloc);
-	sjson::parse_array(_at, array);
-
-	return vector2(sjson::parse_float(array[0]),
-		sjson::parse_float(array[1]));
-}
-
-Vector3 JSONElement::to_vector3(const Vector3& def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator64 alloc;
-	Array<const char*> array(alloc);
-	sjson::parse_array(_at, array);
-
-	return vector3(sjson::parse_float(array[0]),
-		sjson::parse_float(array[1]),
-		sjson::parse_float(array[2]));
-}
-
-Vector4 JSONElement::to_vector4(const Vector4& def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator64 alloc;
-	Array<const char*> array(alloc);
-	sjson::parse_array(_at, array);
-
-	return vector4(sjson::parse_float(array[0]),
-		sjson::parse_float(array[1]),
-		sjson::parse_float(array[2]),
-		sjson::parse_float(array[3]));
-}
-
-Quaternion JSONElement::to_quaternion(const Quaternion& def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator64 alloc;
-	Array<const char*> array(alloc);
-	sjson::parse_array(_at, array);
-
-	const Vector3 axis = vector3(sjson::parse_float(array[0])
-		, sjson::parse_float(array[1])
-		, sjson::parse_float(array[2])
-		);
-	const float angle = sjson::parse_float(array[3]);
-
-	return quaternion(axis, angle);
-}
-
-Matrix4x4 JSONElement::to_matrix4x4(const Matrix4x4& def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator128 alloc;
-	Array<float> array(alloc);
-	to_array(array);
-
-	return matrix4x4(array::begin(array));
-}
-
-StringId32 JSONElement::to_string_id(const StringId32 def) const
-{
-	if (is_nil())
-		return def;
-
-	TempAllocator1024 alloc;
-	DynamicString str(alloc);
-	sjson::parse_string(_at, str);
-	return str.to_string_id();
-}
-
-ResourceId JSONElement::to_resource_id() const
-{
-	DynamicString str(default_allocator());
-	sjson::parse_string(_at, str);
-	return ResourceId(str.c_str());
-}
-
-void JSONElement::to_array(Array<bool>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, sjson::parse_bool(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Array<int16_t>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, (int16_t)sjson::parse_int(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Array<uint16_t>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, (uint16_t)sjson::parse_int(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Array<int32_t>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, (int32_t)sjson::parse_int(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Array<uint32_t>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, (uint32_t)sjson::parse_int(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Array<float>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		array::push_back(array, sjson::parse_float(temp[i]));
-	}
-}
-
-void JSONElement::to_array(Vector<DynamicString>& array) const
-{
-	Array<const char*> temp(default_allocator());
-
-	sjson::parse_array(_at, temp);
-
-	for (uint32_t i = 0; i < array::size(temp); i++)
-	{
-		TempAllocator256 ta;
-		DynamicString str(ta);
-		sjson::parse_string(temp[i], str);
-		vector::push_back(array, str);
-	}
-}
-
-void JSONElement::to_keys(Vector<DynamicString>& keys) const
-{
-	Map<DynamicString, const char*> object(default_allocator());
-	sjson::parse(_at, object);
-
-	const Map<DynamicString, const char*>::Node* it = map::begin(object);
-	while (it != map::end(object))
-	{
-		vector::push_back(keys, (*it).pair.first);
-		it++;
-	}
-}
-
-bool JSONElement::is_nil() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::NIL;
-	}
-
-	return true;
-}
-
-bool JSONElement::is_bool() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::BOOL;
-	}
-
-	return false;
-}
-
-bool JSONElement::is_number() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::NUMBER;
-	}
-
-	return false;
-}
-
-bool JSONElement::is_string() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::STRING;
-	}
-
-	return false;
-}
-
-bool JSONElement::is_array() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::ARRAY;
-	}
-
-	return false;
-}
-
-bool JSONElement::is_object() const
-{
-	if (_at != NULL)
-	{
-		return sjson::type(_at) == JsonValueType::OBJECT;
-	}
-
-	return false;
-}
-
-uint32_t JSONElement::size() const
-{
-	if (_at == NULL)
-	{
-		return 0;
-	}
-
-	switch(sjson::type(_at))
-	{
-		case JsonValueType::NIL:
-		{
-			return 1;
-		}
-		case JsonValueType::OBJECT:
-		{
-			Map<DynamicString, const char*> object(default_allocator());
-			sjson::parse(_at, object);
-			return map::size(object);
-		}
-		case JsonValueType::ARRAY:
-		{
-			Array<const char*> array(default_allocator());
-			sjson::parse_array(_at, array);
-			return array::size(array);
-		}
-		case JsonValueType::STRING:
-		{
-			TempAllocator256 ta;
-			DynamicString string(ta);
-			sjson::parse_string(_at, string);
-			return string.length();
-		}
-		case JsonValueType::NUMBER:
-		{
-			return 1;
-		}
-		case JsonValueType::BOOL:
-		{
-			return 1;
-		}
-		default:
-		{
-			CE_FATAL("Oops, unknown value type");
-			return 0;
-		}
-	}
-}
-
-JSONParser::JSONParser(const char* s)
-	: _file(false)
-	, _document(s)
-{
-	CE_ASSERT_NOT_NULL(s);
-}
-
-JSONParser::JSONParser(File& f)
-	: _file(true)
-	, _document(NULL)
-{
-	const uint32_t size = f.size();
-	char* doc = (char*) default_allocator().allocate(size + 1);
-	f.read(doc, size);
-	doc[size] = '\0';
-	_document = doc;
-}
-
-JSONParser::JSONParser(Buffer& b)
-	: _file(false)
-	, _document(NULL)
-{
-	array::push_back(b, '\0');
-	_document = array::begin(b);
-}
-
-JSONParser::~JSONParser()
-{
-	if (_file)
-	{
-		default_allocator().deallocate((void*) _document);
-	}
-}
-
-JSONElement JSONParser::root()
-{
-	const char* ch = _document;
-	while ((*ch) && (*ch) <= ' ') ch++;
-
-	return JSONElement(ch);
-}
-
-} //namespace crown

+ 0 - 198
src/core/json/json_parser.h

@@ -1,198 +0,0 @@
-/*
- * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "types.h"
-#include "container_types.h"
-#include "math_types.h"
-#include "macros.h"
-#include "string_id.h"
-
-namespace crown
-{
-
-class JSONParser;
-class DynamicString;
-class File;
-
-/// Represents a JSON element.
-/// The objects of this class are valid until the parser
-/// which has generated them, will exist.
-///
-/// @ingroup JSON
-class JSONElement
-{
-public:
-
-	/// Construct the nil JSONElement.
-	/// Used to forward-instantiate elements or as a special
-	/// nil element.
-	JSONElement();
-	explicit JSONElement(const char* at);
-	JSONElement(const JSONElement& other);
-
-	JSONElement& operator=(const JSONElement& other);
-
-	/// Returns the @a i -th item of the current array.
-	JSONElement operator[](uint32_t i);
-
-	/// @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
-	/// If the key is not unique in the object scope, the last
-	/// 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;
-
-	/// Returns true wheter the element is the JSON nil special value.
-	bool is_nil() const;
-
-	/// Returns true wheter the element is a JSON boolean (true or false).
-	bool is_bool() const;
-
-	/// Returns true wheter the element is a JSON number.
-	bool is_number() const;
-
-	/// Returns true whether the element is a JSON string.
-	bool is_string() const;
-
-	/// Returns true whether the element is a JSON array.
-	bool is_array() const;
-
-	/// Returns true whether the element is a JSON object.
-	bool is_object() const;
-
-	/// Returns the size of the element based on the
-	/// element's type:
-	/// * nil, bool, number: 1
-	/// * string: length of the string
-	/// * array: number of elements in the array
-	/// * object: number of keys in the object
-	uint32_t size() const;
-
-	/// Returns the boolean value of the element.
-	bool to_bool(bool def = false) const;
-
-	/// Returns the integer value of the element.
-	int32_t to_int(int32_t def = 0) const;
-
-	/// Returns the float value of the element.
-	float to_float(float def = 0) const;
-
-	/// Returns the string value of the element.
-	DynamicString to_string(const char* def = "") const;
-
-	/// Returns the Vector2 value of the element.
-	/// @note Vector2 = [x, y]
-	Vector2 to_vector2(const Vector2& def = VECTOR2_ZERO) const;
-
-	/// Returns the Vector3 value of the element.
-	/// @note Vector3 = [x, y, z]
-	Vector3 to_vector3(const Vector3& def = VECTOR3_ZERO) const;
-
-	/// Returns the Vector4 value of the element.
-	/// @note Vector4 = [x, y, z, w]
-	Vector4 to_vector4(const Vector4& def = VECTOR4_ZERO) const;
-
-	/// Returns the Quaternion value of the element.
-	/// @note Quaternion = [x, y, z, w]
-	Quaternion to_quaternion(const Quaternion& def = QUATERNION_IDENTITY) const;
-
-	/// Returns the Matrix4x4 value of the element.
-	/// @note Matrix4x4 = [x, x, x, x, y, y, y, y, z, z, z, z, t, t, t, t]
-	Matrix4x4 to_matrix4x4(const Matrix4x4& def = MATRIX4X4_IDENTITY) const;
-
-	/// Returns the string id value hashed to murmur32() of the element.
-	StringId32 to_string_id(const StringId32 def = StringId32(uint32_t(0))) const;
-
-	/// Returns the resource id value of the element.
-	ResourceId to_resource_id() const;
-
-	/// Returns the array value of the element.
-	/// @note
-	/// Calling this function is way faster than accessing individual
-	/// array elements by JSONElement::operator[] and it is the very preferred way
-	/// for retrieving array elemets. However, you have to be sure that the array
-	/// contains only items of the given @array type.
-	void to_array(Array<bool>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Array<int16_t>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Array<uint16_t>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Array<int32_t>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Array<uint32_t>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Array<float>& array) const;
-
-	/// @copydoc JSONElement::to_array(Array<bool>&)
-	void to_array(Vector<DynamicString>& array) const;
-
-	/// Returns all the keys of the element.
-	void to_keys(Vector<DynamicString>& keys) const;
-
-private:
-
-	const char* _at;
-
-	friend class JSONParser;
-};
-
-/// Parses JSON documents.
-///
-/// @ingroup JSON
-class JSONParser
-{
-public:
-
-	/// Reads the JSON document contained in the non-null @a s string.
-	/// @note
-	/// The @a s string has to remain valid for the whole parser's
-	/// existence scope.
-	JSONParser(const char* s);
-
-	/// Reads the JSON document from file @a f.
-	JSONParser(File& f);
-
-	JSONParser(Buffer& b);
-
-	~JSONParser();
-
-	/// Returns the root element of the JSON document.
-	JSONElement root();
-
-private:
-
-	bool _file;
-	const char* _document;
-
-private:
-
-	// Disable copying
-	JSONParser(const JSONParser&);
-	JSONParser& operator=(const JSONParser&);
-};
-
-} // namespace crown