marshal.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. package json
  2. import "core:mem"
  3. import "core:math/bits"
  4. import "base:runtime"
  5. import "core:strconv"
  6. import "core:strings"
  7. import "core:reflect"
  8. import "core:io"
  9. import "core:slice"
  10. Marshal_Data_Error :: enum {
  11. None,
  12. Unsupported_Type,
  13. }
  14. Marshal_Error :: union #shared_nil {
  15. Marshal_Data_Error,
  16. io.Error,
  17. }
  18. // careful with MJSON maps & non quotes usage as keys with whitespace will lead to bad results
  19. Marshal_Options :: struct {
  20. // output based on spec
  21. spec: Specification,
  22. // Use line breaks & tabs/spaces
  23. pretty: bool,
  24. // Use spaces for indentation instead of tabs
  25. use_spaces: bool,
  26. // Given use_spaces true, use this many spaces per indent level. 0 means 4 spaces.
  27. spaces: int,
  28. // Output uint as hex in JSON5 & MJSON
  29. write_uint_as_hex: bool,
  30. // If spec is MJSON and this is true, then keys will be quoted.
  31. //
  32. // WARNING: If your keys contain whitespace and this is false, then the
  33. // output will be bad.
  34. mjson_keys_use_quotes: bool,
  35. // If spec is MJSON and this is true, then use '=' as delimiter between
  36. // keys and values, otherwise ':' is used.
  37. mjson_keys_use_equal_sign: bool,
  38. // When outputting a map, sort the output by key.
  39. //
  40. // NOTE: This will temp allocate and sort a list for each map.
  41. sort_maps_by_key: bool,
  42. // Internal state
  43. indentation: int,
  44. mjson_skipped_first_braces_start: bool,
  45. mjson_skipped_first_braces_end: bool,
  46. }
  47. marshal :: proc(v: any, opt: Marshal_Options = {}, allocator := context.allocator) -> (data: []byte, err: Marshal_Error) {
  48. b := strings.builder_make(allocator)
  49. defer if err != nil {
  50. strings.builder_destroy(&b)
  51. }
  52. // temp guard in case we are sorting map keys, which will use temp allocations
  53. runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD(ignore = allocator == context.temp_allocator)
  54. opt := opt
  55. marshal_to_builder(&b, v, &opt) or_return
  56. if len(b.buf) != 0 {
  57. data = b.buf[:]
  58. }
  59. return data, nil
  60. }
  61. marshal_to_builder :: proc(b: ^strings.Builder, v: any, opt: ^Marshal_Options) -> Marshal_Error {
  62. return marshal_to_writer(strings.to_writer(b), v, opt)
  63. }
  64. marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: Marshal_Error) {
  65. if v == nil {
  66. io.write_string(w, "null") or_return
  67. return
  68. }
  69. ti := runtime.type_info_base(type_info_of(v.id))
  70. a := any{v.data, ti.id}
  71. switch info in ti.variant {
  72. case runtime.Type_Info_Named:
  73. unreachable()
  74. case runtime.Type_Info_Integer:
  75. buf: [40]byte
  76. u: u128
  77. switch i in a {
  78. case i8: u = u128(i)
  79. case i16: u = u128(i)
  80. case i32: u = u128(i)
  81. case i64: u = u128(i)
  82. case i128: u = u128(i)
  83. case int: u = u128(i)
  84. case u8: u = u128(i)
  85. case u16: u = u128(i)
  86. case u32: u = u128(i)
  87. case u64: u = u128(i)
  88. case u128: u = u128(i)
  89. case uint: u = u128(i)
  90. case uintptr: u = u128(i)
  91. case i16le: u = u128(i)
  92. case i32le: u = u128(i)
  93. case i64le: u = u128(i)
  94. case u16le: u = u128(i)
  95. case u32le: u = u128(i)
  96. case u64le: u = u128(i)
  97. case u128le: u = u128(i)
  98. case i16be: u = u128(i)
  99. case i32be: u = u128(i)
  100. case i64be: u = u128(i)
  101. case u16be: u = u128(i)
  102. case u32be: u = u128(i)
  103. case u64be: u = u128(i)
  104. case u128be: u = u128(i)
  105. }
  106. s: string
  107. // allow uints to be printed as hex
  108. if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) {
  109. switch i in a {
  110. case u8, u16, u32, u64, u128:
  111. s = strconv.append_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })
  112. case:
  113. s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
  114. }
  115. } else {
  116. s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
  117. }
  118. io.write_string(w, s) or_return
  119. case runtime.Type_Info_Rune:
  120. r := a.(rune)
  121. io.write_byte(w, '"') or_return
  122. io.write_escaped_rune(w, r, '"', true) or_return
  123. io.write_byte(w, '"') or_return
  124. case runtime.Type_Info_Float:
  125. switch f in a {
  126. case f16: io.write_f16(w, f) or_return
  127. case f32: io.write_f32(w, f) or_return
  128. case f64: io.write_f64(w, f) or_return
  129. case: return .Unsupported_Type
  130. }
  131. case runtime.Type_Info_Complex:
  132. r, i: f64
  133. switch z in a {
  134. case complex32: r, i = f64(real(z)), f64(imag(z))
  135. case complex64: r, i = f64(real(z)), f64(imag(z))
  136. case complex128: r, i = f64(real(z)), f64(imag(z))
  137. case: return .Unsupported_Type
  138. }
  139. io.write_byte(w, '[') or_return
  140. io.write_f64(w, r) or_return
  141. io.write_string(w, ", ") or_return
  142. io.write_f64(w, i) or_return
  143. io.write_byte(w, ']') or_return
  144. case runtime.Type_Info_Quaternion:
  145. return .Unsupported_Type
  146. case runtime.Type_Info_String:
  147. switch s in a {
  148. case string: io.write_quoted_string(w, s, '"', nil, true) or_return
  149. case cstring: io.write_quoted_string(w, string(s), '"', nil, true) or_return
  150. }
  151. case runtime.Type_Info_Boolean:
  152. val: bool
  153. switch b in a {
  154. case bool: val = bool(b)
  155. case b8: val = bool(b)
  156. case b16: val = bool(b)
  157. case b32: val = bool(b)
  158. case b64: val = bool(b)
  159. }
  160. io.write_string(w, val ? "true" : "false") or_return
  161. case runtime.Type_Info_Any:
  162. return .Unsupported_Type
  163. case runtime.Type_Info_Type_Id:
  164. return .Unsupported_Type
  165. case runtime.Type_Info_Pointer:
  166. return .Unsupported_Type
  167. case runtime.Type_Info_Multi_Pointer:
  168. return .Unsupported_Type
  169. case runtime.Type_Info_Soa_Pointer:
  170. return .Unsupported_Type
  171. case runtime.Type_Info_Procedure:
  172. return .Unsupported_Type
  173. case runtime.Type_Info_Parameters:
  174. return .Unsupported_Type
  175. case runtime.Type_Info_Simd_Vector:
  176. return .Unsupported_Type
  177. case runtime.Type_Info_Relative_Pointer:
  178. return .Unsupported_Type
  179. case runtime.Type_Info_Relative_Multi_Pointer:
  180. return .Unsupported_Type
  181. case runtime.Type_Info_Matrix:
  182. return .Unsupported_Type
  183. case runtime.Type_Info_Array:
  184. opt_write_start(w, opt, '[') or_return
  185. for i in 0..<info.count {
  186. opt_write_iteration(w, opt, i) or_return
  187. data := uintptr(v.data) + uintptr(i*info.elem_size)
  188. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  189. }
  190. opt_write_end(w, opt, ']') or_return
  191. case runtime.Type_Info_Enumerated_Array:
  192. index := runtime.type_info_base(info.index).variant.(runtime.Type_Info_Enum)
  193. opt_write_start(w, opt, '[') or_return
  194. for i in 0..<info.count {
  195. opt_write_iteration(w, opt, i) or_return
  196. data := uintptr(v.data) + uintptr(i*info.elem_size)
  197. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  198. }
  199. opt_write_end(w, opt, ']') or_return
  200. case runtime.Type_Info_Dynamic_Array:
  201. opt_write_start(w, opt, '[') or_return
  202. array := cast(^mem.Raw_Dynamic_Array)v.data
  203. for i in 0..<array.len {
  204. opt_write_iteration(w, opt, i) or_return
  205. data := uintptr(array.data) + uintptr(i*info.elem_size)
  206. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  207. }
  208. opt_write_end(w, opt, ']') or_return
  209. case runtime.Type_Info_Slice:
  210. opt_write_start(w, opt, '[') or_return
  211. slice := cast(^mem.Raw_Slice)v.data
  212. for i in 0..<slice.len {
  213. opt_write_iteration(w, opt, i) or_return
  214. data := uintptr(slice.data) + uintptr(i*info.elem_size)
  215. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  216. }
  217. opt_write_end(w, opt, ']') or_return
  218. case runtime.Type_Info_Map:
  219. m := (^mem.Raw_Map)(v.data)
  220. opt_write_start(w, opt, '{') or_return
  221. if m != nil {
  222. if info.map_info == nil {
  223. return .Unsupported_Type
  224. }
  225. map_cap := uintptr(runtime.map_cap(m^))
  226. ks, vs, hs, _, _ := runtime.map_kvh_data_dynamic(m^, info.map_info)
  227. if !opt.sort_maps_by_key {
  228. i := 0
  229. for bucket_index in 0..<map_cap {
  230. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  231. opt_write_iteration(w, opt, i) or_return
  232. i += 1
  233. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  234. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
  235. // check for string type
  236. {
  237. v := any{key, info.key.id}
  238. ti := runtime.type_info_base(type_info_of(v.id))
  239. a := any{v.data, ti.id}
  240. name: string
  241. #partial switch info in ti.variant {
  242. case runtime.Type_Info_String:
  243. switch s in a {
  244. case string: name = s
  245. case cstring: name = string(s)
  246. }
  247. opt_write_key(w, opt, name) or_return
  248. case: return .Unsupported_Type
  249. }
  250. }
  251. marshal_to_writer(w, any{value, info.value.id}, opt) or_return
  252. }
  253. } else {
  254. Entry :: struct {
  255. key: string,
  256. value: any,
  257. }
  258. // If we are sorting the map by key, then we temp alloc an array
  259. // and sort it, then output the result.
  260. sorted := make([dynamic]Entry, 0, map_cap, context.temp_allocator)
  261. for bucket_index in 0..<map_cap {
  262. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  263. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  264. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
  265. name: string
  266. // check for string type
  267. {
  268. v := any{key, info.key.id}
  269. ti := runtime.type_info_base(type_info_of(v.id))
  270. a := any{v.data, ti.id}
  271. #partial switch info in ti.variant {
  272. case runtime.Type_Info_String:
  273. switch s in a {
  274. case string: name = s
  275. case cstring: name = string(s)
  276. }
  277. case: return .Unsupported_Type
  278. }
  279. }
  280. append(&sorted, Entry { key = name, value = any{value, info.value.id}})
  281. }
  282. slice.sort_by(sorted[:], proc(i, j: Entry) -> bool { return i.key < j.key })
  283. for s, i in sorted {
  284. opt_write_iteration(w, opt, i) or_return
  285. opt_write_key(w, opt, s.key) or_return
  286. marshal_to_writer(w, s.value, opt) or_return
  287. }
  288. }
  289. }
  290. opt_write_end(w, opt, '}') or_return
  291. case runtime.Type_Info_Struct:
  292. opt_write_start(w, opt, '{') or_return
  293. for name, i in info.names {
  294. opt_write_iteration(w, opt, i) or_return
  295. if json_name := string(reflect.struct_tag_get(auto_cast info.tags[i], "json")); json_name != "" {
  296. opt_write_key(w, opt, json_name) or_return
  297. } else {
  298. opt_write_key(w, opt, name) or_return
  299. }
  300. id := info.types[i].id
  301. data := rawptr(uintptr(v.data) + info.offsets[i])
  302. marshal_to_writer(w, any{data, id}, opt) or_return
  303. }
  304. opt_write_end(w, opt, '}') or_return
  305. case runtime.Type_Info_Union:
  306. tag_ptr := uintptr(v.data) + info.tag_offset
  307. tag_any := any{rawptr(tag_ptr), info.tag_type.id}
  308. tag: i64 = -1
  309. switch i in tag_any {
  310. case u8: tag = i64(i)
  311. case i8: tag = i64(i)
  312. case u16: tag = i64(i)
  313. case i16: tag = i64(i)
  314. case u32: tag = i64(i)
  315. case i32: tag = i64(i)
  316. case u64: tag = i64(i)
  317. case i64: tag = i64(i)
  318. case: panic("Invalid union tag type")
  319. }
  320. if v.data == nil || tag == 0 {
  321. io.write_string(w, "null") or_return
  322. } else {
  323. id := info.variants[tag-1].id
  324. return marshal_to_writer(w, any{v.data, id}, opt)
  325. }
  326. case runtime.Type_Info_Enum:
  327. return marshal_to_writer(w, any{v.data, info.base.id}, opt)
  328. case runtime.Type_Info_Bit_Set:
  329. is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
  330. if ti == nil {
  331. return false
  332. }
  333. t := runtime.type_info_base(ti)
  334. #partial switch info in t.variant {
  335. case runtime.Type_Info_Integer:
  336. switch info.endianness {
  337. case .Platform: return false
  338. case .Little: return ODIN_ENDIAN != .Little
  339. case .Big: return ODIN_ENDIAN != .Big
  340. }
  341. }
  342. return false
  343. }
  344. bit_data: u64
  345. bit_size := u64(8*ti.size)
  346. do_byte_swap := is_bit_set_different_endian_to_platform(info.underlying)
  347. switch bit_size {
  348. case 0: bit_data = 0
  349. case 8:
  350. x := (^u8)(v.data)^
  351. bit_data = u64(x)
  352. case 16:
  353. x := (^u16)(v.data)^
  354. if do_byte_swap {
  355. x = bits.byte_swap(x)
  356. }
  357. bit_data = u64(x)
  358. case 32:
  359. x := (^u32)(v.data)^
  360. if do_byte_swap {
  361. x = bits.byte_swap(x)
  362. }
  363. bit_data = u64(x)
  364. case 64:
  365. x := (^u64)(v.data)^
  366. if do_byte_swap {
  367. x = bits.byte_swap(x)
  368. }
  369. bit_data = u64(x)
  370. case: panic("unknown bit_size size")
  371. }
  372. io.write_u64(w, bit_data) or_return
  373. return .Unsupported_Type
  374. }
  375. return
  376. }
  377. // write key as quoted string or with optional quotes in mjson
  378. opt_write_key :: proc(w: io.Writer, opt: ^Marshal_Options, name: string) -> (err: io.Error) {
  379. switch opt.spec {
  380. case .JSON, .JSON5:
  381. io.write_quoted_string(w, name) or_return
  382. io.write_string(w, ": " if opt.pretty else ":") or_return
  383. case .MJSON:
  384. if opt.mjson_keys_use_quotes {
  385. io.write_quoted_string(w, name) or_return
  386. } else {
  387. io.write_string(w, name) or_return
  388. }
  389. if opt.mjson_keys_use_equal_sign {
  390. io.write_string(w, " = " if opt.pretty else "=") or_return
  391. } else {
  392. io.write_string(w, ": " if opt.pretty else ":") or_return
  393. }
  394. }
  395. return
  396. }
  397. // insert start byte and increase indentation on pretty
  398. opt_write_start :: proc(w: io.Writer, opt: ^Marshal_Options, c: byte) -> (err: io.Error) {
  399. // Skip MJSON starting braces. We make sure to only do this for c == '{',
  400. // skipping a starting '[' is not allowed.
  401. if opt.spec == .MJSON && !opt.mjson_skipped_first_braces_start && opt.indentation == 0 && c == '{' {
  402. opt.mjson_skipped_first_braces_start = true
  403. return
  404. }
  405. io.write_byte(w, c) or_return
  406. opt.indentation += 1
  407. if opt.pretty {
  408. io.write_byte(w, '\n') or_return
  409. }
  410. return
  411. }
  412. // insert comma separation and write indentations
  413. opt_write_iteration :: proc(w: io.Writer, opt: ^Marshal_Options, iteration: int) -> (err: io.Error) {
  414. switch opt.spec {
  415. case .JSON, .JSON5:
  416. if iteration > 0 {
  417. io.write_byte(w, ',') or_return
  418. if opt.pretty {
  419. io.write_byte(w, '\n') or_return
  420. }
  421. }
  422. opt_write_indentation(w, opt) or_return
  423. case .MJSON:
  424. if iteration > 0 {
  425. // on pretty no commas necessary
  426. if opt.pretty {
  427. io.write_byte(w, '\n') or_return
  428. } else {
  429. // comma separation necessary for non pretty output!
  430. io.write_byte(w, ',') or_return
  431. }
  432. }
  433. opt_write_indentation(w, opt) or_return
  434. }
  435. return
  436. }
  437. // decrease indent, write spacing and insert end byte
  438. opt_write_end :: proc(w: io.Writer, opt: ^Marshal_Options, c: byte) -> (err: io.Error) {
  439. if opt.spec == .MJSON && opt.mjson_skipped_first_braces_start && !opt.mjson_skipped_first_braces_end && opt.indentation == 0 && c == '}' {
  440. opt.mjson_skipped_first_braces_end = true
  441. return
  442. }
  443. opt.indentation -= 1
  444. if opt.pretty {
  445. io.write_byte(w, '\n') or_return
  446. opt_write_indentation(w, opt) or_return
  447. }
  448. io.write_byte(w, c) or_return
  449. return
  450. }
  451. // writes current indentation level based on options
  452. opt_write_indentation :: proc(w: io.Writer, opt: ^Marshal_Options) -> (err: io.Error) {
  453. if !opt.pretty {
  454. return
  455. }
  456. if opt.use_spaces {
  457. spaces := opt.spaces == 0 ? 4 : opt.spaces
  458. for _ in 0..<opt.indentation * spaces {
  459. io.write_byte(w, ' ') or_return
  460. }
  461. } else {
  462. for _ in 0..<opt.indentation {
  463. io.write_byte(w, '\t') or_return
  464. }
  465. }
  466. return
  467. }