Browse Source

Reversed order of sort_maps_by_key check in marshal.odin to make PR comparison clearer.

Karl Zylinski 1 year ago
parent
commit
d8f06ed557
1 changed files with 26 additions and 26 deletions
  1. 26 26
      core/encoding/json/marshal.odin

+ 26 - 26
core/encoding/json/marshal.odin

@@ -275,27 +275,23 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
 			map_cap := uintptr(runtime.map_cap(m^))
 			ks, vs, hs, _, _ := runtime.map_kvh_data_dynamic(m^, info.map_info)
 
-			if opt.sort_maps_by_key {
-				Entry :: struct {
-					key: string,
-					value: any,
-				}
-
-				// If we are sorting the map by key, then we temp alloc an array
-				// and sort it, then output the result.
-				sorted := make([dynamic]Entry, 0, map_cap, context.temp_allocator)
+			if !opt.sort_maps_by_key {
+				i := 0
 				for bucket_index in 0..<map_cap {
 					runtime.map_hash_is_valid(hs[bucket_index]) or_continue
 
+					opt_write_iteration(w, opt, i) or_return
+					i += 1
+
 					key   := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
 					value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
-					name: string
 
 					// check for string type
 					{
 						v := any{key, info.key.id}
 						ti := runtime.type_info_base(type_info_of(v.id))
 						a := any{v.data, ti.id}
+						name: string
 
 						#partial switch info in ti.variant {
 						case runtime.Type_Info_String:
@@ -303,38 +299,35 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
 							case string: name = s
 							case cstring: name = string(s)
 							}
+							opt_write_key(w, opt, name) or_return
 
 						case: return .Unsupported_Type
 						}
 					}
 
-					append(&sorted, Entry { key = name, value = any{value, info.value.id}})
-				}
-
-				slice.sort_by(sorted[:], proc(i, j: Entry) -> bool { return i.key < j.key })
-
-				for s, i in sorted {
-					opt_write_iteration(w, opt, i) or_return
-					opt_write_key(w, opt, s.key) or_return
-					marshal_to_writer(w, s.value, opt) or_return
+					marshal_to_writer(w, any{value, info.value.id}, opt) or_return
 				}
 			} else {
-				i := 0
+				Entry :: struct {
+					key: string,
+					value: any,
+				}
+
+				// If we are sorting the map by key, then we temp alloc an array
+				// and sort it, then output the result.
+				sorted := make([dynamic]Entry, 0, map_cap, context.temp_allocator)
 				for bucket_index in 0..<map_cap {
 					runtime.map_hash_is_valid(hs[bucket_index]) or_continue
 
-					opt_write_iteration(w, opt, i) or_return
-					i += 1
-
 					key   := rawptr(runtime.map_cell_index_dynamic(ks, info.map_info.ks, bucket_index))
 					value := rawptr(runtime.map_cell_index_dynamic(vs, info.map_info.vs, bucket_index))
+					name: string
 
 					// check for string type
 					{
 						v := any{key, info.key.id}
 						ti := runtime.type_info_base(type_info_of(v.id))
 						a := any{v.data, ti.id}
-						name: string
 
 						#partial switch info in ti.variant {
 						case runtime.Type_Info_String:
@@ -342,13 +335,20 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
 							case string: name = s
 							case cstring: name = string(s)
 							}
-							opt_write_key(w, opt, name) or_return
 
 						case: return .Unsupported_Type
 						}
 					}
 
-					marshal_to_writer(w, any{value, info.value.id}, opt) or_return
+					append(&sorted, Entry { key = name, value = any{value, info.value.id}})
+				}
+
+				slice.sort_by(sorted[:], proc(i, j: Entry) -> bool { return i.key < j.key })
+
+				for s, i in sorted {
+					opt_write_iteration(w, opt, i) or_return
+					opt_write_key(w, opt, s.key) or_return
+					marshal_to_writer(w, s.value, opt) or_return
 				}
 			}
 		}