// Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package json import ( "testing" ) type Optionals struct { Sr string `json:"sr"` So string `json:"so,omitempty"` Ir int `json:"omitempty"` // actually named omitempty, not an option Io int `json:"io,omitempty"` Slr []string `json:"slr,random"` Slo []string `json:"slo,omitempty"` Mr map[string]interface{} `json:"mr"` Mo map[string]interface{} `json:",omitempty"` } var optionalsExpected = `{ "sr": "", "omitempty": 0, "slr": [], "mr": {} }` func TestOmitEmpty(t *testing.T) { var o Optionals o.Mr = map[string]interface{}{} o.Mo = map[string]interface{}{} got, err := MarshalIndent(&o, "", " ") if err != nil { t.Fatal(err) } if got := string(got); got != optionalsExpected { t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected) } }