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