8000 proposal: encoding/json.Encoder: add `ZeroizeNilSlice` option · Issue #61368 · golang/go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
proposal: encoding/json.Encoder: add ZeroizeNilSlice option #61368
Open
@zyxkad

Description

@zyxkad

What does it do

Use (*json.Encoder).SetZeroizeNilSlice(true) can let the encoder encode nil slice as an empty json array [] instead of null

Why

If we have a nil slive []Type(nil), the encoder will encode it as null. Then the consumers will having trouble with it, they must to check if the field is null first.
A way to solve this problem is always alloc a zero length slice before encoding. However, I want my code be simplify but not fill with make([]Type, 0) every where.

Additional Information

  1. This change should be completely backward compatible.
  2. The options name is not good, maybe have to decide a better name later.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0