Repository: NethermindEth/juno
Pull request #191 (2022-06-02 18:50:31)

MarshalJSON implementation for Felt
## Changes:
- Add the `MarshalJSON` method to the `Felt` type.

## Types of changes

What types of changes does your code introduce?
_Put an `x` in the boxes that apply_

- [x] New feature (non-breaking change which adds functionality)
**Requires testing**

- [x] Yes

**In case you checked yes, did you write tests??**

- [x] Yes



codecov[bot]: # [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) Report
> Merging [#191](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (10a08e1) into [main](https://codecov.io/gh/NethermindEth/juno/commit/f0d6592887c355fb9328f1c8c2380455b554546e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) (f0d6592) will **not change** coverage.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff            @@
##              main      #191   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           48        48           
  Lines         4292      4291    -1     
=========================================
- Hits          4292      4291    -1     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth) | Coverage Δ | |
|---|---|---|
| [pkg/common/felt.go](https://codecov.io/gh/NethermindEth/juno/pull/191/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL2NvbW1vbi9mZWx0Lmdv) | `100.00% <100.00%> (ø)` | |
| [pkg/crypto/weierstrass/weierstrass.go](https://codecov.io/gh/NethermindEth/juno/pull/191/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth#diff-cGtnL2NyeXB0by93ZWllcnN0cmFzcy93ZWllcnN0cmFzcy5nbw==) | `100.00% <0.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Last update [f0d6592...10a08e1](https://codecov.io/gh/NethermindEth/juno/pull/191?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=NethermindEth).


Patch:
diff --git a/pkg/common/felt.go b/pkg/common/felt.go
index 260ebc69..2451cbd0 100644
--- a/pkg/common/felt.go
+++ b/pkg/common/felt.go
@@ -2,6 +2,7 @@ package common
 
 import (
 	"encoding/hex"
+	"encoding/json"
 	"math/big"
 	"strings"
 )
@@ -51,3 +52,7 @@ func (f *Felt) SetBytes(b []byte) {
 	}
 	copy(f[FeltLength-len(b):], b)
 }
+
+func (f Felt) MarshalJSON() ([]byte, error) {
+	return json.Marshal(f.Hex())
+}


Test patch:
diff --git a/pkg/common/felt_test.go b/pkg/common/felt_test.go
index eb1f9c35..36c206ce 100644
--- a/pkg/common/felt_test.go
+++ b/pkg/common/felt_test.go
@@ -2,6 +2,8 @@ package common
 
 import (
 	"bytes"
+	"encoding/json"
+	"fmt"
 	"math/big"
 	"testing"
 )
@@ -220,3 +222,29 @@ func TestFelt_SetBytes(t *testing.T) {
 		}
 	}
 }
+
+func TestFelt_MarshalJSON(t *testing.T) {
+	type TestCase struct {
+		Input Felt
+		Want  []byte
+	}
+	var tests = [...]TestCase{
+		{
+			[FeltLength]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10},
+			[]byte("\"0xa\""),
+		},
+		{
+			[FeltLength]byte{193, 227, 113, 138, 194, 41, 57, 125, 25, 37, 48, 192, 202, 55, 152, 47, 214, 96, 154, 43, 158, 250, 47, 188, 9, 193, 223, 152, 63, 67, 210, 37},
+			[]byte("\"0xc1e3718ac229397d192530c0ca37982fd6609a2b9efa2fbc09c1df983f43d225\""),
+		},
+	}
+	for _, test := range tests {
+		data, err := json.Marshal(test.Input)
+		if err != nil {
+			t.Error(fmt.Errorf("json.Marshal(%s): %w", test.Input, err))
+		}
+		if bytes.Compare(data, test.Want) != 0 {
+			t.Errorf("json.Marshal(%s) = %s, want: %s", test.Input, data, test.Want)
+		}
+	}
+}