github.com/mitranim/gg@v0.1.17/json_test.go (about) 1 package gg_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/mitranim/gg" 8 "github.com/mitranim/gg/gtest" 9 ) 10 11 func Benchmark_json_Marshal(b *testing.B) { 12 var val SomeModel 13 14 for ind := 0; ind < b.N; ind++ { 15 gg.Nop1(gg.Try1(json.Marshal(val))) 16 } 17 } 18 19 func BenchmarkJsonBytes(b *testing.B) { 20 var val SomeModel 21 22 for ind := 0; ind < b.N; ind++ { 23 gg.Nop1(gg.JsonBytes(val)) 24 } 25 } 26 27 func Benchmark_json_Marshal_string(b *testing.B) { 28 var val SomeModel 29 30 for ind := 0; ind < b.N; ind++ { 31 gg.Nop1(string(gg.Try1(json.Marshal(val)))) 32 } 33 } 34 35 func BenchmarkJsonString(b *testing.B) { 36 var val SomeModel 37 38 for ind := 0; ind < b.N; ind++ { 39 gg.Nop1(gg.JsonString(val)) 40 } 41 } 42 43 func Benchmark_json_Unmarshal(b *testing.B) { 44 var val int 45 46 for ind := 0; ind < b.N; ind++ { 47 gg.Try(json.Unmarshal(gg.ToBytes(`123`), &val)) 48 } 49 } 50 51 func BenchmarkJsonParseTo(b *testing.B) { 52 for ind := 0; ind < b.N; ind++ { 53 gg.Nop1(gg.JsonParseTo[int](`123`)) 54 } 55 } 56 57 func BenchmarkJsonParse(b *testing.B) { 58 var val int 59 60 for ind := 0; ind < b.N; ind++ { 61 gg.JsonParse(`123`, &val) 62 } 63 } 64 65 func TestJsonParseTo(t *testing.T) { 66 gtest.Catch(t) 67 68 gtest.Eq( 69 gg.JsonParseTo[SomeModel](`{"id":10}`), 70 SomeModel{Id: 10}, 71 ) 72 73 gtest.Eq( 74 gg.JsonParseTo[SomeModel]([]byte(`{"id":10}`)), 75 SomeModel{Id: 10}, 76 ) 77 }