...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jreader/reader_examples_test.go

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

     1  package jreader
     2  
     3  import "fmt"
     4  
     5  func ExampleNewReader() {
     6  	r := NewReader([]byte(`"a \"good\" string"`))
     7  	s := r.String()
     8  	if err := r.Error(); err != nil {
     9  		fmt.Println("error:", err.Error())
    10  	} else {
    11  		fmt.Println(s)
    12  	}
    13  	// Output: a "good" string
    14  }
    15  
    16  func ExampleReader_RequireEOF() {
    17  	r := NewReader([]byte(`100,"extra"`))
    18  	n := r.Int()
    19  	err := r.RequireEOF()
    20  	fmt.Println(n, err)
    21  	// Output: 100 unexpected data after end of JSON value at position 3
    22  }
    23  
    24  func ExampleReader_AddError() {
    25  	r := NewReader([]byte(`[1,2,3,4,5]`))
    26  	values := []int{}
    27  	for arr := r.Array(); arr.Next(); {
    28  		n := r.Int()
    29  		values = append(values, n)
    30  		if n > 1 {
    31  			r.AddError(fmt.Errorf("got an error after %d", n))
    32  		}
    33  	}
    34  	err := r.Error()
    35  	fmt.Println(values, err)
    36  	// Output: [1 2] got an error after 2
    37  }
    38  
    39  func ExampleReader_Null() {
    40  	r := NewReader([]byte(`null`))
    41  	if err := r.Null(); err != nil {
    42  		fmt.Println("error:", err)
    43  
    44  	} else {
    45  		fmt.Println("got a null")
    46  	}
    47  	// Output: got a null
    48  }
    49  
    50  func ExampleReader_Bool() {
    51  	r := NewReader([]byte(`true`))
    52  	var value bool = r.Bool()
    53  	if err := r.Error(); err != nil {
    54  		fmt.Println("error:", err)
    55  	} else {
    56  		fmt.Println("value:", value)
    57  	}
    58  	// Output: value: true
    59  }
    60  
    61  func ExampleReader_BoolOrNull() {
    62  	r1 := NewReader([]byte(`null`))
    63  	if value1, nonNull := r1.BoolOrNull(); nonNull {
    64  		fmt.Println("value1:", value1)
    65  	}
    66  	r2 := NewReader([]byte(`false`))
    67  	if value2, nonNull := r2.BoolOrNull(); nonNull {
    68  		fmt.Println("value2:", value2)
    69  	}
    70  	// Output: value2: false
    71  }
    72  
    73  func ExampleReader_Int() {
    74  	r := NewReader([]byte(`123`))
    75  	var value int = r.Int()
    76  	if err := r.Error(); err != nil {
    77  		fmt.Println("error:", err)
    78  	} else {
    79  		fmt.Println("value:", value)
    80  	}
    81  	// Output: value: 123
    82  }
    83  
    84  func ExampleReader_IntOrNull() {
    85  	r1 := NewReader([]byte(`null`))
    86  	if value1, nonNull := r1.IntOrNull(); nonNull {
    87  		fmt.Println("value1:", value1)
    88  	}
    89  	r2 := NewReader([]byte(`0`))
    90  	if value2, nonNull := r2.IntOrNull(); nonNull {
    91  		fmt.Println("value2:", value2)
    92  	}
    93  	// Output: value2: 0
    94  }
    95  
    96  func ExampleReader_Float64() {
    97  	r := NewReader([]byte(`1234.5`))
    98  	var value float64 = r.Float64()
    99  	if err := r.Error(); err != nil {
   100  		fmt.Println("error:", err)
   101  	} else {
   102  		fmt.Println("value:", value)
   103  	}
   104  	// Output: value: 1234.5
   105  }
   106  
   107  func ExampleReader_Float64OrNull() {
   108  	r1 := NewReader([]byte(`null`))
   109  	if value1, nonNull := r1.Float64OrNull(); nonNull {
   110  		fmt.Println("value1:", value1)
   111  	}
   112  	r2 := NewReader([]byte(`0`))
   113  	if value2, nonNull := r2.Float64OrNull(); nonNull {
   114  		fmt.Println("value2:", value2)
   115  	}
   116  	// Output: value2: 0
   117  }
   118  
   119  func ExampleReader_String() {
   120  	r := NewReader([]byte(`"a \"good\" string"`))
   121  	var value string = r.String()
   122  	if err := r.Error(); err != nil {
   123  		fmt.Println("error:", err)
   124  	} else {
   125  		fmt.Println("value:", value)
   126  	}
   127  	// Output: value: a "good" string
   128  }
   129  
   130  func ExampleReader_StringOrNull() {
   131  	r1 := NewReader([]byte(`null`))
   132  	if value1, nonNull := r1.StringOrNull(); nonNull {
   133  		fmt.Println("value1:", "\""+value1+"\"")
   134  	}
   135  	r2 := NewReader([]byte(`""`))
   136  	if value2, nonNull := r2.StringOrNull(); nonNull {
   137  		fmt.Println("value2:", "\""+value2+"\"")
   138  	}
   139  	// Output: value2: ""
   140  }
   141  
   142  func ExampleReader_Array() {
   143  	r := NewReader([]byte(`[1,2]`))
   144  	values := []int{}
   145  	for arr := r.Array(); arr.Next(); {
   146  		values = append(values, r.Int())
   147  	}
   148  	fmt.Println("values:", values)
   149  	// Output: values: [1 2]
   150  }
   151  
   152  func ExampleReader_ArrayOrNull() {
   153  	printArray := func(input string) {
   154  		r := NewReader([]byte(input))
   155  		values := []int{}
   156  		arr := r.Array()
   157  		for arr.Next() {
   158  			values = append(values, r.Int())
   159  		}
   160  		fmt.Println(input, "->", values, "... IsDefined =", arr.IsDefined())
   161  	}
   162  	printArray("null")
   163  	printArray("[1,2]")
   164  	// Output: null -> [] ... IsDefined = false
   165  	// [1,2] -> [1 2] ... IsDefined = true
   166  }
   167  
   168  func ExampleReader_Object() {
   169  	r := NewReader([]byte(`{"a":1,"b":2}`))
   170  	items := []string{}
   171  	for obj := r.Object(); obj.Next(); {
   172  		name := obj.Name()
   173  		value := r.Int()
   174  		items = append(items, fmt.Sprintf("%s=%d", name, value))
   175  	}
   176  	fmt.Println("items:", items)
   177  	// Output: items: [a=1 b=2]
   178  }
   179  
   180  func ExampleReader_ObjectOrNull() {
   181  	printObject := func(input string) {
   182  		r := NewReader([]byte(input))
   183  		items := []string{}
   184  		obj := r.Object()
   185  		for obj.Next() {
   186  			name := obj.Name()
   187  			value := r.Int()
   188  			items = append(items, fmt.Sprintf("%s=%d", name, value))
   189  		}
   190  		fmt.Println(input, "->", items, "... IsDefined =", obj.IsDefined())
   191  	}
   192  	printObject("null")
   193  	printObject(`{"a":1,"b":2}`)
   194  	// Output: null -> [] ... IsDefined = false
   195  	// {"a":1,"b":2} -> [a=1 b=2] ... IsDefined = true
   196  }
   197  
   198  func ExampleReader_Any() {
   199  	printValue := func(input string) {
   200  		r := NewReader([]byte(input))
   201  		value := r.Any()
   202  		switch value.Kind {
   203  		case NullValue:
   204  			fmt.Println("a null")
   205  		case BoolValue:
   206  			fmt.Println("a bool:", value.Bool)
   207  		case NumberValue:
   208  			fmt.Println("a number:", value.Number)
   209  		case StringValue:
   210  			fmt.Println("a string:", value.String)
   211  		case ArrayValue:
   212  			n := 0
   213  			for value.Array.Next() {
   214  				n++ // for this example, we're not looking at the actual element value
   215  			}
   216  			fmt.Println("an array with", n, "elements")
   217  		case ObjectValue:
   218  			n := 0
   219  			for value.Object.Next() {
   220  				n++ // for this example, we're not looking at the actual element value
   221  			}
   222  		}
   223  	}
   224  	printValue(`123`)
   225  	printValue(`["a","b"]`)
   226  	// Output: a number: 123
   227  	// an array with 2 elements
   228  }
   229  
   230  func ExampleObjectState_WithRequiredProperties() {
   231  	requiredProps := []string{"key", "name"}
   232  	r := NewReader([]byte(`{"name": "x"}`))
   233  	var key, name string
   234  	for obj := r.Object().WithRequiredProperties(requiredProps); obj.Next(); {
   235  		switch string(obj.Name()) {
   236  		case "key":
   237  			key = r.String()
   238  		case "name":
   239  			name = r.String()
   240  		}
   241  	}
   242  	if err := r.Error(); err != nil {
   243  		if rpe, ok := err.(RequiredPropertyError); ok {
   244  			fmt.Println("missing property:", rpe.Name)
   245  		} else {
   246  			fmt.Println("unexpected error:", err)
   247  		}
   248  	} else {
   249  		fmt.Println(key, name)
   250  	}
   251  	// Output: missing property: key
   252  }
   253  

View as plain text