Skip to content

Commit

Permalink
Replace MarshalJSON() with MarshalText() in most cases (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan authored Mar 7, 2024
1 parent 720cdd5 commit 6026484
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 65 deletions.
12 changes: 6 additions & 6 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ const (
BlockRejected
)

func (s BlockStatus) MarshalJSON() ([]byte, error) {
func (s BlockStatus) MarshalText() ([]byte, error) {
switch s {
case BlockPending:
return []byte(`"PENDING"`), nil
return []byte("PENDING"), nil
case BlockAcceptedL2:
return []byte(`"ACCEPTED_ON_L2"`), nil
return []byte("ACCEPTED_ON_L2"), nil
case BlockAcceptedL1:
return []byte(`"ACCEPTED_ON_L1"`), nil
return []byte("ACCEPTED_ON_L1"), nil
case BlockRejected:
return []byte(`"REJECTED"`), nil
return []byte("REJECTED"), nil
default:
return nil, errors.New("unknown block status")
return nil, fmt.Errorf("unknown block status %v", s)
}
}

Expand Down
9 changes: 3 additions & 6 deletions rpc/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func (s Sync) MarshalJSON() ([]byte, error) {
if s.Syncing != nil && !*s.Syncing {
return json.Marshal(false)
}
type Alias Sync
return json.Marshal(&struct {
Alias
}{
Alias: Alias(s),
})

type alias Sync
return json.Marshal(alias(s))
}
56 changes: 26 additions & 30 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (t TransactionType) String() string {
}
}

func (t TransactionType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t.String())), nil
func (t TransactionType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}

func (t *TransactionType) UnmarshalJSON(data []byte) error {
Expand Down Expand Up @@ -71,14 +71,14 @@ const (
FRI
)

func (u FeeUnit) MarshalJSON() ([]byte, error) {
func (u FeeUnit) MarshalText() ([]byte, error) {
switch u {
case WEI:
return []byte(`"WEI"`), nil
return []byte("WEI"), nil
case FRI:
return []byte(`"FRI"`), nil
return []byte("FRI"), nil
default:
return nil, errors.New("unknown FeeUnit")
return nil, fmt.Errorf("unknown FeeUnit %v", u)
}
}

Expand All @@ -91,18 +91,18 @@ const (
TxnStatusRejected
)

func (s TxnStatus) MarshalJSON() ([]byte, error) {
func (s TxnStatus) MarshalText() ([]byte, error) {
switch s {
case TxnStatusReceived:
return []byte(`"RECEIVED"`), nil
return []byte("RECEIVED"), nil
case TxnStatusRejected:
return []byte(`"REJECTED"`), nil
return []byte("REJECTED"), nil
case TxnStatusAcceptedOnL1:
return []byte(`"ACCEPTED_ON_L1"`), nil
return []byte("ACCEPTED_ON_L1"), nil
case TxnStatusAcceptedOnL2:
return []byte(`"ACCEPTED_ON_L2"`), nil
return []byte("ACCEPTED_ON_L2"), nil
default:
return nil, errors.New("unknown ExecutionStatus")
return nil, fmt.Errorf("unknown ExecutionStatus %v", s)
}
}

Expand All @@ -113,14 +113,14 @@ const (
TxnFailure
)

func (es TxnExecutionStatus) MarshalJSON() ([]byte, error) {
func (es TxnExecutionStatus) MarshalText() ([]byte, error) {
switch es {
case TxnSuccess:
return []byte(`"SUCCEEDED"`), nil
return []byte("SUCCEEDED"), nil
case TxnFailure:
return []byte(`"REVERTED"`), nil
return []byte("REVERTED"), nil
default:
return nil, errors.New("unknown ExecutionStatus")
return nil, fmt.Errorf("unknown ExecutionStatus %v", es)
}
}

Expand All @@ -131,14 +131,14 @@ const (
TxnAcceptedOnL2
)

func (fs TxnFinalityStatus) MarshalJSON() ([]byte, error) {
func (fs TxnFinalityStatus) MarshalText() ([]byte, error) {
switch fs {
case TxnAcceptedOnL1:
return []byte(`"ACCEPTED_ON_L1"`), nil
return []byte("ACCEPTED_ON_L1"), nil
case TxnAcceptedOnL2:
return []byte(`"ACCEPTED_ON_L2"`), nil
return []byte("ACCEPTED_ON_L2"), nil
default:
return nil, errors.New("unknown FinalityStatus")
return nil, fmt.Errorf("unknown FinalityStatus %v", fs)
}
}

Expand All @@ -149,14 +149,14 @@ const (
DAModeL2
)

func (m DataAvailabilityMode) MarshalJSON() ([]byte, error) {
func (m DataAvailabilityMode) MarshalText() ([]byte, error) {
switch m {
case DAModeL1:
return []byte(`"L1"`), nil
return []byte("L1"), nil
case DAModeL2:
return []byte(`"L2"`), nil
return []byte("L2"), nil
default:
return nil, errors.New("unknown DataAvailabilityMode")
return nil, fmt.Errorf("unknown DataAvailabilityMode %v", m)
}
}

Expand All @@ -179,21 +179,17 @@ const (
ResourceL2Gas
)

func (r Resource) MarshalJSON() ([]byte, error) {
func (r Resource) MarshalText() ([]byte, error) {
switch r {
case ResourceL1Gas:
return []byte("l1_gas"), nil
case ResourceL2Gas:
return []byte("l2_gas"), nil
default:
return nil, errors.New("unknown Resource")
return nil, fmt.Errorf("unknown Resource %v", r)
}
}

func (r Resource) MarshalText() ([]byte, error) {
return r.MarshalJSON()
}

func (r *Resource) UnmarshalJSON(data []byte) error {
switch string(data) {
case `"l1_gas"`:
Expand Down
5 changes: 2 additions & 3 deletions utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"encoding"
"encoding/json"
"errors"
"time"

Expand Down Expand Up @@ -70,8 +69,8 @@ func (l *LogLevel) Type() string {
return "LogLevel"
}

func (l *LogLevel) MarshalJSON() ([]byte, error) {
return json.RawMessage(`"` + l.String() + `"`), nil
func (l *LogLevel) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}

func (l *LogLevel) UnmarshalText(text []byte) error {
Expand Down
3 changes: 2 additions & 1 deletion utils/log_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils_test

import (
"encoding/json"
"strings"
"testing"

Expand Down Expand Up @@ -75,7 +76,7 @@ func TestLogLevelUnmarshalText(t *testing.T) {
func TestLogLevelMarshalJSON(t *testing.T) {
for level, str := range levelStrings {
t.Run("level "+str, func(t *testing.T) {
lb, err := level.MarshalJSON()
lb, err := json.Marshal(&level)
require.NoError(t, err)

expectedStr := `"` + str + `"`
Expand Down
5 changes: 2 additions & 3 deletions utils/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"encoding"
"encoding/json"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -132,8 +131,8 @@ func (n *Network) MarshalYAML() (any, error) {
return n.String(), nil
}

func (n *Network) MarshalJSON() ([]byte, error) {
return json.RawMessage(`"` + n.String() + `"`), nil
func (n *Network) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}

func (n *Network) Set(s string) error {
Expand Down
3 changes: 2 additions & 1 deletion utils/network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils_test

import (
"encoding/json"
"fmt"
"math/big"
"strings"
Expand Down Expand Up @@ -130,7 +131,7 @@ func TestNetworkUnmarshalText(t *testing.T) {
func TestNetworkMarshalJSON(t *testing.T) {
for network, str := range networkStrings {
t.Run("network "+str, func(t *testing.T) {
nb, err := network.MarshalJSON()
nb, err := json.Marshal(&network)
require.NoError(t, err)

expectedStr := `"` + str + `"`
Expand Down
5 changes: 2 additions & 3 deletions vm/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package vm
import (
"encoding/json"
"errors"
"fmt"
"slices"

"github.com/NethermindEth/juno/core/felt"
Expand Down Expand Up @@ -75,8 +74,8 @@ func (t TransactionType) String() string {
}
}

func (t TransactionType) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("%q", t.String())), nil
func (t TransactionType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}

func (t *TransactionType) UnmarshalJSON(data []byte) error {
Expand Down
16 changes: 4 additions & 12 deletions vm/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ const (
DAModeL2
)

func (m DataAvailabilityMode) MarshalJSON() ([]byte, error) {
func (m DataAvailabilityMode) MarshalText() ([]byte, error) {
switch m {
case DAModeL1:
return []byte(`"L1"`), nil
return []byte("L1"), nil
case DAModeL2:
return []byte(`"L2"`), nil
return []byte("L2"), nil
default:
return nil, errors.New("unknown resource")
}
Expand All @@ -104,16 +104,8 @@ func (r Resource) MarshalText() ([]byte, error) {
case ResourceL2Gas:
return []byte("L2_GAS"), nil
default:
return nil, errors.New("unknown resource")
}
}

func (r Resource) MarshalJSON() ([]byte, error) {
result, err := r.MarshalText()
if err != nil {
return nil, err
return nil, fmt.Errorf("unknown resource %v", r)
}
return []byte(`"` + string(result) + `"`), nil
}

type ResourceBounds struct {
Expand Down

0 comments on commit 6026484

Please sign in to comment.