1# -*- coding: utf-8 -*- 2# ============================================================================= 3# Authors : Alexander Kmoch <allixender@gmail.com> 4# 5# ============================================================================= 6 7""" 8API for OGC Web Services Context Document (OWS Context) format. 9 10GeoJson Encoding: http://www.opengeospatial.org/standards/owc 11 12OGC OWS Context GeoJSON Encoding Standard 1.0 (14-055r2) 13""" 14 15import json 16from datetime import datetime 17from owslib.owscontext.common import skip_nulls, skip_nulls_rec 18 19 20# from owslib.util import log 21 22class DateTimeEncoder(json.JSONEncoder): 23 """ 24 https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable/36142844#36142844 25 26 usage: json.dumps(yourobj, cls=DateTimeEncoder) 27 """ 28 29 def default(self, o): 30 if isinstance(o, datetime): 31 return o.isoformat() 32 33 return json.JSONEncoder.default(self, o) 34 35 36def decode_json(jsondata): 37 """ 38 TODO do we need to make sure everything is UTF-8? 39 here parse json to an instance of OWC:Context 40 41 :param jsondata: 42 :return: dict 43 """ 44 return json.loads(jsondata, object_hook=skip_nulls) 45 46 47def encode_json(obj): 48 """ 49 TODO do we need to make sure everything is UTF-8? 50 eg. ensure_ascii=False, encoding='utf8) .encode('utf8') ? 51 encode instance of OWCContext/or subclass into GeoJson encoding 52 53 :param obj: 54 :return: JSON 55 """ 56 jsdata = json.dumps(skip_nulls_rec(obj), cls=DateTimeEncoder) 57 58 return jsdata 59