github.com/6543-forks/go-swagger@v0.26.0/generator/templates/serializers/additionalpropertiesserializer.gotmpl (about) 1 {{ define "additionalPropertiesSerializer" }} 2 // UnmarshalJSON unmarshals this object with additional properties from JSON 3 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(data []byte) error { 4 // stage 1, bind the properties 5 var stage1 {{ template "withoutAdditionalBody" . }} 6 if err := json.Unmarshal(data, &stage1); err != nil { 7 return err 8 } 9 var rcv {{ pascalize .Name }} 10 {{ range .Properties }} 11 rcv.{{ pascalize .Name }} = stage1.{{ pascalize .Name }} 12 {{- end }} 13 *{{ .ReceiverName }} = rcv 14 15 // stage 2, remove properties and add to map 16 stage2 := make(map[string]{{ if .AdditionalProperties }}json.RawMessage{{ else }}interface{}{{ end }}) 17 if err := json.Unmarshal(data, &stage2); err != nil { 18 return err 19 } 20 21 {{ range .Properties }} 22 delete(stage2, {{ printf "%q" .Name }}) 23 {{- end }} 24 25 {{- if .AdditionalProperties }} 26 // stage 3, add additional properties values 27 if len(stage2) > 0 { 28 result := make(map[string]{{ template "schemaType" .AdditionalProperties }}) 29 for k, v := range stage2 { 30 var toadd {{ template "schemaType" .AdditionalProperties }} 31 if err := json.Unmarshal(v, {{if not .AdditionalProperties.IsNullable }}&{{ end }}toadd); err != nil { 32 return err 33 } 34 result[k] = toadd 35 } 36 {{ .ValueExpression }} = result 37 } 38 {{- else }} 39 {{ .ValueExpression }} = stage2 40 {{- end }} 41 42 return nil 43 } 44 45 // MarshalJSON marshals this object with additional properties into a JSON object 46 func ({{.ReceiverName}} {{ pascalize .Name }}) MarshalJSON() ([]byte, error) { 47 var stage1 {{ template "withoutAdditionalBody" . }} 48 {{ range .Properties }} 49 stage1.{{ pascalize .Name }} = {{ .ValueExpression }} 50 {{- end }} 51 52 // make JSON object for known properties 53 props, err := json.Marshal(stage1) 54 if err != nil { 55 return nil, err 56 } 57 58 if len({{ .ValueExpression }}) == 0 { 59 return props, nil 60 } 61 62 // make JSON object for the additional properties 63 additional, err := json.Marshal({{ .ValueExpression }}) 64 if err != nil { 65 return nil, err 66 } 67 68 if len(props) < 3 { 69 return additional, nil 70 } 71 72 // concatenate the 2 objects 73 props[len(props)-1] = ',' 74 return append(props, additional[1:]...), nil 75 } 76 {{- end }} 77 78 {{ define "noAdditionalPropertiesSerializer" }} 79 // UnmarshalJSON unmarshals this object while disallowing additional properties from JSON 80 func ({{.ReceiverName}} *{{ pascalize .Name }}) UnmarshalJSON(data []byte) error { 81 var props {{ template "withoutAdditionalBody" . }} 82 83 dec := json.NewDecoder(bytes.NewReader(data)) 84 dec.DisallowUnknownFields() 85 if err := dec.Decode(&props); err != nil { 86 return err 87 } 88 89 {{- $rcv := .ReceiverName }} 90 {{ range .Properties }} 91 {{ .ReceiverName }}.{{ pascalize .Name }} = props.{{ pascalize .Name }} 92 {{- end }} 93 return nil 94 } 95 {{- end }}