marshal.odin 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package json
  2. import "core:mem"
  3. import "core:math/bits"
  4. import "core:runtime"
  5. import "core:strconv"
  6. import "core:strings"
  7. Marshal_Error :: enum {
  8. None,
  9. Unsupported_Type,
  10. Invalid_Data,
  11. }
  12. marshal :: proc(v: any, allocator := context.allocator) -> ([]byte, Marshal_Error) {
  13. b: strings.Builder;
  14. strings.init_builder(&b, allocator);
  15. err := marshal_arg(&b, v);
  16. if err != .None {
  17. strings.destroy_builder(&b);
  18. return nil, err;
  19. }
  20. if len(b.buf) == 0 {
  21. strings.destroy_builder(&b);
  22. return nil, err;
  23. }
  24. return b.buf[:], err;
  25. }
  26. marshal_arg :: proc(b: ^strings.Builder, v: any) -> Marshal_Error {
  27. if v == nil {
  28. strings.write_string(b, "null");
  29. return .None;
  30. }
  31. ti := runtime.type_info_base(type_info_of(v.id));
  32. a := any{v.data, ti.id};
  33. switch info in ti.variant {
  34. case runtime.Type_Info_Named:
  35. unreachable();
  36. case runtime.Type_Info_Integer:
  37. buf: [21]byte;
  38. u: u64;
  39. switch i in a {
  40. case i8: u = u64(i);
  41. case i16: u = u64(i);
  42. case i32: u = u64(i);
  43. case i64: u = u64(i);
  44. case int: u = u64(i);
  45. case u8: u = u64(i);
  46. case u16: u = u64(i);
  47. case u32: u = u64(i);
  48. case u64: u = u64(i);
  49. case uint: u = u64(i);
  50. case uintptr: u = u64(i);
  51. case i16le: u = u64(i);
  52. case i32le: u = u64(i);
  53. case i64le: u = u64(i);
  54. case u16le: u = u64(i);
  55. case u32le: u = u64(i);
  56. case u64le: u = u64(i);
  57. case i16be: u = u64(i);
  58. case i32be: u = u64(i);
  59. case i64be: u = u64(i);
  60. case u16be: u = u64(i);
  61. case u32be: u = u64(i);
  62. case u64be: u = u64(i);
  63. }
  64. s := strconv.append_bits(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil);
  65. strings.write_string(b, s);
  66. case runtime.Type_Info_Rune:
  67. r := a.(rune);
  68. strings.write_byte(b, '"');
  69. strings.write_escaped_rune(b, r, '"', true);
  70. strings.write_byte(b, '"');
  71. case runtime.Type_Info_Float:
  72. val: f64;
  73. switch f in a {
  74. case f16: val = f64(f);
  75. case f32: val = f64(f);
  76. case f64: val = f64(f);
  77. }
  78. buf: [386]byte;
  79. str := strconv.append_float(buf[1:], val, 'f', 2*ti.size, 8*ti.size);
  80. s := buf[:len(str)+1];
  81. if s[1] == '+' || s[1] == '-' {
  82. s = s[1:];
  83. } else {
  84. s[0] = '+';
  85. }
  86. if s[0] == '+' {
  87. s = s[1:];
  88. }
  89. strings.write_string(b, string(s));
  90. case runtime.Type_Info_Complex:
  91. return .Unsupported_Type;
  92. case runtime.Type_Info_Quaternion:
  93. return .Unsupported_Type;
  94. case runtime.Type_Info_String:
  95. switch s in a {
  96. case string: strings.write_quoted_string(b, s);
  97. case cstring: strings.write_quoted_string(b, string(s));
  98. }
  99. case runtime.Type_Info_Boolean:
  100. val: bool;
  101. switch b in a {
  102. case bool: val = bool(b);
  103. case b8: val = bool(b);
  104. case b16: val = bool(b);
  105. case b32: val = bool(b);
  106. case b64: val = bool(b);
  107. }
  108. strings.write_string(b, val ? "true" : "false");
  109. case runtime.Type_Info_Any:
  110. return .Unsupported_Type;
  111. case runtime.Type_Info_Type_Id:
  112. return .Unsupported_Type;
  113. case runtime.Type_Info_Pointer:
  114. return .Unsupported_Type;
  115. case runtime.Type_Info_Multi_Pointer:
  116. return .Unsupported_Type;
  117. case runtime.Type_Info_Procedure:
  118. return .Unsupported_Type;
  119. case runtime.Type_Info_Tuple:
  120. return .Unsupported_Type;
  121. case runtime.Type_Info_Enumerated_Array:
  122. return .Unsupported_Type;
  123. case runtime.Type_Info_Simd_Vector:
  124. return .Unsupported_Type;
  125. case runtime.Type_Info_Relative_Pointer:
  126. return .Unsupported_Type;
  127. case runtime.Type_Info_Relative_Slice:
  128. return .Unsupported_Type;
  129. case runtime.Type_Info_Array:
  130. strings.write_byte(b, '[');
  131. for i in 0..<info.count {
  132. if i > 0 { strings.write_string(b, ", "); }
  133. data := uintptr(v.data) + uintptr(i*info.elem_size);
  134. marshal_arg(b, any{rawptr(data), info.elem.id});
  135. }
  136. strings.write_byte(b, ']');
  137. case runtime.Type_Info_Dynamic_Array:
  138. strings.write_byte(b, '[');
  139. array := cast(^mem.Raw_Dynamic_Array)v.data;
  140. for i in 0..<array.len {
  141. if i > 0 { strings.write_string(b, ", "); }
  142. data := uintptr(array.data) + uintptr(i*info.elem_size);
  143. marshal_arg(b, any{rawptr(data), info.elem.id});
  144. }
  145. strings.write_byte(b, ']');
  146. case runtime.Type_Info_Slice:
  147. strings.write_byte(b, '[');
  148. slice := cast(^mem.Raw_Slice)v.data;
  149. for i in 0..<slice.len {
  150. if i > 0 { strings.write_string(b, ", "); }
  151. data := uintptr(slice.data) + uintptr(i*info.elem_size);
  152. marshal_arg(b, any{rawptr(data), info.elem.id});
  153. }
  154. strings.write_byte(b, ']');
  155. case runtime.Type_Info_Map:
  156. m := (^mem.Raw_Map)(v.data);
  157. strings.write_byte(b, '{');
  158. if m != nil {
  159. if info.generated_struct == nil {
  160. return .Unsupported_Type;
  161. }
  162. entries := &m.entries;
  163. gs := runtime.type_info_base(info.generated_struct).variant.(runtime.Type_Info_Struct);
  164. ed := runtime.type_info_base(gs.types[1]).variant.(runtime.Type_Info_Dynamic_Array);
  165. entry_type := ed.elem.variant.(runtime.Type_Info_Struct);
  166. entry_size := ed.elem_size;
  167. for i in 0..<entries.len {
  168. if i > 0 { strings.write_string(b, ", "); }
  169. data := uintptr(entries.data) + uintptr(i*entry_size);
  170. key := rawptr(data + entry_type.offsets[2]);
  171. value := rawptr(data + entry_type.offsets[3]);
  172. marshal_arg(b, any{key, info.key.id});
  173. strings.write_string(b, ": ");
  174. marshal_arg(b, any{value, info.value.id});
  175. }
  176. }
  177. strings.write_byte(b, '}');
  178. case runtime.Type_Info_Struct:
  179. strings.write_byte(b, '{');
  180. for name, i in info.names {
  181. if i > 0 { strings.write_string(b, ", "); }
  182. strings.write_quoted_string(b, name);
  183. strings.write_string(b, ": ");
  184. id := info.types[i].id;
  185. data := rawptr(uintptr(v.data) + info.offsets[i]);
  186. marshal_arg(b, any{data, id});
  187. }
  188. strings.write_byte(b, '}');
  189. case runtime.Type_Info_Union:
  190. tag_ptr := uintptr(v.data) + info.tag_offset;
  191. tag_any := any{rawptr(tag_ptr), info.tag_type.id};
  192. tag: i64 = -1;
  193. switch i in tag_any {
  194. case u8: tag = i64(i);
  195. case i8: tag = i64(i);
  196. case u16: tag = i64(i);
  197. case i16: tag = i64(i);
  198. case u32: tag = i64(i);
  199. case i32: tag = i64(i);
  200. case u64: tag = i64(i);
  201. case i64: tag = i64(i);
  202. case: panic("Invalid union tag type");
  203. }
  204. if v.data == nil || tag == 0 {
  205. strings.write_string(b, "null");
  206. } else {
  207. id := info.variants[tag-1].id;
  208. marshal_arg(b, any{v.data, id});
  209. }
  210. case runtime.Type_Info_Enum:
  211. return marshal_arg(b, any{v.data, info.base.id});
  212. case runtime.Type_Info_Bit_Set:
  213. is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
  214. if ti == nil {
  215. return false;
  216. }
  217. t := runtime.type_info_base(ti);
  218. #partial switch info in t.variant {
  219. case runtime.Type_Info_Integer:
  220. switch info.endianness {
  221. case .Platform: return false;
  222. case .Little: return ODIN_ENDIAN != "little";
  223. case .Big: return ODIN_ENDIAN != "big";
  224. }
  225. }
  226. return false;
  227. }
  228. bit_data: u64;
  229. bit_size := u64(8*ti.size);
  230. do_byte_swap := is_bit_set_different_endian_to_platform(info.underlying);
  231. switch bit_size {
  232. case 0: bit_data = 0;
  233. case 8:
  234. x := (^u8)(v.data)^;
  235. bit_data = u64(x);
  236. case 16:
  237. x := (^u16)(v.data)^;
  238. if do_byte_swap {
  239. x = bits.byte_swap(x);
  240. }
  241. bit_data = u64(x);
  242. case 32:
  243. x := (^u32)(v.data)^;
  244. if do_byte_swap {
  245. x = bits.byte_swap(x);
  246. }
  247. bit_data = u64(x);
  248. case 64:
  249. x := (^u64)(v.data)^;
  250. if do_byte_swap {
  251. x = bits.byte_swap(x);
  252. }
  253. bit_data = u64(x);
  254. case: panic("unknown bit_size size");
  255. }
  256. strings.write_u64(b, bit_data);
  257. return .Unsupported_Type;
  258. }
  259. return .None;
  260. }