github.com/ethersphere/bee/v2@v2.2.0/pkg/bigint/bigint.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package bigint 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "math/big" 11 ) 12 13 type BigInt struct { 14 *big.Int 15 } 16 17 func (i *BigInt) MarshalJSON() ([]byte, error) { 18 if i.Int == nil { 19 return []byte("null"), nil 20 } 21 22 return []byte(fmt.Sprintf(`"%s"`, i.String())), nil 23 } 24 25 func (i *BigInt) UnmarshalJSON(b []byte) error { 26 var val string 27 err := json.Unmarshal(b, &val) 28 if err != nil { 29 return err 30 } 31 32 if i.Int == nil { 33 i.Int = new(big.Int) 34 } 35 36 i.SetString(val, 10) 37 38 return nil 39 } 40 41 // Wrap wraps big.Int pointer into BigInt struct. 42 func Wrap(i *big.Int) *BigInt { 43 return &BigInt{Int: i} 44 }