github.com/damirazo/docker@v1.9.0/pkg/jsonlog/jsonlog_marshalling.go (about)

     1  // This code was initially generated by ffjson <https://github.com/pquerna/ffjson>
     2  // This code was generated via the following steps:
     3  // $ go get -u github.com/pquerna/ffjson
     4  // $ make BIND_DIR=. shell
     5  // $ ffjson pkg/jsonlog/jsonlog.go
     6  // $ mv pkg/jsonglog/jsonlog_ffjson.go pkg/jsonlog/jsonlog_marshalling.go
     7  //
     8  // It has been modified to improve the performance of time marshalling to JSON
     9  // and to clean it up.
    10  // Should this code need to be regenerated when the JSONLog struct is changed,
    11  // the relevant changes which have been made are:
    12  // import (
    13  //        "bytes"
    14  //-
    15  //        "unicode/utf8"
    16  //+
    17  //+       "github.com/docker/docker/pkg/timeutils"
    18  // )
    19  //
    20  // func (mj *JSONLog) MarshalJSON() ([]byte, error) {
    21  //@@ -20,13 +16,13 @@ func (mj *JSONLog) MarshalJSON() ([]byte, error) {
    22  //        }
    23  //        return buf.Bytes(), nil
    24  // }
    25  //+
    26  // func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
    27  //-       var err error
    28  //-       var obj []byte
    29  //-       var first bool = true
    30  //-       _ = obj
    31  //-       _ = err
    32  //-       _ = first
    33  //+       var (
    34  //+               err       error
    35  //+               timestamp string
    36  //+               first     bool = true
    37  //+       )
    38  //        buf.WriteString(`{`)
    39  //        if len(mj.Log) != 0 {
    40  //                if first == true {
    41  //@@ -52,11 +48,11 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
    42  //                buf.WriteString(`,`)
    43  //        }
    44  //        buf.WriteString(`"time":`)
    45  //-       obj, err = mj.Created.MarshalJSON()
    46  //+       timestamp, err = timeutils.FastMarshalJSON(mj.Created)
    47  //        if err != nil {
    48  //                return err
    49  //        }
    50  //-       buf.Write(obj)
    51  //+       buf.WriteString(timestamp)
    52  //        buf.WriteString(`}`)
    53  //        return nil
    54  // }
    55  // @@ -81,9 +81,10 @@ func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
    56  //         if len(mj.Log) != 0 {
    57  // -                if first == true {
    58  // -                       first = false
    59  // -               } else {
    60  // -                       buf.WriteString(`,`)
    61  // -               }
    62  // +               first = false
    63  //                 buf.WriteString(`"log":`)
    64  //                 ffjsonWriteJSONString(buf, mj.Log)
    65  //         }
    66  
    67  package jsonlog
    68  
    69  import (
    70  	"bytes"
    71  	"unicode/utf8"
    72  
    73  	"github.com/docker/docker/pkg/timeutils"
    74  )
    75  
    76  // MarshalJSON marshals the JSONLog.
    77  func (mj *JSONLog) MarshalJSON() ([]byte, error) {
    78  	var buf bytes.Buffer
    79  	buf.Grow(1024)
    80  	if err := mj.MarshalJSONBuf(&buf); err != nil {
    81  		return nil, err
    82  	}
    83  	return buf.Bytes(), nil
    84  }
    85  
    86  // MarshalJSONBuf marshals the JSONLog and stores the result to a bytes.Buffer.
    87  func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
    88  	var (
    89  		err       error
    90  		timestamp string
    91  		first     = true
    92  	)
    93  	buf.WriteString(`{`)
    94  	if len(mj.Log) != 0 {
    95  		first = false
    96  		buf.WriteString(`"log":`)
    97  		ffjsonWriteJSONString(buf, mj.Log)
    98  	}
    99  	if len(mj.Stream) != 0 {
   100  		if first == true {
   101  			first = false
   102  		} else {
   103  			buf.WriteString(`,`)
   104  		}
   105  		buf.WriteString(`"stream":`)
   106  		ffjsonWriteJSONString(buf, mj.Stream)
   107  	}
   108  	if first == true {
   109  		first = false
   110  	} else {
   111  		buf.WriteString(`,`)
   112  	}
   113  	buf.WriteString(`"time":`)
   114  	timestamp, err = timeutils.FastMarshalJSON(mj.Created)
   115  	if err != nil {
   116  		return err
   117  	}
   118  	buf.WriteString(timestamp)
   119  	buf.WriteString(`}`)
   120  	return nil
   121  }
   122  
   123  func ffjsonWriteJSONString(buf *bytes.Buffer, s string) {
   124  	const hex = "0123456789abcdef"
   125  
   126  	buf.WriteByte('"')
   127  	start := 0
   128  	for i := 0; i < len(s); {
   129  		if b := s[i]; b < utf8.RuneSelf {
   130  			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   131  				i++
   132  				continue
   133  			}
   134  			if start < i {
   135  				buf.WriteString(s[start:i])
   136  			}
   137  			switch b {
   138  			case '\\', '"':
   139  				buf.WriteByte('\\')
   140  				buf.WriteByte(b)
   141  			case '\n':
   142  				buf.WriteByte('\\')
   143  				buf.WriteByte('n')
   144  			case '\r':
   145  				buf.WriteByte('\\')
   146  				buf.WriteByte('r')
   147  			default:
   148  
   149  				buf.WriteString(`\u00`)
   150  				buf.WriteByte(hex[b>>4])
   151  				buf.WriteByte(hex[b&0xF])
   152  			}
   153  			i++
   154  			start = i
   155  			continue
   156  		}
   157  		c, size := utf8.DecodeRuneInString(s[i:])
   158  		if c == utf8.RuneError && size == 1 {
   159  			if start < i {
   160  				buf.WriteString(s[start:i])
   161  			}
   162  			buf.WriteString(`\ufffd`)
   163  			i += size
   164  			start = i
   165  			continue
   166  		}
   167  
   168  		if c == '\u2028' || c == '\u2029' {
   169  			if start < i {
   170  				buf.WriteString(s[start:i])
   171  			}
   172  			buf.WriteString(`\u202`)
   173  			buf.WriteByte(hex[c&0xF])
   174  			i += size
   175  			start = i
   176  			continue
   177  		}
   178  		i += size
   179  	}
   180  	if start < len(s) {
   181  		buf.WriteString(s[start:])
   182  	}
   183  	buf.WriteByte('"')
   184  }