Skip to content

Commit

Permalink
cue: only use internaljson.Marshal where necessary
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mvdan committed Sep 25, 2024
1 parent fed4998 commit da2e501
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (o *structValue) appendJSON(b []byte) ([]byte, error) {
n := o.Len()
for i := range n {
k, v := o.At(i)
// Do not use json.Marshal as it escapes HTML.
s, err := internaljson.Marshal(k)
if err != nil {
return nil, err
Expand Down Expand Up @@ -927,17 +928,20 @@ func (v Value) appendJSON(b []byte) ([]byte, error) {
case adt.NullKind:
return append(b, "null"...), nil
case adt.BoolKind:
b2, err := internaljson.Marshal(x.(*adt.Bool).B)
b2, err := json.Marshal(x.(*adt.Bool).B)
return append(b, b2...), err
case adt.IntKind, adt.FloatKind, adt.NumberKind:
// TODO(mvdan): MarshalText does not guarantee valid JSON,
// but apd.Decimal does not expose a MarshalJSON method either.
b2, err := x.(*adt.Num).X.MarshalText()
b2 = bytes.TrimLeft(b2, "+")
return append(b, b2...), err
case adt.StringKind:
// Do not use json.Marshal as it escapes HTML.
b2, err := internaljson.Marshal(x.(*adt.String).Str)
return append(b, b2...), err
case adt.BytesKind:
b2, err := internaljson.Marshal(x.(*adt.Bytes).B)
b2, err := json.Marshal(x.(*adt.Bytes).B)
return append(b, b2...), err
case adt.ListKind:
i, _ := v.List()
Expand Down

0 comments on commit da2e501

Please sign in to comment.