serde.sprak
###############
# serde.sprak #
###############
string serialise_string(string input)
string output = ""
loop character in input
if character == '"'
output += "\"
else if character == "\"
output += "\"
end
output += character
end
return '"' + output + '"'
end
string serialise_array(array input)
string output = ""
bool not_start = false
loop key in GetIndexes(input)
if Type(key) != "array"
if not_start
output += ","
else
not_start = true
end
output += serialise(key) + ":" + serialise(input[key])
end
end
return "{" + output + "}"
end
string serialise(var input)
string input_type = Type(input)
if input_type == "unknown"
string output = ""
loop character in "" + input
if character != "i"
output += character
end
end
return output
else if input_type == "string"
return serialise_string(input)
else if input_type == "number"
return "" + input
else if input_type == "bool"
if input
return "t"
end
return "f"
else if input_type == "array"
return serialise_array(input)
end
end
string deserialise_string(string input)
string output = ""
bool escaped = false
loop character in input
if !escaped
if character == '\'
escaped = true
else if character != '"'
output += character
end
else
output += character
escaped = false
end
end
return output
end
bool is_digit(string input)
array digits = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
loop digits
if input == @
return true
end
end
return false
end
bool is_numeric(string character)
if is_digit(character)
return true
else if character == "-"
return true
else if character == "."
return true
else
return false
end
end
# Types:
# * 0: none
# * 1: bool
# * 2: number
# * 3: string
# * 4: array
array deserialise_array(string input)
array accumulator = []
bool in_string = false
bool escaped = false
bool in_value = false
string buffer = ""
number key_type = 0
bool key_bool = false
number key_number = 0
string key_string = ""
number value_type = 0
bool value_bool = false
number value_number = 0
string value_string = ""
array contexts = []
loop character in input
if character == '"'
if !in_string
in_string = true
if in_value
value_type = 3
else
key_type = 3
end
else if !escaped
in_string = false
else if in_value
value_string += '"'
else
key_string += '"'
end
else if character == "t"
if !in_string
if in_value
value_bool = true
value_type = 1
else
key_bool = true
key_type = 1
end
else if in_value
value_string += "t"
else
key_string += "t"
end
else if character == "f"
if !in_string
if in_value
value_bool = false
value_type = 1
else
key_bool = false
key_type = 1
end
else if in_value
value_string += "f"
else
key_string += "f"
end
else if character == ":"
if !in_string
in_value = true
if key_type == 2
key_number = buffer
buffer = ""
end
else if in_value
value_string += ":"
else
key_string += ":"
end
else if character == ","
if !in_string
if key_type == 2
if value_type == 3
accumulator[key_number] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_number] = value_number
else if value_type == 1
accumulator[key_number] = value_bool
end
else if key_type == 3
if value_type == 3
accumulator[key_string] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_string] = value_number
else if value_type == 1
accumulator[key_string] = value_bool
end
else if key_type == 1
if value_type == 3
accumulator[key_bool] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_bool] = value_number
else if value_type == 1
accumulator[key_bool] = value_bool
end
end
in_value = false
buffer = ""
key_string = ""
value_string = ""
else if in_value
value_string += ","
else
key_string += ","
end
else if is_numeric(character)
if !in_string
buffer += character
if in_value
value_type = 2
else
key_type = 2
end
else if in_value
value_string += character
else
key_string += character
end
else if character == "{"
if !in_string
if key_type == 2
Append(contexts, [accumulator, key_type, key_number])
else if key_type == 3
Append(contexts, [accumulator, key_type, key_string])
else if key_type == 0
Append(contexts, [accumulator, key_type])
else
Append(contexts, [accumulator, key_type, key_bool])
end
accumulator = []
in_string = false
escaped = false
in_value = false
buffer = ""
key_type = 0
key_bool = false
key_number = 0
key_string = ""
value_type = 0
value_bool = false
value_number = 0
value_string = ""
else
value_string += "{"
end
else if character == "}"
if !in_string
if key_type == 2
if value_type == 3
accumulator[key_number] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_number] = value_number
else if value_type == 1
accumulator[key_number] = value_bool
end
else if key_type == 3
if value_type == 3
accumulator[key_string] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_string] = value_number
else if value_type == 1
accumulator[key_string] = value_bool
end
else if key_type == 1
if value_type == 3
accumulator[key_bool] = value_string
else if value_type == 2
value_number = buffer
accumulator[key_bool] = value_number
else if value_type == 1
accumulator[key_bool] = value_bool
end
end
array last_context = contexts[Count(contexts) - 1]
array last_accumulator = last_context[0]
number last_key_type = last_context[1]
if last_key_type != 0
last_accumulator[last_context[2]] = accumulator
else
return accumulator
end
accumulator = last_accumulator
value_type = 4
Remove(contexts, Count(contexts) - 1)
else
value_string += "}"
end
else if character == "\"
if !escaped
escaped = true
else
escaped = false
if in_value
value_string += "\"
else
key_string += "\"
end
end
else if in_value
value_string += character
else
key_string += character
end
end
end
var deserialise(string input)
string head = input[0]
if head == "{"
return deserialise_array(input)
else if is_numeric(head)
number output = input
return output
else if head == '"'
return deserialise_string(input)
else if head == "t"
return true
else if head == "f"
return false
end
end