AsJSON converts a datum into our standard json representation.
(d Datum, loc *time.Location)
| 2732 | |
| 2733 | // AsJSON converts a datum into our standard json representation. |
| 2734 | func AsJSON(d Datum, loc *time.Location) (json.JSON, error) { |
| 2735 | switch t := d.(type) { |
| 2736 | case *DBool: |
| 2737 | return json.FromBool(bool(*t)), nil |
| 2738 | case *DInt: |
| 2739 | return json.FromInt(int(*t)), nil |
| 2740 | case *DFloat: |
| 2741 | return json.FromFloat64(float64(*t)) |
| 2742 | case *DDecimal: |
| 2743 | return json.FromDecimal(t.Decimal), nil |
| 2744 | case *DString: |
| 2745 | return json.FromString(string(*t)), nil |
| 2746 | case *DCollatedString: |
| 2747 | return json.FromString(t.Contents), nil |
| 2748 | case *DJSON: |
| 2749 | return t.JSON, nil |
| 2750 | case *DArray: |
| 2751 | builder := json.NewArrayBuilder(t.Len()) |
| 2752 | for _, e := range t.Array { |
| 2753 | j, err := AsJSON(e, loc) |
| 2754 | if err != nil { |
| 2755 | return nil, err |
| 2756 | } |
| 2757 | builder.Add(j) |
| 2758 | } |
| 2759 | return builder.Build(), nil |
| 2760 | case *DTuple: |
| 2761 | builder := json.NewObjectBuilder(len(t.D)) |
| 2762 | // We need to make sure that t.typ is initialized before getting the tuple |
| 2763 | // labels (it is valid for t.typ be left uninitialized when instantiating a |
| 2764 | // DTuple). |
| 2765 | t.maybePopulateType() |
| 2766 | labels := t.typ.TupleLabels() |
| 2767 | for i, e := range t.D { |
| 2768 | j, err := AsJSON(e, loc) |
| 2769 | if err != nil { |
| 2770 | return nil, err |
| 2771 | } |
| 2772 | var key string |
| 2773 | if i >= len(labels) { |
| 2774 | key = fmt.Sprintf("f%d", i+1) |
| 2775 | } else { |
| 2776 | key = labels[i] |
| 2777 | } |
| 2778 | builder.Add(key, j) |
| 2779 | } |
| 2780 | return builder.Build(), nil |
| 2781 | case *DTimestampTZ: |
| 2782 | // Our normal timestamp-formatting code uses a variation on RFC 3339, |
| 2783 | // without the T separator. This causes some compatibility problems |
| 2784 | // with certain JSON consumers, so we'll use an alternate formatting |
| 2785 | // path here to maintain consistency with PostgreSQL. |
| 2786 | return json.FromString(t.Time.In(loc).Format(time.RFC3339Nano)), nil |
| 2787 | case *DTimestamp: |
| 2788 | // This is RFC3339Nano, but without the TZ fields. |
| 2789 | return json.FromString(t.UTC().Format("2006-01-02T15:04:05.999999999")), nil |
| 2790 | case *DDate, *DUuid, *DOid, *DInterval, *DBytes, *DIPAddr, *DTime, *DTimeTZ, *DBitArray: |
| 2791 | return json.FromString(AsStringWithFlags(t, FmtBareStrings)), nil |
nothing calls this directly
no test coverage detected
searching dependent graphs…