marshal.odin 15 KB

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