Skip to content

Commit

Permalink
fix floatString UnmarshalJSON method
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed May 12, 2024
1 parent c1136cd commit 16dc6a3
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions client/asset/btc/electrum/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package electrum

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"
)

type positional []any
Expand Down Expand Up @@ -87,13 +87,12 @@ type floatString float64

func (fs *floatString) UnmarshalJSON(b []byte) error {
// Try to strip the string contents out of quotes.
var str string
if err := json.Unmarshal(b, &str); err != nil {
return err // wasn't a string
if bytes.HasPrefix(b, []byte(`"`)) && bytes.HasSuffix(b, []byte(`"`)) {
b = bytes.Trim(b, `"`)
}
fl, err := strconv.ParseFloat(str, 64)
if err != nil {
return err // The string didn't contain a float.
var fl float64
if err := json.Unmarshal(b, &fl); err != nil {
return err
}
*fs = floatString(fl)
return nil
Expand Down

0 comments on commit 16dc6a3

Please sign in to comment.