MCPcopy Create free account
hub / github.com/dolthub/doltgresql / valueToJsonRaw

Function valueToJsonRaw

server/functions/array_to_json.go:88–118  ·  view source on GitHub ↗

valueToJsonRaw converts a single value to a JSON byte slice.

(ctx *sql.Context, elemType *pgtypes.DoltgresType, val any)

Source from the content-addressed store, hash-verified

86
87// valueToJsonRaw converts a single value to a JSON byte slice.
88func 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.

Callers 3

rowToJsonFunction · 0.85
arrayToJsonRawFunction · 0.85
arrayToJsonPrettyFunction · 0.85

Calls 4

arrayToJsonRawFunction · 0.85
TypeNameMethod · 0.80
StringMethod · 0.65
MarshalMethod · 0.45

Tested by

no test coverage detected