1 // Package jwriter provides an efficient mechanism for writing JSON data sequentially. 2 // 3 // The high-level API for this package, Writer, is designed to facilitate writing custom JSON 4 // marshaling logic concisely and reliably. Output is buffered in memory. 5 // 6 // import ( 7 // "gopkg.in/launchdarkly/jsonstream.v1/jwriter" 8 // ) 9 // 10 // type myStruct struct { 11 // value int 12 // } 13 // 14 // func (s myStruct) WriteToJSONWriter(w *jwriter.Writer) { 15 // obj := w.Object() // writing a JSON object structure like {"value":2} 16 // obj.Property("value").Int(s.value) 17 // obj.End() 18 // } 19 // 20 // func PrintMyStructJSON(s myStruct) { 21 // w := jwriter.NewWriter() 22 // s.WriteToJSONWriter(&w) 23 // fmt.Println(string(w.Bytes()) 24 // } 25 // 26 // Output can optionally be dumped to an io.Writer at intervals to avoid allocating a large buffer: 27 // 28 // func WriteToHTTPResponse(s myStruct, resp http.ResponseWriter) { 29 // resp.Header.Add("Content-Type", "application/json") 30 // w := jwriter.NewStreamingWriter(resp, 1000) 31 // myStruct.WriteToJSONWriter(&w) 32 // } 33 // 34 // The underlying low-level token writing mechanism has two available implementations. The default 35 // implementation has no external dependencies. For interoperability with the easyjson library 36 // (https://github.com/mailru/easyjson), there is also an implementation that delegates to the 37 // easyjson streaming writer; this is enabled by setting the build tag "launchdarkly_easyjson". 38 // Be aware that by default, easyjson uses Go's "unsafe" package (https://pkg.go.dev/unsafe), 39 // which may not be available on all platforms. 40 // 41 // Setting the "launchdarkly_easyjson" tag also adds a new constructor function, 42 // NewWriterFromEasyJSONWriter, allowing Writer-based code to send output directly to an 43 // existing EasyJSON jwriter.Writer. This may be desirable in order to define common marshaling 44 // logic that may be used with or without EasyJSON. For example: 45 // 46 // import ( 47 // ej_jwriter "github.com/mailru/easyjson/jwriter" 48 // ) 49 // 50 // func (s myStruct) MarshalEasyJSON(w *ej_jwriter.Writer) { 51 // ww := jwriter.NewWriterFromEasyJSONWriter(w) 52 // s.WriteToJSONWriter(&ww) 53 // } 54 package jwriter 55