1 package ldvalue 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "reflect" 7 8 "github.com/launchdarkly/go-jsonstream/v3/jreader" 9 "github.com/launchdarkly/go-jsonstream/v3/jwriter" 10 ) 11 12 // JSONString returns the JSON representation of the value as a string. This is 13 // guaranteed to be logically equivalent to calling [json.Marshal] and converting the 14 // first return value to a string. 15 // 16 // Since types that support this method are by definition always convertible to JSON, 17 // it does not need to return an error value so it can be easily used within an 18 // expression. 19 func (o OptionalBool) JSONString() string { 20 return o.AsValue().JSONString() 21 } 22 23 // JSONString returns the JSON representation of the value as a string. This is 24 // guaranteed to be logically equivalent to calling [json.Marshal] and converting the 25 // first return value to a string. 26 // 27 // Since types that support this method are by definition always convertible to JSON, 28 // it does not need to return an error value so it can be easily used within an 29 // expression. 30 func (o OptionalInt) JSONString() string { 31 return o.AsValue().JSONString() 32 } 33 34 // JSONString returns the JSON representation of the value as a string. This is 35 // guaranteed to be logically equivalent to calling [json.Marshal] and converting the 36 // first return value to a string. 37 // 38 // Since types that support this method are by definition always convertible to JSON, 39 // it does not need to return an error value so it can be easily used within an 40 // expression. 41 func (o OptionalString) JSONString() string { 42 return o.AsValue().JSONString() 43 } 44 45 // MarshalJSON converts the OptionalBool to its JSON representation. 46 // 47 // The output will be either a JSON boolean or null. Note that the "omitempty" tag for a struct 48 // field will not cause an empty OptionalBool field to be omitted; it will be output as null. 49 // If you want to completely omit a JSON property when there is no value, it must be a bool 50 // pointer instead of an OptionalBool; use [OptionalBool.AsPointer] to get a pointer. 51 func (o OptionalBool) MarshalJSON() ([]byte, error) { 52 return o.AsValue().MarshalJSON() 53 } 54 55 // MarshalJSON converts the OptionalInt to its JSON representation. 56 // 57 // The output will be either a JSON number or null. Note that the "omitempty" tag for a struct 58 // field will not cause an empty OptionalInt field to be omitted; it will be output as null. 59 // If you want to completely omit a JSON property when there is no value, it must be an int 60 // pointer instead of an OptionalInt; use [OptionalInt.AsPointer] method to get a pointer. 61 func (o OptionalInt) MarshalJSON() ([]byte, error) { 62 return o.AsValue().MarshalJSON() 63 } 64 65 // MarshalJSON converts the OptionalString to its JSON representation. 66 // 67 // The output will be either a JSON string or null. Note that the "omitempty" tag for a struct 68 // field will not cause an empty OptionalString field to be omitted; it will be output as null. 69 // If you want to completely omit a JSON property when there is no value, it must be a string 70 // pointer instead of an OptionalString; use [OptionalString.AsPointer] method to get a pointer. 71 func (o OptionalString) MarshalJSON() ([]byte, error) { 72 return o.AsValue().MarshalJSON() 73 } 74 75 // UnmarshalJSON parses an OptionalBool from JSON. 76 // 77 // The input must be either a JSON boolean or null. 78 func (o *OptionalBool) UnmarshalJSON(data []byte) error { 79 if bytes.Equal(data, []byte("null")) { 80 *o = OptionalBool{} 81 return nil 82 } 83 if bytes.Equal(data, []byte("true")) { 84 *o = NewOptionalBool(true) 85 return nil 86 } 87 if bytes.Equal(data, []byte("false")) { 88 *o = NewOptionalBool(false) 89 return nil 90 } 91 return &json.UnmarshalTypeError{Value: string(data), Type: reflect.TypeOf(o)} 92 } 93 94 // UnmarshalJSON parses an OptionalInt from JSON. 95 // 96 // The input must be either a JSON number that is an integer or null. 97 func (o *OptionalInt) UnmarshalJSON(data []byte) error { 98 return jreader.UnmarshalJSONWithReader(data, o) 99 } 100 101 // UnmarshalJSON parses an OptionalString from JSON. 102 // 103 // The input must be either a JSON string or null. 104 func (o *OptionalString) UnmarshalJSON(data []byte) error { 105 return jreader.UnmarshalJSONWithReader(data, o) 106 } 107 108 // ReadFromJSONReader provides JSON deserialization for use with the jsonstream API. 109 // 110 // This implementation is used by the SDK in cases where it is more efficient than [json.Unmarshal]. 111 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 112 func (o *OptionalBool) ReadFromJSONReader(r *jreader.Reader) { 113 val, nonNull := r.BoolOrNull() 114 if r.Error() == nil { 115 if nonNull { 116 *o = NewOptionalBool(val) 117 } else { 118 *o = OptionalBool{} 119 } 120 } 121 } 122 123 // ReadFromJSONReader provides JSON deserialization for use with the jsonstream API. 124 // 125 // This implementation is used by the SDK in cases where it is more efficient than [json.Unmarshal]. 126 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 127 func (o *OptionalInt) ReadFromJSONReader(r *jreader.Reader) { 128 val, nonNull := r.IntOrNull() 129 if r.Error() == nil { 130 if nonNull { 131 *o = NewOptionalInt(val) 132 } else { 133 *o = OptionalInt{} 134 } 135 } 136 } 137 138 // ReadFromJSONReader provides JSON deserialization for use with the jsonstream API. 139 // 140 // This implementation is used by the SDK in cases where it is more efficient than [json.Unmarshal]. 141 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 142 func (o *OptionalString) ReadFromJSONReader(r *jreader.Reader) { 143 val, nonNull := r.StringOrNull() 144 if r.Error() == nil { 145 if nonNull { 146 *o = NewOptionalString(val) 147 } else { 148 *o = OptionalString{} 149 } 150 } 151 } 152 153 // WriteToJSONWriter provides JSON serialization for use with the jsonstream API. 154 // 155 // This implementation is used by the SDK in cases where it is more efficient than [json.Marshal]. 156 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 157 func (o OptionalBool) WriteToJSONWriter(w *jwriter.Writer) { 158 o.AsValue().WriteToJSONWriter(w) 159 } 160 161 // WriteToJSONWriter provides JSON serialization for use with the jsonstream API. 162 // 163 // This implementation is used by the SDK in cases where it is more efficient than [json.Marshal]. 164 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 165 func (o OptionalInt) WriteToJSONWriter(w *jwriter.Writer) { 166 o.AsValue().WriteToJSONWriter(w) 167 } 168 169 // WriteToJSONWriter provides JSON serialization for use with the jsonstream API. 170 // 171 // This implementation is used by the SDK in cases where it is more efficient than [json.Marshal]. 172 // See [github.com/launchdarkly/go-jsonstream/v3] for more details. 173 func (o OptionalString) WriteToJSONWriter(w *jwriter.Writer) { 174 o.AsValue().WriteToJSONWriter(w) 175 } 176