json.alc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. include "primitives.alh"
  2. include "services.alh"
  3. include "constructors.alh"
  4. include "modelling.alh"
  5. String function json_connect():
  6. String port
  7. port = ""
  8. while (port == ""):
  9. port = comm_connect("JSON")
  10. return port!
  11. Void function json_send_data(port : String, data : Element):
  12. // Send the current value to the service
  13. // First determine what it is!
  14. if (has_value(data)):
  15. // Primitive value, so send as-is
  16. comm_set(port, "P")
  17. comm_set(port, data)
  18. else:
  19. // Is a dict or list
  20. // Problem is that we don't know which it is, as everything is a dictionary in the Modelverse...
  21. // We will do a simple check: if it is a dictionary with keys the contiguous range of integers from 0 to length - 1, it is a list!
  22. Element keys
  23. Element expected
  24. Element copy
  25. Element entry
  26. Element key
  27. keys = dict_keys(data)
  28. expected = list_to_set(range(dict_len(data)))
  29. if (set_equality(keys, expected)):
  30. // Equal, so we are (most likely...) dealing with a list
  31. comm_set(port, "L")
  32. comm_set(port, list_len(data))
  33. copy = list_copy(data)
  34. while (list_len(copy) > 0):
  35. entry = list_pop(copy, 0)
  36. json_send_data(port, entry)
  37. else:
  38. // Not equal, so we are surely dealing with a dict
  39. comm_set(port, "D")
  40. comm_set(port, dict_len(data))
  41. while (set_len(keys) > 0):
  42. key = set_pop(keys)
  43. comm_set(port, key)
  44. json_send_data(port, data[key])
  45. return!
  46. Element function process_JSON_data(port : String):
  47. String type
  48. Element result
  49. type = comm_get(port)
  50. if (type == "P"):
  51. // Primitive data type, so just return as-is
  52. result = comm_get(port)
  53. elif (type == "L"):
  54. // List, so fetch elements in turn
  55. Integer length
  56. length = comm_get(port)
  57. result = list_create()
  58. while (length > 0):
  59. list_append(result, process_JSON_data(port))
  60. length = length - 1
  61. elif (type == "D"):
  62. // Dict, so fetch elements in turn
  63. Integer length
  64. length = comm_get(port)
  65. result = dict_create()
  66. while (length > 0):
  67. dict_add(result, comm_get(port), process_JSON_data(port))
  68. length = length - 1
  69. return result!
  70. String function json_serialize(data : Element):
  71. String port
  72. String response
  73. port = json_connect()
  74. comm_set(port, "encode")
  75. response = comm_get(port)
  76. if (response == "OK"):
  77. // Send data to the service...
  78. json_send_data(port, data)
  79. response = comm_get(port)
  80. comm_close(port)
  81. return response!
  82. else:
  83. log("Error: " + response)
  84. comm_close(port)
  85. return ""!
  86. Element function json_deserialize(str : String):
  87. String port
  88. String response
  89. Element rval
  90. port = json_connect()
  91. comm_set(port, "decode")
  92. response = comm_get(port)
  93. if (response == "OK"):
  94. comm_set(port, str)
  95. rval = process_JSON_data(port)
  96. comm_close(port)
  97. return rval!
  98. else:
  99. log("Error: " + response)
  100. comm_close(port)
  101. return read_root()!