valueToJsonRaw converts a single value to a JSON byte slice.
(ctx *sql.Context, elemType *pgtypes.DoltgresType, val any)
| 86 | |
| 87 | // valueToJsonRaw converts a single value to a JSON byte slice. |
| 88 | func valueToJsonRaw(ctx *sql.Context, elemType *pgtypes.DoltgresType, val any) (json.RawMessage, error) { |
| 89 | if val == nil { |
| 90 | return json.RawMessage("null"), nil |
| 91 | } |
| 92 | switch v := val.(type) { |
| 93 | case *apd.Decimal: |
| 94 | return json.RawMessage(v.Text('f')), nil |
| 95 | case pgtypes.JsonDocument: |
| 96 | sb := strings.Builder{} |
| 97 | pgtypes.JsonValueFormatter(&sb, v.Value) |
| 98 | return json.RawMessage(sb.String()), nil |
| 99 | case types.JSONDocument: |
| 100 | return json.Marshal(v.Val) |
| 101 | case sql.JSONWrapper: |
| 102 | jsonVal, err := v.ToInterface(ctx) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | return json.Marshal(jsonVal) |
| 107 | case []any: |
| 108 | return arrayToJsonRaw(ctx, elemType, v) |
| 109 | case string: |
| 110 | // JSON-typed values are already valid JSON and should be embedded without re-quoting. |
| 111 | if elemType != nil && elemType.ID.TypeName() == "json" { |
| 112 | return json.RawMessage(v), nil |
| 113 | } |
| 114 | return json.Marshal(v) |
| 115 | default: |
| 116 | return json.Marshal(val) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // arrayToJsonPretty produces a pretty-printed JSON array where dimension-1 elements are |
| 121 | // separated by a comma and newline. |
no test coverage detected