marshal.odin 7.5 KB

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