From ca1bd6304b31e16d121ac41f5b88f0ea906b88bb Mon Sep 17 00:00:00 2001 From: odino Date: Tue, 14 May 2019 16:58:37 +0400 Subject: [PATCH] Fixing hash to JSON conversion `"` were not escaped, thus would lead to invalid json (`{"x": "\"y"}` would become `{"x": ""y"}`) --- evaluator/evaluator_test.go | 4 ++-- object/object.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go index ee63a0b6..34fc832c 100644 --- a/evaluator/evaluator_test.go +++ b/evaluator/evaluator_test.go @@ -1546,7 +1546,7 @@ func TestHashFunctions(t *testing.T) { expected string }{ {` - h = {"a": 1, "b": 2, "c": {"x": 10, "y":20}} + h = {"a": 1, "b": 2, "c": {"x": 10, "y":20}, "e": "\"test"} hk = h.keys() hk = keys(h) hv = h.values() @@ -1557,7 +1557,7 @@ func TestHashFunctions(t *testing.T) { hp = pop(h, "c") hp = h.pop("d") str(h) - `, `{"b": 2}`, + `, `{"b": 2, "e": "\"test"}`, }, } diff --git a/object/object.go b/object/object.go index 2e305069..5dcf8d60 100644 --- a/object/object.go +++ b/object/object.go @@ -184,7 +184,7 @@ type String struct { func (s *String) Type() ObjectType { return STRING_OBJ } func (s *String) Inspect() string { return s.Value } -func (s *String) Json() string { return `"` + s.Inspect() + `"` } +func (s *String) Json() string { return `"` + strings.ReplaceAll(s.Inspect(), `"`, `\"`) + `"` } func (s *String) ZeroValue() string { return "" } func (s *String) HashKey() HashKey { return HashKey{Type: s.Type(), Value: s.Value}