File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 1
1
package haxmap
2
2
3
3
import (
4
+ "encoding/json"
4
5
"reflect"
5
6
"sort"
6
7
"strconv"
@@ -344,6 +345,28 @@ func (m *Map[K, V]) Fillrate() uintptr {
344
345
return (data .count .Load () * 100 ) / uintptr (len (data .index ))
345
346
}
346
347
348
+ // MarshalJSON implements the json.Marshaler interface.
349
+ func (m * Map [K , V ]) MarshalJSON () ([]byte , error ) {
350
+ gomap := make (map [K ]V )
351
+ for i := m .listHead .next (); i != nil ; i = i .next () {
352
+ gomap [i .key ] = * i .value .Load ()
353
+ }
354
+ return json .Marshal (gomap )
355
+ }
356
+
357
+ // UnmarshalJSON implements the json.Unmarshaler interface.
358
+ func (m * Map [K , V ]) UnmarshalJSON (i []byte ) error {
359
+ gomap := make (map [K ]V )
360
+ err := json .Unmarshal (i , & gomap )
361
+ if err != nil {
362
+ return err
363
+ }
364
+ for k , v := range gomap {
365
+ m .Set (k , v )
366
+ }
367
+ return nil
368
+ }
369
+
347
370
// allocate map with the given size
348
371
func (m * Map [K , V ]) allocate (newSize uintptr ) {
349
372
if m .resizing .CompareAndSwap (notResizing , resizingInProgress ) {
You can’t perform that action at this time.
0 commit comments