I wrote this function that accepts a Squirrel table or array and creates a JSON string. If something like this already exists, please don’t tell me–I spent most of the day yesterday on it :-). Other comments or observations welcome! I supposed I should use a blob instead of a string, right?
//make a JSON string from an array, table, interger, or string //call without second and third arguments function makeJSON(obj, outputString = "", slotName = "") { //if slotName is passed in, the object is a //table slot and therefore has a name if(slotName != "") outputString += "\"" + slotName + "\": "; switch(typeof(obj)) { case "string": outputString += "\"" + obj + "\""; break; case "integer": outputString += obj; break; case "table": outputString += "{"; local j = 0; foreach(name, slot in obj){ if(j > 0) outputString += ", "; outputString = makeJSON(slot, outputString, name); j++; } outputString += "}"; break; case "array": outputString += "["; foreach(i, element in obj){ if(i > 0) outputString += ", " outputString = makeJSON(element, outputString); } outputString += "]"; break; } return outputString; }