...

Source file src/github.com/mailru/easyjson/raw.go

Documentation: github.com/mailru/easyjson

     1  package easyjson
     2  
     3  import (
     4  	"github.com/mailru/easyjson/jlexer"
     5  	"github.com/mailru/easyjson/jwriter"
     6  )
     7  
     8  // RawMessage is a raw piece of JSON (number, string, bool, object, array or
     9  // null) that is extracted without parsing and output as is during marshaling.
    10  type RawMessage []byte
    11  
    12  // MarshalEasyJSON does JSON marshaling using easyjson interface.
    13  func (v *RawMessage) MarshalEasyJSON(w *jwriter.Writer) {
    14  	if len(*v) == 0 {
    15  		w.RawString("null")
    16  	} else {
    17  		w.Raw(*v, nil)
    18  	}
    19  }
    20  
    21  // UnmarshalEasyJSON does JSON unmarshaling using easyjson interface.
    22  func (v *RawMessage) UnmarshalEasyJSON(l *jlexer.Lexer) {
    23  	*v = RawMessage(l.Raw())
    24  }
    25  
    26  // UnmarshalJSON implements encoding/json.Unmarshaler interface.
    27  func (v *RawMessage) UnmarshalJSON(data []byte) error {
    28  	*v = data
    29  	return nil
    30  }
    31  
    32  var nullBytes = []byte("null")
    33  
    34  // MarshalJSON implements encoding/json.Marshaler interface.
    35  func (v RawMessage) MarshalJSON() ([]byte, error) {
    36  	if len(v) == 0 {
    37  		return nullBytes, nil
    38  	}
    39  	return v, nil
    40  }
    41  
    42  // IsDefined is required for integration with omitempty easyjson logic.
    43  func (v *RawMessage) IsDefined() bool {
    44  	return len(*v) > 0
    45  }
    46  

View as plain text