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