-
Notifications
You must be signed in to change notification settings - Fork 44
/
jsons.py
45 lines (35 loc) · 1.12 KB
/
jsons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
from ...utils import get_json_encoder
from ..internal_utils.options import Options
from ..internal_utils.readers.jsonreader import JSONReader
from ..types import StringType
from .expressions import Expression
class StructsToJson(Expression):
pretty_name = "structstojson"
default_options = dict(
dateFormat="yyyy-MM-dd",
timestampFormat="yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
)
def __init__(self, column, options):
super().__init__(column)
self.column = column
self.input_options = options
self.options = Options(JSONReader.default_options, options)
self.encoder = get_json_encoder(self.options)
def eval(self, row, schema):
value = self.column.eval(row, schema)
return json.dumps(
value,
cls=self.encoder,
separators=(',', ':')
)
def args(self):
if self.input_options is None:
return (self.column, )
return (
self.column,
self.input_options
)
def data_type(self, schema):
return StringType()
__all__ = ["StructsToJson"]