Advertisement
NovaYoshi

Squirrel ToJSON function

Nov 22nd, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. function ToJSON(Table) {
  2. if(typeof(Table)!="array" && typeof(Table)!="table")
  3. return Table.tostring();
  4. local Out = "";
  5. function AsString(Item) {
  6. switch(typeof(Item)) {
  7. case "table":
  8. case "array":
  9. return ToJSON(Item);
  10. case "string":
  11. local Len = Item.len();
  12. local Str = "";
  13. for(local i=0;i<Len;i++) {
  14. if(Item[i]=='\\' || Item[i]=='\"')
  15. Str+="\\";
  16. Str+=format("%c", Item[i])
  17. }
  18. return "\""+Str+"\"";
  19. default:
  20. return Item.tostring();
  21. }
  22. }
  23. if(typeof(Table) == "table") {
  24. if(Table.len() == 0) return "{}";
  25. Out = "{";
  26. foreach(Key,Val in Table)
  27. Out += "\""+Key+"\":"+AsString(Val)+", ";
  28. return Out.slice(0,-2) + "}";
  29. }
  30. if(typeof(Table) == "array") {
  31. if(Table.len() == 0) return "[]";
  32. Out = "[";
  33. foreach(Val in Table)
  34. Out += AsString(Val)+", ";
  35. return Out.slice(0,-2) + "]";
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement