PrepareValue implements [Field.PrepareValue] interface method.
(record *Record, raw any)
| 117 | |
| 118 | // PrepareValue implements [Field.PrepareValue] interface method. |
| 119 | func (f *JSONField) PrepareValue(record *Record, raw any) (any, error) { |
| 120 | if str, ok := raw.(string); ok { |
| 121 | // in order to support seamlessly both json and multipart/form-data requests, |
| 122 | // the following normalization rules are applied for plain string values: |
| 123 | // - "true" is converted to the json `true` |
| 124 | // - "false" is converted to the json `false` |
| 125 | // - "null" is converted to the json `null` |
| 126 | // - "[1,2,3]" is converted to the json `[1,2,3]` |
| 127 | // - "{\"a\":1,\"b\":2}" is converted to the json `{"a":1,"b":2}` |
| 128 | // - numeric strings are converted to json number |
| 129 | // - double quoted strings are left as they are (aka. without normalizations) |
| 130 | // - any other string (empty string too) is double quoted |
| 131 | if str == "" { |
| 132 | raw = strconv.Quote(str) |
| 133 | } else if str == "null" || str == "true" || str == "false" { |
| 134 | raw = str |
| 135 | } else if ((str[0] >= '0' && str[0] <= '9') || |
| 136 | str[0] == '-' || |
| 137 | str[0] == '"' || |
| 138 | str[0] == '[' || |
| 139 | str[0] == '{') && |
| 140 | is.JSON.Validate(str) == nil { |
| 141 | raw = str |
| 142 | } else { |
| 143 | raw = strconv.Quote(str) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return types.ParseJSONRaw(raw) |
| 148 | } |
| 149 | |
| 150 | var emptyJSONValues = []string{ |
| 151 | "null", `""`, "[]", "{}", "", |