unknown_fields.go raw

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