| Home | Trees | Indices | Help |
|
|---|
|
|
1 """
2 Writing tables in JSON.
3
4 We use python's built-in json engine to write an -- as yet -- ad-hoc json
5 format that essentially looks like this:
6
7 {
8 "contains": "table",
9 "columns": { (column metadata more or less as in VOTable) }
10 "data": { (rows as tuples) }
11 ("warnings": [...])
12 }
13
14 No streaming at all is forseen for this format at this point.
15 """
16
17 #c Copyright 2008-2019, the GAVO project
18 #c
19 #c This program is free software, covered by the GNU GPL. See the
20 #c COPYING file in the source distribution.
21
22
23 import json
24
25 from gavo import base
26 from gavo import rsc
27 from gavo.formats import common
28
29
31 """A MetaBuilder for mapping table meta information into our standard
32 JSON structure.
33 """
37
39 return self.result
40
46
47
49 """returns a sequence of VOTable-like column description dictionaries.
50 """
51 res = []
52 for annCol in serManager:
53 res.append(annCol.annotations.copy())
54 res[-1].pop("displayHint", None)
55 res[-1].pop("winningFactory", None)
56 res[-1].pop("nullvalue", None)
57 res[-1].pop("note", None)
58 return res
59
60
62 """returns a dict of param dicts.
63 """
64 result = {
65 'contains': "params",
66 }
67 for param in serManager.table.iterParams():
68 if param.value is not None:
69 result[param.name] = {
70 'value': param.value,
71 'unit': param.unit,
72 'ucd': param.ucd,
73 'description': param.description,}
74 return result
75
76
78 """returns a dictionary representing table for JSON serialisation.
79 """
80 if isinstance(table, rsc.Data):
81 table = table.getPrimaryTable()
82 sm = base.SerManager(table, acquireSamples=acquireSamples)
83
84 result = {
85 'contains': "table",
86 'params': _getJSONParams(sm),
87 'columns': _getJSONColumns(sm),
88 'data': list(sm.getMappedTuples()),
89 }
90 table.traverse(JSONMetaBuilder(result))
91 return result
92
93
99
100
101 # NOTE: while json could easily serialize full data elements,
102 # right now we're only writing single tables.
103 common.registerDataWriter("json", writeTableAsJSON, "application/json",
104 "JSON", ".json")
105
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Thu May 2 07:29:09 2019 | http://epydoc.sourceforge.net |