unknown_fields.go 847 B

12345678910111213141516171819202122232425262728293031323334
  1. package easyjson
  2. import (
  3. json "encoding/json"
  4. jlexer "github.com/mailru/easyjson/jlexer"
  5. "github.com/mailru/easyjson/jwriter"
  6. )
  7. // UnknownFieldsProxy implemets UnknownsUnmarshaler and UnknownsMarshaler
  8. // use it as embedded field in your structure to parse and then serialize unknown struct fields
  9. type UnknownFieldsProxy struct {
  10. unknownFields map[string]interface{}
  11. }
  12. func (s *UnknownFieldsProxy) UnmarshalUnknown(in *jlexer.Lexer, key string) {
  13. if s.unknownFields == nil {
  14. s.unknownFields = make(map[string]interface{}, 1)
  15. }
  16. s.unknownFields[key] = in.Interface()
  17. }
  18. func (s UnknownFieldsProxy) MarshalUnknowns(out *jwriter.Writer, first bool) {
  19. for key, val := range s.unknownFields {
  20. if first {
  21. first = false
  22. } else {
  23. out.RawByte(',')
  24. }
  25. out.String(string(key))
  26. out.RawByte(':')
  27. out.Raw(json.Marshal(val))
  28. }
  29. }