Skip to content

Commit

Permalink
cue: marshal a Value as JSON null directly
Browse files Browse the repository at this point in the history
json.Marshal(nil) always produces ([]byte("null"), nil).
Doing so directly is more straightforward to understand and cheaper.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I77452e40374c642400d873a56dda7d7723f8c537
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1201805
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 7ba104c commit 97f622b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ func (v Value) MarshalJSON() (b []byte, err error) {
func (v Value) marshalJSON() (b []byte, err error) {
v, _ = v.Default()
if v.v == nil {
return internaljson.Marshal(nil)
return []byte("null"), nil
}
ctx := newContext(v.idx)
x := v.eval(ctx)
Expand All @@ -926,7 +926,7 @@ func (v Value) marshalJSON() (b []byte, err error) {
// TODO: implement marshalles in value.
switch k := x.Kind(); k {
case adt.NullKind:
return internaljson.Marshal(nil)
return []byte("null"), nil
case adt.BoolKind:
return internaljson.Marshal(x.(*adt.Bool).B)
case adt.IntKind, adt.FloatKind, adt.NumberKind:
Expand Down

0 comments on commit 97f622b

Please sign in to comment.