mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Ignore struct fields tagged with "-"
This commit is contained in:
+9
-2
@@ -146,9 +146,16 @@ func (g *Generator) genStructFieldEncoder(t reflect.Type, f reflect.StructField)
|
||||
omitEmpty := g.omitEmpty
|
||||
|
||||
for i, s := range strings.Split(f.Tag.Get("json"), ",") {
|
||||
if i > 0 && s == "omitempty" {
|
||||
if i == 0 {
|
||||
if s == "-" {
|
||||
return nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if s == "omitempty" {
|
||||
omitEmpty = true
|
||||
} else if i > 0 && s == "!omitempty" {
|
||||
} else if s == "!omitempty" {
|
||||
omitEmpty = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"encoding/json"
|
||||
|
||||
"github.com/mailru/easyjson"
|
||||
)
|
||||
|
||||
@@ -27,6 +28,7 @@ var testCases = []struct {
|
||||
{&rawValue, rawString},
|
||||
{&stdMarshalerValue, stdMarshalerString},
|
||||
{&unexportedStructValue, unexportedStructString},
|
||||
{&excludedFieldValue, excludedFieldString},
|
||||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
|
||||
@@ -362,3 +362,14 @@ type unexportedStruct struct {
|
||||
|
||||
var unexportedStructValue = unexportedStruct{"test"}
|
||||
var unexportedStructString = `{"Value":"test"}`
|
||||
|
||||
type ExcludedField struct {
|
||||
Process bool `json:"process"`
|
||||
DoNotProcess bool `json:"-"`
|
||||
}
|
||||
|
||||
var excludedFieldValue = ExcludedField{
|
||||
Process: true,
|
||||
DoNotProcess: false,
|
||||
}
|
||||
var excludedFieldString = `{"process":true}`
|
||||
|
||||
Reference in New Issue
Block a user