@@ -24,21 +24,14 @@ class Adaptor:
24
24
def __init__ (self , data : Any ) -> None :
25
25
self ._d = data
26
26
27
- @overload
28
- def write_json (self , file : str ) -> None : ...
29
- @overload
30
- def write_json (self , file : None = ...) -> str : ...
31
- def write_json (self , file : str | None = None ) -> str | None :
32
- if file is None :
33
- msg = (
34
- f"Writing to JSON string rather than file is not supported for "
35
- f"{ type (self ._d )} "
36
- )
37
- raise NotImplementedError (msg )
27
+ def write_json (self , file : str ) -> None :
28
+ with open (file , "w" ) as f :
29
+ f .write (self .to_json ())
38
30
31
+ def to_json (self ) -> str :
39
32
import json
40
33
41
- json .dump (self ._d , open ( file , mode = "w" ) )
34
+ return json .dumps (self ._d )
42
35
43
36
def write_joblib (self , file : str ) -> None :
44
37
import joblib
@@ -101,7 +94,7 @@ def head(self, n: int) -> DFAdaptor: ...
101
94
def data_preview (self ) -> str :
102
95
# TODO(compat) is 100 hard-coded?
103
96
# Note that we go df -> json -> dict, to take advantage of type conversions in the dataframe library
104
- data : list [dict [Any , Any ]] = json .loads (self .head (100 ).write_json ())
97
+ data : list [dict [Any , Any ]] = json .loads (self .head (100 ).to_json ())
105
98
columns = [
106
99
{"name" : [col ], "label" : [col ], "align" : ["left" ], "type" : ["" ]}
107
100
for col in self .columns
@@ -135,18 +128,7 @@ def shape(self) -> tuple[int, int]:
135
128
def head (self , n : int ) -> PandasAdaptor :
136
129
return PandasAdaptor (self ._d .head (n ))
137
130
138
- @overload
139
- def write_json (self , file : str ) -> None : ...
140
- @overload
141
- def write_json (self , file : None ) -> str : ...
142
- def write_json (self , file : str | None = None ) -> str | None :
143
- if file is not None :
144
- msg = (
145
- f"Writing to file rather than JSON string is not supported for "
146
- f"{ type (self ._d )} "
147
- )
148
- raise NotImplementedError (msg )
149
-
131
+ def to_json (self ) -> str :
150
132
return self ._d .to_json (orient = "records" )
151
133
152
134
def write_csv (self , file : str ) -> None :
0 commit comments