def convert_row_to_dict(self, row, schema): """Converts a TableRow instance using the schema to a Python dict.""" result = {} for index, field in enumerate(schema.fields): value = None if isinstance(schema, bigquery.TableSchema): cell = row.f[index] value = from_json_value(cell.v) if cell.v is not None else None elif isinstance(schema, bigquery.TableFieldSchema): cell = row['f'][index] value = cell['v'] if 'v' in cell else None if field.mode == 'REPEATED': result[field.name] = [ self._convert_cell_value_to_dict(x['v'], field) for x in value ] elif value is None: if not field.mode == 'NULLABLE': raise ValueError( 'Received \'None\' as the value for the field %s ' 'but the field is not NULLABLE.', field.name) result[field.name] = None else: result[field.name] = self._convert_cell_value_to_dict( value, field) return result
def encode(self, table_row): if self.table_schema is None: raise AttributeError( 'The TableRowJsonCoder requires a table schema for ' 'encoding operations. Please specify a table_schema argument.') try: return json.dumps(collections.OrderedDict( zip(self.field_names, [from_json_value(f.v) for f in table_row.f])), allow_nan=False) except ValueError as e: raise ValueError('%s. %s' % (e, JSON_COMPLIANCE_ERROR))
def convert_row_to_dict(self, row, schema): """Converts a TableRow instance using the schema to a Python dict.""" result = {} for index, field in enumerate(schema.fields): value = None if isinstance(schema, bigquery.TableSchema): cell = row.f[index] if cell.v is None: continue # Field not present in the row. value = from_json_value(cell.v) elif isinstance(schema, bigquery.TableFieldSchema): cell = row['f'][index] if 'v' not in cell: continue # Field not present in the row. value = cell['v'] if field.mode == 'REPEATED': result[field.name] = [self._convert_cell_value_to_dict(x['v'], field) for x in value] else: result[field.name] = self._convert_cell_value_to_dict(value, field) return result
def test_large_integer(self): num = 1 << 35 self.assertEquals(num, from_json_value(to_json_value(num))) self.assertEquals(long(num), from_json_value(to_json_value(long(num))))
def test_long_value(self): self.assertEquals(long(27), from_json_value(to_json_value(long(27))))
def test_with_type(self): rt = from_json_value(to_json_value('abcd', with_type=True)) self.assertEquals('http://schema.org/Text', rt['@type']) self.assertEquals('abcd', rt['value'])
def test_none_from(self): self.assertIsNone(from_json_value(to_json_value(None)))
def test_float_from(self): self.assertEquals(4.5, from_json_value(to_json_value(4.5)))
def test_int_from(self): self.assertEquals(-27, from_json_value(to_json_value(-27)))
def test_false_from(self): self.assertEquals(False, from_json_value(to_json_value(False)))
def test_true_from(self): self.assertEquals(True, from_json_value(to_json_value(True)))
def test_string_from(self): self.assertEquals('WXYZ', from_json_value(to_json_value('WXYZ')))