Skip to content

Commit

Permalink
Merge pull request #7 from morikuni/fix-json
Browse files Browse the repository at this point in the history
Fix json
  • Loading branch information
morikuni authored Jun 6, 2023
2 parents 1f8d06c + 7c1d3b6 commit 062b2c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
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 []byte(`"` + d.String() + `"`), nil
}

func (d Dict[M]) String() string {
Expand Down
17 changes: 17 additions & 0 deletions mlang_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mlang_test

import (
"encoding/json"
"fmt"
"testing"

Expand Down Expand Up @@ -58,6 +59,22 @@ func TestSetDefaultLanguage(t *testing.T) {
equal(t, hello("Alice").MustGet(language.Japanese), "Hello, Alice!")
}

func TestDict_MarshalJSON(t *testing.T) {
hello := func(name string) mlang.Message {
return mlang.Dict[string]{
language.English: fmt.Sprintf("Hello, %s!", name),
language.French: fmt.Sprintf("Bonjour, %s!", name),
}
}

mlang.SetDefaultLanguage(language.English)
bs, err := json.Marshal(hello("Alice"))
if err != nil {
t.Fatal(err)
}
equal(t, string(bs), `"Hello, Alice!"`)
}

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

0 comments on commit 062b2c5

Please sign in to comment.