Skip to content

Commit

Permalink
Merge pull request #9 from morikuni/fix-marshal-json
Browse files Browse the repository at this point in the history
Marshal string with json.Marshal
  • Loading branch information
morikuni authored May 27, 2024
2 parents 8779f46 + b5dd3fe commit ac537d9
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.20

require golang.org/x/text v0.9.0

require github.com/morikuni/failure/v2 v2.0.0-20240407145138-d0020a5003d2
require github.com/morikuni/failure/v2 v2.0.0-20240419002657-2551069d1c86

require golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261 h1:Rqq1Kh/HMFt
github.com/morikuni/failure/v2 v2.0.0-20230512003104-6bb497ead261/go.mod h1:tHod902kOvu2+09OAbzPMrE4B8fIc+M/2kl/UI3mDQI=
github.com/morikuni/failure/v2 v2.0.0-20240407145138-d0020a5003d2 h1:TAkI7cXmMnSuuVs1J7Xm9+vGaw649eEVUTz193HyZS4=
github.com/morikuni/failure/v2 v2.0.0-20240407145138-d0020a5003d2/go.mod h1:tHod902kOvu2+09OAbzPMrE4B8fIc+M/2kl/UI3mDQI=
github.com/morikuni/failure/v2 v2.0.0-20240419002657-2551069d1c86 h1:f3IP/QdKL5cQe8fTFRWV0slL60Ss/goB2UntNcnOGqk=
github.com/morikuni/failure/v2 v2.0.0-20240419002657-2551069d1c86/go.mod h1:tHod902kOvu2+09OAbzPMrE4B8fIc+M/2kl/UI3mDQI=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
Expand Down
2 changes: 1 addition & 1 deletion mlang.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (d Dict[M]) LogValue() slog.Value {

// MarshalJSON implements json.Marshaler.
func (d Dict[M]) MarshalJSON() ([]byte, error) {
return []byte(`"` + d.String() + `"`), nil
return json.Marshal(d.String())
}

func (d Dict[M]) String() string {
Expand Down
4 changes: 2 additions & 2 deletions mlang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestSetDefaultLanguage(t *testing.T) {
func TestDict_MarshalJSON(t *testing.T) {
hello := func(name string) mlang.Message {
return mlang.Dict[string]{
language.English: fmt.Sprintf("Hello, %s!", name),
language.English: fmt.Sprintf(`Hello, "%s"!`, name),
language.French: fmt.Sprintf("Bonjour, %s!", name),
}
}
Expand All @@ -72,7 +72,7 @@ func TestDict_MarshalJSON(t *testing.T) {
if err != nil {
t.Fatal(err)
}
equal(t, string(bs), `"Hello, Alice!"`)
equal(t, string(bs), `"Hello, \"Alice\"!"`)
}

func equal(t *testing.T, got, want any) {
Expand Down

0 comments on commit ac537d9

Please sign in to comment.