marshal.odin 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. package encoding_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, loc := #caller_location) -> (data: []byte, err: Marshal_Error) {
  52. b := strings.builder_make(allocator, loc)
  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 := cast_any_int_to_u128(a)
  81. s: string
  82. // allow uints to be printed as hex
  83. if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) {
  84. switch i in a {
  85. case u8, u16, u32, u64, u128:
  86. s = strconv.write_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })
  87. case:
  88. s = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
  89. }
  90. } else {
  91. s = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
  92. }
  93. io.write_string(w, s) or_return
  94. case runtime.Type_Info_Rune:
  95. r := a.(rune)
  96. io.write_byte(w, '"') or_return
  97. io.write_escaped_rune(w, r, '"', true) or_return
  98. io.write_byte(w, '"') or_return
  99. case runtime.Type_Info_Float:
  100. switch f in a {
  101. case f16: io.write_f16(w, f) or_return
  102. case f32: io.write_f32(w, f) or_return
  103. case f64: io.write_f64(w, f) or_return
  104. case: return .Unsupported_Type
  105. }
  106. case runtime.Type_Info_Complex:
  107. r, i: f64
  108. switch z in a {
  109. case complex32: r, i = f64(real(z)), f64(imag(z))
  110. case complex64: r, i = f64(real(z)), f64(imag(z))
  111. case complex128: r, i = f64(real(z)), f64(imag(z))
  112. case: return .Unsupported_Type
  113. }
  114. io.write_byte(w, '[') or_return
  115. io.write_f64(w, r) or_return
  116. io.write_string(w, ", ") or_return
  117. io.write_f64(w, i) or_return
  118. io.write_byte(w, ']') or_return
  119. case runtime.Type_Info_Quaternion:
  120. return .Unsupported_Type
  121. case runtime.Type_Info_String:
  122. switch s in a {
  123. case string: io.write_quoted_string(w, s, '"', nil, true) or_return
  124. case cstring: io.write_quoted_string(w, string(s), '"', nil, true) or_return
  125. }
  126. case runtime.Type_Info_Boolean:
  127. val: bool
  128. switch b in a {
  129. case bool: val = bool(b)
  130. case b8: val = bool(b)
  131. case b16: val = bool(b)
  132. case b32: val = bool(b)
  133. case b64: val = bool(b)
  134. }
  135. io.write_string(w, val ? "true" : "false") or_return
  136. case runtime.Type_Info_Any:
  137. return .Unsupported_Type
  138. case runtime.Type_Info_Type_Id:
  139. return .Unsupported_Type
  140. case runtime.Type_Info_Pointer:
  141. return .Unsupported_Type
  142. case runtime.Type_Info_Multi_Pointer:
  143. return .Unsupported_Type
  144. case runtime.Type_Info_Soa_Pointer:
  145. return .Unsupported_Type
  146. case runtime.Type_Info_Procedure:
  147. return .Unsupported_Type
  148. case runtime.Type_Info_Parameters:
  149. return .Unsupported_Type
  150. case runtime.Type_Info_Simd_Vector:
  151. return .Unsupported_Type
  152. case runtime.Type_Info_Matrix:
  153. return .Unsupported_Type
  154. case runtime.Type_Info_Bit_Field:
  155. return .Unsupported_Type
  156. case runtime.Type_Info_Array:
  157. opt_write_start(w, opt, '[') or_return
  158. for i in 0..<info.count {
  159. opt_write_iteration(w, opt, i == 0) or_return
  160. data := uintptr(v.data) + uintptr(i*info.elem_size)
  161. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  162. }
  163. opt_write_end(w, opt, ']') or_return
  164. case runtime.Type_Info_Enumerated_Array:
  165. index_type := reflect.type_info_base(info.index)
  166. enum_type := index_type.variant.(reflect.Type_Info_Enum)
  167. opt_write_start(w, opt, '{') or_return
  168. for i in 0..<info.count {
  169. value := cast(runtime.Type_Info_Enum_Value)i
  170. index, found := slice.linear_search(enum_type.values, value)
  171. if !found {
  172. continue
  173. }
  174. opt_write_iteration(w, opt, i == 0) or_return
  175. opt_write_key(w, opt, enum_type.names[index]) or_return
  176. data := uintptr(v.data) + uintptr(i*info.elem_size)
  177. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  178. }
  179. opt_write_end(w, opt, '}') or_return
  180. case runtime.Type_Info_Dynamic_Array:
  181. opt_write_start(w, opt, '[') or_return
  182. array := cast(^mem.Raw_Dynamic_Array)v.data
  183. for i in 0..<array.len {
  184. opt_write_iteration(w, opt, i == 0) or_return
  185. data := uintptr(array.data) + uintptr(i*info.elem_size)
  186. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  187. }
  188. opt_write_end(w, opt, ']') or_return
  189. case runtime.Type_Info_Slice:
  190. opt_write_start(w, opt, '[') or_return
  191. slice := cast(^mem.Raw_Slice)v.data
  192. for i in 0..<slice.len {
  193. opt_write_iteration(w, opt, i == 0) or_return
  194. data := uintptr(slice.data) + uintptr(i*info.elem_size)
  195. marshal_to_writer(w, any{rawptr(data), info.elem.id}, opt) or_return
  196. }
  197. opt_write_end(w, opt, ']') or_return
  198. case runtime.Type_Info_Map:
  199. m := (^mem.Raw_Map)(v.data)
  200. opt_write_start(w, opt, '{') or_return
  201. if m != nil {
  202. if info.map_info == nil {
  203. return .Unsupported_Type
  204. }
  205. map_cap := uintptr(runtime.map_cap(m^))
  206. ks, vs, hs, _, _ := runtime.map_kvh_data_dynamic(m^, info.map_info)
  207. if !opt.sort_maps_by_key {
  208. i := 0
  209. for bucket_index in 0..<map_cap {
  210. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  211. opt_write_iteration(w, opt, i == 0) or_return
  212. i += 1
  213. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  214. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
  215. // check for string type
  216. {
  217. kv := any{key, info.key.id}
  218. kti := runtime.type_info_base(type_info_of(kv.id))
  219. ka := any{kv.data, kti.id}
  220. name: string
  221. #partial switch info in kti.variant {
  222. case runtime.Type_Info_String:
  223. switch s in ka {
  224. case string: name = s
  225. case cstring: name = string(s)
  226. }
  227. opt_write_key(w, opt, name) or_return
  228. case runtime.Type_Info_Integer:
  229. buf: [40]byte
  230. u := cast_any_int_to_u128(ka)
  231. name = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil)
  232. opt_write_key(w, opt, name) or_return
  233. case: return .Unsupported_Type
  234. }
  235. }
  236. marshal_to_writer(w, any{value, info.value.id}, opt) or_return
  237. }
  238. } else {
  239. Entry :: struct {
  240. key: string,
  241. value: any,
  242. }
  243. // If we are sorting the map by key, then we temp alloc an array
  244. // and sort it, then output the result.
  245. sorted := make([dynamic]Entry, 0, map_cap, context.temp_allocator)
  246. for bucket_index in 0..<map_cap {
  247. runtime.map_hash_is_valid(hs[bucket_index]) or_continue
  248. key := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
  249. value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
  250. name: string
  251. // check for string type
  252. {
  253. kv := any{key, info.key.id}
  254. kti := runtime.type_info_base(type_info_of(kv.id))
  255. ka := any{kv.data, kti.id}
  256. #partial switch info in kti.variant {
  257. case runtime.Type_Info_String:
  258. switch s in ka {
  259. case string: name = s
  260. case cstring: name = string(s)
  261. }
  262. case: return .Unsupported_Type
  263. }
  264. }
  265. append(&sorted, Entry { key = name, value = any{value, info.value.id}})
  266. }
  267. slice.sort_by(sorted[:], proc(i, j: Entry) -> bool { return i.key < j.key })
  268. for s, i in sorted {
  269. opt_write_iteration(w, opt, i == 0) or_return
  270. opt_write_key(w, opt, s.key) or_return
  271. marshal_to_writer(w, s.value, opt) or_return
  272. }
  273. }
  274. }
  275. opt_write_end(w, opt, '}') or_return
  276. case runtime.Type_Info_Struct:
  277. is_omitempty :: proc(v: any) -> bool {
  278. v := v
  279. if v == nil {
  280. return true
  281. }
  282. ti := runtime.type_info_core(type_info_of(v.id))
  283. #partial switch info in ti.variant {
  284. case runtime.Type_Info_String:
  285. switch x in v {
  286. case string:
  287. return x == ""
  288. case cstring:
  289. return x == nil || x == ""
  290. }
  291. case runtime.Type_Info_Any:
  292. return v.(any) == nil
  293. case runtime.Type_Info_Type_Id:
  294. return v.(typeid) == nil
  295. case runtime.Type_Info_Pointer,
  296. runtime.Type_Info_Multi_Pointer,
  297. runtime.Type_Info_Procedure:
  298. return (^rawptr)(v.data)^ == nil
  299. case runtime.Type_Info_Dynamic_Array:
  300. return (^runtime.Raw_Dynamic_Array)(v.data).len == 0
  301. case runtime.Type_Info_Slice:
  302. return (^runtime.Raw_Slice)(v.data).len == 0
  303. case runtime.Type_Info_Union,
  304. runtime.Type_Info_Bit_Set,
  305. runtime.Type_Info_Soa_Pointer:
  306. return reflect.is_nil(v)
  307. case runtime.Type_Info_Map:
  308. return (^runtime.Raw_Map)(v.data).len == 0
  309. }
  310. return false
  311. }
  312. marshal_struct_fields :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err: Marshal_Error) {
  313. ti := runtime.type_info_base(type_info_of(v.id))
  314. info := ti.variant.(runtime.Type_Info_Struct)
  315. first_iteration := true
  316. for name, i in info.names[:info.field_count] {
  317. omitempty := false
  318. json_name, extra := json_name_from_tag_value(reflect.struct_tag_get(reflect.Struct_Tag(info.tags[i]), "json"))
  319. if json_name == "-" {
  320. continue
  321. }
  322. for flag in strings.split_iterator(&extra, ",") {
  323. switch flag {
  324. case "omitempty":
  325. omitempty = true
  326. }
  327. }
  328. id := info.types[i].id
  329. data := rawptr(uintptr(v.data) + info.offsets[i])
  330. the_value := any{data, id}
  331. if omitempty && is_omitempty(the_value) {
  332. continue
  333. }
  334. opt_write_iteration(w, opt, first_iteration) or_return
  335. first_iteration = false
  336. if json_name != "" {
  337. opt_write_key(w, opt, json_name) or_return
  338. } else {
  339. // Marshal the fields of 'using _: T' fields directly into the parent struct
  340. if info.usings[i] && name == "_" {
  341. marshal_struct_fields(w, the_value, opt) or_return
  342. continue
  343. } else {
  344. opt_write_key(w, opt, name) or_return
  345. }
  346. }
  347. marshal_to_writer(w, the_value, opt) or_return
  348. }
  349. return
  350. }
  351. opt_write_start(w, opt, '{') or_return
  352. marshal_struct_fields(w, v, opt) or_return
  353. opt_write_end(w, opt, '}') or_return
  354. case runtime.Type_Info_Union:
  355. if len(info.variants) == 0 || v.data == nil {
  356. io.write_string(w, "null") or_return
  357. return nil
  358. }
  359. tag_ptr := uintptr(v.data) + info.tag_offset
  360. tag_any := any{rawptr(tag_ptr), info.tag_type.id}
  361. tag: i64 = -1
  362. switch i in tag_any {
  363. case u8: tag = i64(i)
  364. case i8: tag = i64(i)
  365. case u16: tag = i64(i)
  366. case i16: tag = i64(i)
  367. case u32: tag = i64(i)
  368. case i32: tag = i64(i)
  369. case u64: tag = i64(i)
  370. case i64: tag = i64(i)
  371. case: panic("Invalid union tag type")
  372. }
  373. if !info.no_nil {
  374. if tag == 0 {
  375. io.write_string(w, "null") or_return
  376. return nil
  377. }
  378. tag -= 1
  379. }
  380. id := info.variants[tag].id
  381. return marshal_to_writer(w, any{v.data, id}, opt)
  382. case runtime.Type_Info_Enum:
  383. if !opt.use_enum_names || len(info.names) == 0 {
  384. return marshal_to_writer(w, any{v.data, info.base.id}, opt)
  385. } else {
  386. name, found := reflect.enum_name_from_value_any(v)
  387. if found {
  388. return marshal_to_writer(w, name, opt)
  389. } else {
  390. return marshal_to_writer(w, any{v.data, info.base.id}, opt)
  391. }
  392. }
  393. case runtime.Type_Info_Bit_Set:
  394. is_bit_set_different_endian_to_platform :: proc(ti: ^runtime.Type_Info) -> bool {
  395. if ti == nil {
  396. return false
  397. }
  398. t := runtime.type_info_base(ti)
  399. #partial switch info in t.variant {
  400. case runtime.Type_Info_Integer:
  401. switch info.endianness {
  402. case .Platform: return false
  403. case .Little: return ODIN_ENDIAN != .Little
  404. case .Big: return ODIN_ENDIAN != .Big
  405. }
  406. }
  407. return false
  408. }
  409. bit_data: u64
  410. bit_size := u64(8*ti.size)
  411. do_byte_swap := is_bit_set_different_endian_to_platform(info.underlying)
  412. switch bit_size {
  413. case 0: bit_data = 0
  414. case 8:
  415. x := (^u8)(v.data)^
  416. bit_data = u64(x)
  417. case 16:
  418. x := (^u16)(v.data)^
  419. if do_byte_swap {
  420. x = bits.byte_swap(x)
  421. }
  422. bit_data = u64(x)
  423. case 32:
  424. x := (^u32)(v.data)^
  425. if do_byte_swap {
  426. x = bits.byte_swap(x)
  427. }
  428. bit_data = u64(x)
  429. case 64:
  430. x := (^u64)(v.data)^
  431. if do_byte_swap {
  432. x = bits.byte_swap(x)
  433. }
  434. bit_data = u64(x)
  435. case: panic("unknown bit_size size")
  436. }
  437. io.write_u64(w, bit_data) or_return
  438. }
  439. return
  440. }
  441. // write key as quoted string or with optional quotes in mjson
  442. opt_write_key :: proc(w: io.Writer, opt: ^Marshal_Options, name: string) -> (err: io.Error) {
  443. switch opt.spec {
  444. case .JSON, .JSON5:
  445. io.write_quoted_string(w, name) or_return
  446. io.write_string(w, ": " if opt.pretty else ":") or_return
  447. case .MJSON:
  448. if opt.mjson_keys_use_quotes {
  449. io.write_quoted_string(w, name) or_return
  450. } else {
  451. io.write_string(w, name) or_return
  452. }
  453. if opt.mjson_keys_use_equal_sign {
  454. io.write_string(w, " = " if opt.pretty else "=") or_return
  455. } else {
  456. io.write_string(w, ": " if opt.pretty else ":") or_return
  457. }
  458. }
  459. return
  460. }
  461. // insert start byte and increase indentation on pretty
  462. opt_write_start :: proc(w: io.Writer, opt: ^Marshal_Options, c: byte) -> (err: io.Error) {
  463. // Skip MJSON starting braces. We make sure to only do this for c == '{',
  464. // skipping a starting '[' is not allowed.
  465. if opt.spec == .MJSON && !opt.mjson_skipped_first_braces_start && opt.indentation == 0 && c == '{' {
  466. opt.mjson_skipped_first_braces_start = true
  467. return
  468. }
  469. io.write_byte(w, c) or_return
  470. opt.indentation += 1
  471. if opt.pretty {
  472. io.write_byte(w, '\n') or_return
  473. }
  474. return
  475. }
  476. // insert comma separation and write indentations
  477. opt_write_iteration :: proc(w: io.Writer, opt: ^Marshal_Options, first_iteration: bool) -> (err: io.Error) {
  478. switch opt.spec {
  479. case .JSON, .JSON5:
  480. if !first_iteration {
  481. io.write_byte(w, ',') or_return
  482. if opt.pretty {
  483. io.write_byte(w, '\n') or_return
  484. }
  485. }
  486. opt_write_indentation(w, opt) or_return
  487. case .MJSON:
  488. if !first_iteration {
  489. // on pretty no commas necessary
  490. if opt.pretty {
  491. io.write_byte(w, '\n') or_return
  492. } else {
  493. // comma separation necessary for non pretty output!
  494. io.write_byte(w, ',') or_return
  495. }
  496. }
  497. opt_write_indentation(w, opt) or_return
  498. }
  499. return
  500. }
  501. // decrease indent, write spacing and insert end byte
  502. opt_write_end :: proc(w: io.Writer, opt: ^Marshal_Options, c: byte) -> (err: io.Error) {
  503. if opt.spec == .MJSON && opt.mjson_skipped_first_braces_start && !opt.mjson_skipped_first_braces_end && opt.indentation == 0 && c == '}' {
  504. opt.mjson_skipped_first_braces_end = true
  505. return
  506. }
  507. opt.indentation -= 1
  508. if opt.pretty {
  509. io.write_byte(w, '\n') or_return
  510. opt_write_indentation(w, opt) or_return
  511. }
  512. io.write_byte(w, c) or_return
  513. return
  514. }
  515. // writes current indentation level based on options
  516. opt_write_indentation :: proc(w: io.Writer, opt: ^Marshal_Options) -> (err: io.Error) {
  517. if !opt.pretty {
  518. return
  519. }
  520. if opt.use_spaces {
  521. spaces := opt.spaces == 0 ? 4 : opt.spaces
  522. for _ in 0..<opt.indentation * spaces {
  523. io.write_byte(w, ' ') or_return
  524. }
  525. } else {
  526. for _ in 0..<opt.indentation {
  527. io.write_byte(w, '\t') or_return
  528. }
  529. }
  530. return
  531. }
  532. @(private)
  533. cast_any_int_to_u128 :: proc(any_int_value: any) -> u128 {
  534. u: u128 = 0
  535. switch i in any_int_value {
  536. case i8: u = u128(i)
  537. case i16: u = u128(i)
  538. case i32: u = u128(i)
  539. case i64: u = u128(i)
  540. case i128: u = u128(i)
  541. case int: u = u128(i)
  542. case u8: u = u128(i)
  543. case u16: u = u128(i)
  544. case u32: u = u128(i)
  545. case u64: u = u128(i)
  546. case u128: u = u128(i)
  547. case uint: u = u128(i)
  548. case uintptr: u = u128(i)
  549. case i16le: u = u128(i)
  550. case i32le: u = u128(i)
  551. case i64le: u = u128(i)
  552. case u16le: u = u128(i)
  553. case u32le: u = u128(i)
  554. case u64le: u = u128(i)
  555. case u128le: u = u128(i)
  556. case i16be: u = u128(i)
  557. case i32be: u = u128(i)
  558. case i64be: u = u128(i)
  559. case u16be: u = u128(i)
  560. case u32be: u = u128(i)
  561. case u64be: u = u128(i)
  562. case u128be: u = u128(i)
  563. }
  564. return u
  565. }