...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jwriter/writer_examples_test.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/jwriter

     1  package jwriter
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  )
     9  
    10  func ExampleNewWriter() {
    11  	w := NewWriter()
    12  	obj := w.Object()
    13  	obj.Name("property").String("value")
    14  	obj.End()
    15  	fmt.Println(string(w.Bytes()))
    16  	// Output: {"property":"value"}
    17  }
    18  
    19  func ExampleNewStreamingWriter() {
    20  	w := NewStreamingWriter(os.Stdout, 10)
    21  	obj := w.Object()
    22  	obj.Name("property").String("value")
    23  	obj.End()
    24  	w.Flush()
    25  	// Output: {"property":"value"}
    26  }
    27  
    28  func ExampleWriter_AddError() {
    29  	w := NewWriter()
    30  	obj := w.Object()
    31  	obj.Name("prop1").Bool(true)
    32  	w.AddError(errors.New("sorry, we can't serialize this after all"))
    33  	obj.Name("prop2").Bool(true) // no output is generated here because the Writer has already failed
    34  	fmt.Println("error is:", w.Error())
    35  	fmt.Println("buffer is:", string(w.Bytes()))
    36  	// Output: error is: sorry, we can't serialize this after all
    37  	// buffer is: {"prop1":true
    38  }
    39  
    40  func ExampleWriter_Null() {
    41  	w := NewWriter()
    42  	w.Null()
    43  	fmt.Println(string(w.Bytes()))
    44  	// Output: null
    45  }
    46  
    47  func ExampleWriter_Bool() {
    48  	w := NewWriter()
    49  	w.Bool(true)
    50  	fmt.Println(string(w.Bytes()))
    51  	// Output: true
    52  }
    53  
    54  func ExampleWriter_BoolOrNull() {
    55  	w := NewWriter()
    56  	w.BoolOrNull(false, true)
    57  	fmt.Println(string(w.Bytes()))
    58  	// Output: null
    59  }
    60  
    61  func ExampleWriter_Int() {
    62  	w := NewWriter()
    63  	w.Int(123)
    64  	fmt.Println(string(w.Bytes()))
    65  	// Output: 123
    66  }
    67  
    68  func ExampleWriter_IntOrNull() {
    69  	w := NewWriter()
    70  	w.IntOrNull(false, 1)
    71  	fmt.Println(string(w.Bytes()))
    72  	// Output: null
    73  }
    74  
    75  func ExampleWriter_Float64() {
    76  	w := NewWriter()
    77  	w.Float64(1234.5)
    78  	fmt.Println(string(w.Bytes()))
    79  	// Output: 1234.5
    80  }
    81  
    82  func ExampleWriter_Float64OrNull() {
    83  	w := NewWriter()
    84  	w.Float64OrNull(false, 1)
    85  	fmt.Println(string(w.Bytes()))
    86  	// Output: null
    87  }
    88  
    89  func ExampleWriter_String() {
    90  	w := NewWriter()
    91  	w.String(`string says "hello"`)
    92  	fmt.Println(string(w.Bytes()))
    93  	// Output: "string says \"hello\""
    94  }
    95  
    96  func ExampleWriter_StringOrNull() {
    97  	w := NewWriter()
    98  	w.StringOrNull(false, "no")
    99  	fmt.Println(string(w.Bytes()))
   100  	// Output: null
   101  }
   102  
   103  func ExampleWriter_Array() {
   104  	w := NewWriter()
   105  	arr := w.Array()
   106  	arr.Bool(true)
   107  	arr.Int(3)
   108  	arr.End()
   109  	fmt.Println(string(w.Bytes()))
   110  	// Output: [true,3]
   111  }
   112  
   113  func ExampleWriter_Object() {
   114  	w := NewWriter()
   115  	obj := w.Object()
   116  	obj.Name("boolProperty").Bool(true)
   117  	obj.Name("intProperty").Int(3)
   118  	obj.End()
   119  	fmt.Println(string(w.Bytes()))
   120  	// Output: {"boolProperty":true,"intProperty":3}
   121  }
   122  
   123  func ExampleWriter_Raw() {
   124  	data := json.RawMessage(`{"value":1}`)
   125  	w := NewWriter()
   126  	w.Raw(data)
   127  
   128  	fmt.Println(string(w.Bytes()))
   129  	// Output: {"value":1}
   130  }
   131  

View as plain text