/**
 *	Json library
 */
#Const	Version			"2017-05-03"
#Const	ScriptName	"Libs/Nadeo/Json2.Script.txt"

// ---------------------------------- //
// Libraries
// ---------------------------------- //
#Include "TextLib" as TL

// ---------------------------------- //
// Functions
// ---------------------------------- //
// ---------------------------------- //
// Public
// ---------------------------------- //
// ---------------------------------- //
/**	Return the version number of the script
 *
 *	@return														The version number of the script
 */
Text GetScriptVersion() {
	return Version;
}

// ---------------------------------- //
/**	Return the name of the script
 *
 *	@return														The name of the script
 */
Text GetScriptName() {
	return ScriptName;
}

// ---------------------------------- //
/** Escape special characters for JSON string
 *
 *	@param	_Text											The text to clean
 *
 *	@return														The cleaned text
 */
Text EscapeText(Text _Text) {
	return TL::Replace(
		TL::Replace(
			TL::Replace(
				_Text, "\\", "\\\\"
			), "/", "\\/"
		), "\"", "\\\""
	);
}

// ---------------------------------- //
/** Convert a Text into a JSON string
 *
 *	@param	_Text											The Text to convert
 *
 *	@return														The converted Text
 */
Text GetText(Text _Text) {
	return dump(EscapeText(_Text));
}

// ---------------------------------- //
/** Convert an array of Text in a JSON string
 *
 *	@param	_TextArray								The array of Text to convert
 *
 *	@return														The converted array of Text
 */
Text GetTextArray(Text[] _TextArray) {
	declare TextArray = "[";
	declare NotFirst = False;
	foreach (Element in _TextArray) {
		if (!NotFirst) {
			NotFirst = True;
		} else {
			TextArray ^= ", ";
		}
		TextArray ^= GetText(Element);
	}
	TextArray ^= "]";
	
	return TextArray;
}

// ---------------------------------- //
/** Convert a Real into a JSON string
 *
 *	@param	_Real											The Real to convert
 *
 *	@return														The converted Real
 */
Text GetReal(Real _Real) {
	return _Real^"0";
}

// ---------------------------------- //
/** Convert a Integer into a JSON string
 *
 *	@param	_Integer									The Integer to convert
 *
 *	@return														The converted Integer
 */
Text GetInteger(Integer _Integer) {
	return dump(_Integer);
}

// ---------------------------------- //
/** Convert a Int3 into a JSON string
 *
 *	@param	_Int3											The Int3 to convert
 *
 *	@return														The converted Int3
 */
Text GetInt3(Int3 _Int3) {
	return """{ "x": {{{GetInteger(_Int3.X)}}}, "y": {{{GetInteger(_Int3.Y)}}}, "z": {{{GetInteger(_Int3.Z)}}} }""";
}

// ---------------------------------- //
/** Convert a Vec3 into a JSON string
 *
 *	@param	_Vec3											The Vec3 to convert
 *
 *	@return														The converted Vec3
 */
Text GetVec3(Vec3 _Vec3) {
	return """{ "x": {{{GetReal(_Vec3.X)}}}, "y": {{{GetReal(_Vec3.Y)}}}, "z": {{{GetReal(_Vec3.Z)}}} }""";
}

// ---------------------------------- //
/** Convert a Vec2 into a JSON string
 *
 *	@param	_Vec2											The Vec2 to convert
 *
 *	@return														The converted Vec2
 */
Text GetVec2(Vec2 _Vec2) {
	return """{ "x": {{{GetReal(_Vec2.X)}}}, "y": {{{GetReal(_Vec2.Y)}}} }""";
}

// ---------------------------------- //
/** Convert a Boolean into a JSON string
 *
 *	@param	_Boolean									The Boolean to convert
 *
 *	@return														The converted Boolean
 */
Text GetBoolean(Boolean _Boolean) {
	return TL::ToLowerCase(dump(_Boolean));
}

// ---------------------------------- //
/** Convert an Ident into a JSON string
 *
 *	@param	_Ident										The Ident to convert
 *
 *	@return														The converted Ident
 */
Text GetIdent(Ident _Ident) {
	return GetText(""^_Ident);
}