AsJSON converts a datum into our standard json representation.
( d Datum, dcc sessiondatapb.DataConversionConfig, loc *time.Location, )
| 4086 | |
| 4087 | // AsJSON converts a datum into our standard json representation. |
| 4088 | func AsJSON( |
| 4089 | d Datum, dcc sessiondatapb.DataConversionConfig, loc *time.Location, |
| 4090 | ) (json.JSON, error) { |
| 4091 | d = UnwrapDOidWrapper(d) |
| 4092 | switch t := d.(type) { |
| 4093 | case *DBool: |
| 4094 | return json.FromBool(bool(*t)), nil |
| 4095 | case *DInt: |
| 4096 | return json.FromInt(int(*t)), nil |
| 4097 | case *DFloat: |
| 4098 | return json.FromFloat64(float64(*t)) |
| 4099 | case *DDecimal: |
| 4100 | return json.FromDecimal(t.Decimal), nil |
| 4101 | case *DString: |
| 4102 | return json.FromString(string(*t)), nil |
| 4103 | case *DCollatedString: |
| 4104 | return json.FromString(t.Contents), nil |
| 4105 | case *DEnum: |
| 4106 | return json.FromString(t.LogicalRep), nil |
| 4107 | case *DJSON: |
| 4108 | return t.JSON, nil |
| 4109 | case *DArray: |
| 4110 | builder := json.NewArrayBuilder(t.Len()) |
| 4111 | for _, e := range t.Array { |
| 4112 | j, err := AsJSON(e, dcc, loc) |
| 4113 | if err != nil { |
| 4114 | return nil, err |
| 4115 | } |
| 4116 | builder.Add(j) |
| 4117 | } |
| 4118 | return builder.Build(), nil |
| 4119 | case *DTuple: |
| 4120 | builder := json.NewObjectBuilder(len(t.D)) |
| 4121 | // We need to make sure that t.typ is initialized before getting the tuple |
| 4122 | // labels (it is valid for t.typ be left uninitialized when instantiating a |
| 4123 | // DTuple). |
| 4124 | t.maybePopulateType() |
| 4125 | labels := t.typ.TupleLabels() |
| 4126 | for i, e := range t.D { |
| 4127 | j, err := AsJSON(e, dcc, loc) |
| 4128 | if err != nil { |
| 4129 | return nil, err |
| 4130 | } |
| 4131 | var key string |
| 4132 | if i >= len(labels) { |
| 4133 | key = fmt.Sprintf("f%d", i+1) |
| 4134 | } else { |
| 4135 | key = labels[i] |
| 4136 | } |
| 4137 | builder.Add(key, j) |
| 4138 | } |
| 4139 | return builder.Build(), nil |
| 4140 | case *DTimestampTZ: |
| 4141 | // Our normal timestamp-formatting code uses a variation on RFC 3339, |
| 4142 | // without the T separator. This causes some compatibility problems |
| 4143 | // with certain JSON consumers, so we'll use an alternate formatting |
| 4144 | // path here to maintain consistency with PostgreSQL. |
| 4145 | return json.FromString(formatTime(t.Time.In(loc), time.RFC3339Nano)), nil |
nothing calls this directly
no test coverage detected
searching dependent graphs…