github.com/titanous/docker@v1.4.1/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 shell BINDDIR=.
     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  
    56  package jsonlog
    57  
    58  import (
    59  	"bytes"
    60  	"unicode/utf8"
    61  
    62  	"github.com/docker/docker/pkg/timeutils"
    63  )
    64  
    65  func (mj *JSONLog) MarshalJSON() ([]byte, error) {
    66  	var buf bytes.Buffer
    67  	buf.Grow(1024)
    68  	err := mj.MarshalJSONBuf(&buf)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	return buf.Bytes(), nil
    73  }
    74  
    75  func (mj *JSONLog) MarshalJSONBuf(buf *bytes.Buffer) error {
    76  	var (
    77  		err       error
    78  		timestamp string
    79  		first     bool = true
    80  	)
    81  	buf.WriteString(`{`)
    82  	if len(mj.Log) != 0 {
    83  		if first == true {
    84  			first = false
    85  		} else {
    86  			buf.WriteString(`,`)
    87  		}
    88  		buf.WriteString(`"log":`)
    89  		ffjson_WriteJsonString(buf, mj.Log)
    90  	}
    91  	if len(mj.Stream) != 0 {
    92  		if first == true {
    93  			first = false
    94  		} else {
    95  			buf.WriteString(`,`)
    96  		}
    97  		buf.WriteString(`"stream":`)
    98  		ffjson_WriteJsonString(buf, mj.Stream)
    99  	}
   100  	if first == true {
   101  		first = false
   102  	} else {
   103  		buf.WriteString(`,`)
   104  	}
   105  	buf.WriteString(`"time":`)
   106  	timestamp, err = timeutils.FastMarshalJSON(mj.Created)
   107  	if err != nil {
   108  		return err
   109  	}
   110  	buf.WriteString(timestamp)
   111  	buf.WriteString(`}`)
   112  	return nil
   113  }
   114  
   115  func ffjson_WriteJsonString(buf *bytes.Buffer, s string) {
   116  	const hex = "0123456789abcdef"
   117  
   118  	buf.WriteByte('"')
   119  	start := 0
   120  	for i := 0; i < len(s); {
   121  		if b := s[i]; b < utf8.RuneSelf {
   122  			if 0x20 <= b && b != '\\' && b != '"' && b != '<' && b != '>' && b != '&' {
   123  				i++
   124  				continue
   125  			}
   126  			if start < i {
   127  				buf.WriteString(s[start:i])
   128  			}
   129  			switch b {
   130  			case '\\', '"':
   131  				buf.WriteByte('\\')
   132  				buf.WriteByte(b)
   133  			case '\n':
   134  				buf.WriteByte('\\')
   135  				buf.WriteByte('n')
   136  			case '\r':
   137  				buf.WriteByte('\\')
   138  				buf.WriteByte('r')
   139  			default:
   140  
   141  				buf.WriteString(`\u00`)
   142  				buf.WriteByte(hex[b>>4])
   143  				buf.WriteByte(hex[b&0xF])
   144  			}
   145  			i++
   146  			start = i
   147  			continue
   148  		}
   149  		c, size := utf8.DecodeRuneInString(s[i:])
   150  		if c == utf8.RuneError && size == 1 {
   151  			if start < i {
   152  				buf.WriteString(s[start:i])
   153  			}
   154  			buf.WriteString(`\ufffd`)
   155  			i += size
   156  			start = i
   157  			continue
   158  		}
   159  
   160  		if c == '\u2028' || c == '\u2029' {
   161  			if start < i {
   162  				buf.WriteString(s[start:i])
   163  			}
   164  			buf.WriteString(`\u202`)
   165  			buf.WriteByte(hex[c&0xF])
   166  			i += size
   167  			start = i
   168  			continue
   169  		}
   170  		i += size
   171  	}
   172  	if start < len(s) {
   173  		buf.WriteString(s[start:])
   174  	}
   175  	buf.WriteByte('"')
   176  }