Last active
May 9, 2025 20:20
-
-
Save Vftdan/35095cfb5fbac9ca47142f1e2a8fa285 to your computer and use it in GitHub Desktop.
A JSON-like serialization and deserialization for Sprak from the `Else Heart.Break()` game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Was not checked for compilation and runtime errors | |
| string Serialize(var obj) | |
| string t = Type(obj) | |
| if t == 'number' | |
| string s = obj | |
| return s | |
| else if t == 'bool' | |
| if obj | |
| return 'true' | |
| else | |
| return 'false' | |
| end | |
| else if t == 'string' | |
| string acc = '"' | |
| loop char in obj | |
| if char == '"' | |
| acc += '\"' | |
| else | |
| acc += char | |
| end | |
| end | |
| acc += '"' | |
| return acc | |
| else if t == 'array' | |
| array keys = GetIndexes(obj) | |
| bool first = true | |
| string acc = '{' | |
| loop key in keys | |
| if first | |
| first = false | |
| else | |
| acc += ',' | |
| end | |
| acc += Serialize(key) + ':' + Serialize(obj[key]) | |
| end | |
| acc += '}' | |
| return acc | |
| else | |
| return 'UNKNOWN_TYPE:' + t | |
| end | |
| end | |
| var Deserialize(string text) | |
| array state = DeserializePart(text, 0) | |
| var result = state[0] | |
| number nextIndex = state[1] | |
| loop | |
| if nextIndex >= Count(text) | |
| return result | |
| end | |
| string curChar = text[nextIndex] | |
| if curChar != ' ' | |
| return 'UNEXPECTED TRAILING CHAR:' + curChar | |
| end | |
| nextIndex += 1 | |
| end | |
| end | |
| var DeserializePart(string text, number nextIndex) | |
| string firstChar = text[nextIndex] | |
| loop | |
| if firstChar == ' ' | |
| nextIndex += 1 | |
| firstChar = text[nextIndex] | |
| else | |
| break | |
| end | |
| end | |
| if firstChar == '-' || '0' <= firstChar && firstChar <= '9' | |
| # number | |
| string token = firstChar | |
| loop | |
| nextIndex += 1 | |
| if nextIndex >= Count(text) | |
| break | |
| end | |
| string curChar = text[nextIndex] | |
| if curChar != '-' && curChar != '.' && (curChar < '0' || '9' < curChar) | |
| break | |
| end | |
| token += curChar | |
| end | |
| number result = token | |
| return [result, nextIndex] | |
| else if firstChar == 't' | |
| return [true, nextIndex + 4] | |
| else if firstChar == 'f' | |
| return [false, nextIndex + 5] | |
| else if firstChar == '"' | |
| string result = '' | |
| loop | |
| nextIndex += 1 | |
| string curChar = text[nextIndex] | |
| if curChar == '"' | |
| nextIndex += 1 | |
| break | |
| end | |
| if curChar == '\' | |
| nextIndex += 1 | |
| curChar = text[nextIndex] | |
| end | |
| result += curChar | |
| end | |
| return [result, nextIndex] | |
| else if firstChar == '{' | |
| nextIndex += 1 | |
| array result = [] | |
| loop | |
| string curChar = text[nextIndex] | |
| loop | |
| if curChar == ' ' | |
| nextIndex += 1 | |
| curChar = text[nextIndex] | |
| else | |
| break | |
| end | |
| end | |
| if curChar == '}' | |
| nextIndex += 1 | |
| break | |
| end | |
| array state = DeserializePart(text, nextIndex) | |
| var key = state[0] | |
| nextIndex = state[1] | |
| curChar = text[nextIndex] | |
| loop | |
| if curChar == ' ' | |
| nextIndex += 1 | |
| curChar = text[nextIndex] | |
| else | |
| break | |
| end | |
| end | |
| if curChar != ':' | |
| return ['EXPECTED COLON, GOT:' + curChar, nextIndex] | |
| end | |
| nextIndex += 1 | |
| state = DeserializePart(text, nextIndex) | |
| var value = state[0] | |
| nextIndex = state[1] | |
| result[key] = value | |
| curChar = text[nextIndex] | |
| loop | |
| if curChar == ' ' | |
| nextIndex += 1 | |
| curChar = text[nextIndex] | |
| else | |
| break | |
| end | |
| end | |
| if curChar == '}' | |
| nextIndex += 1 | |
| break | |
| end | |
| if curChar != ',' | |
| return ['EXPECTED COMMA, GOT:' + curChar, nextIndex] | |
| end | |
| nextIndex += 1 | |
| end | |
| return result | |
| else | |
| return ['UNEXPECTED STARTING CHARACTER:' + firstChar, nextIndex + 1] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment