MCPcopy Create free account
hub / github.com/buger/jsonparser / randomJSONValue

Function randomJSONValue

property_test.go:45–81  ·  view source on GitHub ↗

randomJSONValue builds a random nested Go value suitable for encoding/json.Marshal. The shape (depth, breadth, alternatives) is chosen by the supplied RNG so the generator is independent of the parser undertest.

(r *mathrand.Rand, depth int)

Source from the content-addressed store, hash-verified

43// encoding/json.Marshal. The shape (depth, breadth, alternatives) is chosen
44// by the supplied RNG so the generator is independent of the parser undertest.
45func randomJSONValue(r *mathrand.Rand, depth int) interface{} {
46 if depth <= 0 {
47 // Leaves: choose a scalar.
48 switch r.Intn(6) {
49 case 0:
50 return r.Int63()
51 case 1:
52 return r.Float64()
53 case 2:
54 return r.Intn(2) == 0
55 case 3:
56 return nil
57 case 4:
58 return randJSONString(r)
59 default:
60 return r.Int63()
61 }
62 }
63 switch r.Intn(3) {
64 case 0:
65 return randJSONString(r)
66 case 1:
67 n := r.Intn(4) + 1
68 arr := make([]interface{}, n)
69 for i := range arr {
70 arr[i] = randomJSONValue(r, depth-1)
71 }
72 return arr
73 default:
74 n := r.Intn(4) + 1
75 obj := make(map[string]interface{}, n)
76 for i := 0; i < n; i++ {
77 obj[randKey(r)] = randomJSONValue(r, depth-1)
78 }
79 return obj
80 }
81}
82
83func randKey(r *mathrand.Rand) string {
84 letters := "abcdefghijklmnopqrstuvwx"

Callers 5

randomObjectJSONBytesFunction · 0.85
randomJSONBytesFunction · 0.85
TestPropertyGetRoundTripFunction · 0.85

Calls 2

randJSONStringFunction · 0.85
randKeyFunction · 0.85

Tested by

no test coverage detected