Skip to content

Commit da2e501

Browse files
committed
cue: only use internaljson.Marshal where necessary
That is, when marshaling strings, which can include HTML characters which json.Marshal escapes. Booleans cannot include them, and bytes are hex-encoded anyway, so no escaping takes place. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I4a381b5151e100c28d98e54d4e064c481e00b68e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201809 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent fed4998 commit da2e501

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cue/types.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ func (o *structValue) appendJSON(b []byte) ([]byte, error) {
148148
n := o.Len()
149149
for i := range n {
150150
k, v := o.At(i)
151+
// Do not use json.Marshal as it escapes HTML.
151152
s, err := internaljson.Marshal(k)
152153
if err != nil {
153154
return nil, err
@@ -927,17 +928,20 @@ func (v Value) appendJSON(b []byte) ([]byte, error) {
927928
case adt.NullKind:
928929
return append(b, "null"...), nil
929930
case adt.BoolKind:
930-
b2, err := internaljson.Marshal(x.(*adt.Bool).B)
931+
b2, err := json.Marshal(x.(*adt.Bool).B)
931932
return append(b, b2...), err
932933
case adt.IntKind, adt.FloatKind, adt.NumberKind:
934+
// TODO(mvdan): MarshalText does not guarantee valid JSON,
935+
// but apd.Decimal does not expose a MarshalJSON method either.
933936
b2, err := x.(*adt.Num).X.MarshalText()
934937
b2 = bytes.TrimLeft(b2, "+")
935938
return append(b, b2...), err
936939
case adt.StringKind:
940+
// Do not use json.Marshal as it escapes HTML.
937941
b2, err := internaljson.Marshal(x.(*adt.String).Str)
938942
return append(b, b2...), err
939943
case adt.BytesKind:
940-
b2, err := internaljson.Marshal(x.(*adt.Bytes).B)
944+
b2, err := json.Marshal(x.(*adt.Bytes).B)
941945
return append(b, b2...), err
942946
case adt.ListKind:
943947
i, _ := v.List()

0 commit comments

Comments
 (0)