@@ -65,6 +65,76 @@ func TestGoldenFrameJSON(t *testing.T) {
65
65
if diff := cmp .Diff (f , out , data .FrameTestCompareOptions ()... ); diff != "" {
66
66
t .Errorf ("Result mismatch (-want +got):\n %s" , diff )
67
67
}
68
+
69
+ // Now try each field as the only field in the frame
70
+ for idx , field := range f .Fields {
71
+ expected := out .Fields [idx ]
72
+ size := expected .Len ()
73
+
74
+ frame := data .NewFrame ("test-" + field .Name , field )
75
+ b , err := data .FrameToJSON (frame , data .IncludeAll ) // json.Marshal(f2)
76
+ require .NoError (t , err )
77
+
78
+ f2 := & data.Frame {}
79
+ err = json .Unmarshal (b , f2 )
80
+ require .NoError (t , err )
81
+
82
+ found := f2 .Fields [0 ]
83
+ require .Equal (t , size , found .Len ())
84
+ for i := 0 ; i < size ; i ++ {
85
+ // Lots of NaN flavors that are really the same
86
+ if expected .Type ().Numeric () {
87
+ fA , _ := expected .NullableFloatAt (i )
88
+ fB , _ := found .NullableFloatAt (i )
89
+ if fA != nil && fB != nil {
90
+ if math .IsNaN (* fA ) && math .IsNaN (* fB ) {
91
+ continue // many flavors of NaN!
92
+ }
93
+ }
94
+ }
95
+
96
+ switch expected .Type () {
97
+ case data .FieldTypeJSON , data .FieldTypeNullableJSON :
98
+ msgA , okA := expected .ConcreteAt (i )
99
+ msgB , okB := found .ConcreteAt (i )
100
+ if okA && okB {
101
+ a := string (msgA .(json.RawMessage ))
102
+ b := string (msgB .(json.RawMessage ))
103
+ require .JSONEq (t , a , b , field .Name ) // pretty print does not matter
104
+ continue
105
+ } else if expected .Type () == data .FieldTypeNullableJSON {
106
+ // The pointer conversions get too weird for this to be reasonable
107
+ continue
108
+ }
109
+ }
110
+
111
+ assert .EqualValues (t , expected .At (i ), found .At (i ), "field: %s, row: %d" , field .Name , i )
112
+ }
113
+ }
114
+ }
115
+
116
+ func TestMarshalFieldTypeJSON (t * testing.T ) {
117
+ testJSON := func (ft data.FieldType ) {
118
+ msg := json .RawMessage ([]byte (`{"l1":"v1"}` ))
119
+ f1 := data .NewFieldFromFieldType (ft , 1 )
120
+ f1 .SetConcrete (0 , msg )
121
+
122
+ f := data .NewFrame ("frame" , f1 )
123
+
124
+ fbytes , err := json .Marshal (f )
125
+ require .NoError (t , err )
126
+
127
+ // fmt.Println("frame in json:", string(fbytes))
128
+
129
+ f2 := & data.Frame {}
130
+ err = json .Unmarshal (fbytes , f2 )
131
+ require .NoError (t , err )
132
+ val , ok := f2 .Fields [0 ].ConcreteAt (0 )
133
+ require .True (t , ok )
134
+ require .Equal (t , msg , val )
135
+ }
136
+ testJSON (data .FieldTypeJSON )
137
+ testJSON (data .FieldTypeNullableJSON ) // this version is silly
68
138
}
69
139
70
140
type simpleTestObj struct {
0 commit comments