def cleanup(json): """ Remove all missing values. Sometimes its useful for object methods to return missing value in order to not include that value in the json format. Examples:: >>> User(BourneMixin): ... def attributes(): ... return ['id', 'name', 'birthday', 'somefunc'] ... def age(): ... if birthday: ... return empty ... else: ... return calc_age(self.birthday) Now if some user has birthday the age function is going to return the age. However if user doesn't have birthday the age function is returning a special empty value which tells jsonifier not to include that key in json format. >>> User(id=1, name='someone').as_json() {'id': 1, 'name': 'Someone'} """ return dict(filter(lambda a: a[1] is not empty, json.items()))
def _do_create_from_json(cls, json, parse_def, context, duplicate_handling=None, object_importer=None): # Special case for JSON-LD added = False ls = cls() def guess_lang(value): from .discussion import Discussion discussion = context.get_instance_of_class(Discussion) if discussion: tr_service = discussion.translation_service() lang, _ = tr_service.identify(value) return LocaleLabel.UNDEFINED if isinstance(json, list): for entry_record in json: value = entry_record['@value'] if value: added = True lang = entry_record.get('@language', None) or guess_lang(value) ls.add_value(value, lang) elif isinstance(json, dict): if '@id' in json or '@type' in json: return super(LangString, cls)._do_create_from_json(json, parse_def, context, duplicate_handling, object_importer) elif '@value' in json: value = json['@value'] if value: added = True lang = json.get('@language', None) or guess_lang(value) ls.add_value(value, lang) else: for lang, value in json.items(): if value: added = True ls.add_value(value, lang) elif isinstance(json, string_types): if json: added = True lang = guess_lang(json) ls.add_value(json, lang) else: raise ValueError("Not a valid langstring: " + json) i_context = ls.get_instance_context(context) if added: cls.default_db.add(ls) else: i_context._instance = None return i_context
def _do_update_from_json( self, json, parse_def, context, duplicate_handling=None, object_importer=None): for key, value in json.items(): if key == '@id': if value != self.uri(): raise RuntimeError("Wrong id") else: self[key] = value return self
def JsonToSortedTab(self, json): strTab = [] outTab = [] for v, k in json.items(): strTab.append(int(v)) strTab.append(k) outTab.append(strTab) strTab = [] outTab.sort(key=lambda x: x[0]) return outTab
def _do_update_from_json(self, json, parse_def, context, duplicate_handling=None, object_importer=None): # Special case for JSON-LD if isinstance(json, list): for entry_record in json: lang = entry_record.get('@language', LocaleLabel.UNDEFINED) value = entry_record['@value'] entry = self.entries_as_dict.get(lang, None) if entry: entry.set_value(value) elif value: self.add_value(value, lang) elif isinstance(json, dict): if '@id' in json or '@type' in json: return super(LangString, self)._do_update_from_json( json, parse_def, context, duplicate_handling, object_importer) elif '@value' in json: value = json['@value'] if value: lang = json.get('@language', LocaleLabel.UNDEFINED) entry = self.entries_as_dict.get(lang, None) if entry: entry.set_value(value) elif value: self.add_value(value, lang) else: for lang, value in json.items(): entry = self.entries_as_dict.get(lang, None) if entry: entry.set_value(value) elif value: self.add_value(value, lang) elif isinstance(json, string_types): from .discussion import Discussion lang = LocaleLabel.UNDEFINED discussion = context.get_instance_of_class(Discussion) if discussion: tr_service = discussion.translation_service() lang, _ = tr_service.identify(json) entry = self.entries_as_dict.get(lang, None) if entry: entry.set_value(json) elif json: self.add_value(json, lang) else: raise ValueError("Not a valid langstring: " + json) return self
def _simplify_json(self, json): result = {} # First, resolve references for element in json.values(): for k,v in element.items(): if isinstance(v, unicode) and v.startswith("http://") and json.has_key(v): element[k] = json[v] for k, v in json.items(): if v.has_key("_typeGroup"): group = v["_typeGroup"] if not result.has_key(group): result[group]=[] del v["_typeGroup"] v["__reference"] = k result[group].append(v) return result
def _simplify_json(self, json): result = {} # First, resolve references for element in json.values(): for k,v in element.items(): if isinstance(v, str) and v.startswith("http://") and v in json: element[k] = json[v] for k, v in json.items(): if "_typeGroup" in v: group = v["_typeGroup"] if not group in result: result[group]=[] del v["_typeGroup"] v["__reference"] = k result[group].append(v) return result
def _fix_json(self, check_fun, mod_fun, json): if isinstance(json, dict): d = {} for k, v in json.items(): w = self._fix_json(check_fun, mod_fun, v) if check_fun(k, w): d[k] = w return d if isinstance(json, list): l = [] for v in json: w = self._fix_json(check_fun, mod_fun, v) if w: l.append(w) return l return mod_fun(json)
def json2xml(self, json): string = "" for k, v in json.items(): string = string + "<%s>" % (k) + str(v) + "</%s>" % (k) return string