> I was under the impression it wouldn't omit the array if it had zero 
length, hence my question.

Using omitempty omits the array/slice field in the encoding if it is zero 
length. Check out https://play.golang.org/p/B1LcICyEfeK.

Related encoding/json <https://golang.org/pkg/encoding/json/> package doc 
for omitempty:

    The "omitempty" option specifies that the field should be omitted from 
the encoding if the field has an empty value, defined as false, 0, a nil 
pointer, a nil interface value, and any empty array, slice, map, or string.

where "empty array, slice" means a slice that is nil or is of zero length.

On Friday, September 4, 2020 at 4:12:13 AM UTC+5:30 bbse...@gmail.com wrote:

> On Thu, Sep 3, 2020 at 2:32 PM Alexander Mills
> <alexande...@gmail.com> wrote:
> >
> > I was under the impression it wouldn't omit the array if it had zero 
> length, hence my question..
> > i mean at some point we want an empty array in JSON, so I assume it's 
> included by default
>
> If you define boolField as a type, then you can have a custom
> marshaler that checks array sizes, something like this:
>
> func (b boolField) MarshalJSON() ([]byte,error) {
> data:=map[string]interface{}{}
> if len(b.Must)>0 {
> data["must"]=b.Must
> }
> ...
> return json.Marshal(data)
> }
>
> >
> > On Thursday, September 3, 2020 at 3:35:05 AM UTC-7 nishanth...@gmail.com 
> wrote:
> >>
> >> > how do I omit an array on boolField if the array is empty?
> >>
> >> Do you mean how to omit an empty array's field in the serialized JSON? 
> If so, you can use "omitempty" like you did in the other type.
> >>
> >> var boolField struct {
> >> Must []HasTermOrMatch `json:"must,omitempty"`
> >> MustNot []HasTermOrMatch `json:"must_not,omitempty"`
> >> Filter []HasTermOrMatch `json:"filter,omitempty"`
> >> Should []HasTermOrMatch `json:"should,omitempty"`
> >> }
> >>
> >> This should omit the slice field in the serialized JSON if the slice is 
> nil or zero length.
> >> On Thursday, September 3, 2020 at 1:08:00 AM UTC+5:30 Alexander Mills 
> wrote:
> >>>
> >>> I have this code used to construct an ElasticSearch json query:
> >>>
> >>> ```
> >>> type Term = map[string]interface{}
> >>> type QueryString = map[string]interface{}
> >>> type Range = map[string]interface{}
> >>> type Match = map[string]string
> >>>
> >>> type HasTermOrMatch = struct {
> >>> Term `json:"term,omitempty"`
> >>> QueryString `json:"query_string,omitempty"`
> >>> Match `json:"match,omitempty"`
> >>> Range `json:"range,omitempty"`
> >>> }
> >>>
> >>> var boolField struct {
> >>> Must []HasTermOrMatch `json:"must"`
> >>> MustNot []HasTermOrMatch `json:"must_not"`
> >>> Filter []HasTermOrMatch `json:"filter"`
> >>> Should []HasTermOrMatch `json:"should"`
> >>> }
> >>> ```
> >>>
> >>> my question is - how do I omit an array on boolField if the array is 
> empty?
> >>>
> >>> I hear there might be an interface I can add something like this:
> >>>
> >>>
> >>> func (v Match) IsZero() bool {
> >>> return len(v) < 1
> >>> }
> >>>
> >>> func (v Term) IsZero() bool {
> >>> return len(v) < 1
> >>> }
> >>>
> >>> func (v Range) IsZero() bool {
> >>> return len(v) < 1
> >>> }
> >>>
> >>> func (v QueryString) IsZero() bool {
> >>> return len(v) < 1
> >>> }
> >>>
> >>>
> >>> anyone know how to do this?
> >>> thanks
> >>> -alex
> >>>
> >>>
> >>>
> >>>
> > --
> > You received this message because you are subscribed to the Google 
> Groups "golang-nuts" group.
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to golang-nuts...@googlegroups.com.
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/7df1c875-ed2e-46c4-a6f3-2064485de805n%40googlegroups.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/acd36ffe-124d-4415-ad23-beb0f9377c21n%40googlegroups.com.

Reply via email to