Exemple #1
0
 def serialize(self, obj):
     if is_base_type(obj):
         return BasicSerializer().serialize(obj)
     elif has_cls_attr(obj.__class__, PYCKSON_SERIALIZER):
         return get_custom_serializer(obj.__class__).serialize(obj)
     else:
         return ClassSerializer(self.model_provider).serialize(obj)
Exemple #2
0
 def get(self, obj_type, parent_class, name_in_parent) -> Serializer:
     if obj_type in BASIC_TYPES or obj_type in EXTRA_TYPES:
         return BasicSerializer()
     if obj_type in DATE_TYPES:
         return DateSerializer(parent_class, obj_type)
     if obj_type is Decimal:
         return DecimalSerializer()
     if type(obj_type) is str or type(obj_type) is ForwardRef:
         return GenericSerializer(self.model_provider)
     if obj_type is list or obj_type is set:
         type_info = get_cls_attr(parent_class, PYCKSON_TYPEINFO, dict())
         if name_in_parent in type_info:
             sub_type = type_info[name_in_parent]
             return ListSerializer(self.get(sub_type, parent_class, name_in_parent))
         else:
             raise TypeError('list parameter {} in class {} has no subType'.format(name_in_parent,
                                                                                   parent_class.__name__))
     if is_list_annotation(obj_type) or is_set_annotation(obj_type):
         return ListSerializer(self.get(obj_type.__args__[0], parent_class, name_in_parent))
     if is_enum_annotation(obj_type):
         return EnumSerializer()
     if is_basic_dict_annotation(obj_type):
         return BasicDictSerializer()
     if is_typing_dict_annotation(obj_type):
         if obj_type.__args__[0] != str:
             raise TypeError('typing.Dict key can only be str in class {}'.format(parent_class))
         return TypingDictSerializer(self.get(obj_type.__args__[1], parent_class, name_in_parent))
     if has_cls_attr(obj_type, PYCKSON_SERIALIZER):
         return CustomDeferredSerializer(obj_type)
     return ClassSerializer(self.model_provider)
Exemple #3
0
 def parse(self, json_value):
     if is_base_type(self.cls):
         return BasicParser().parse(json_value)
     elif is_base_type_with_cast(self.cls):
         return BasicParserWithCast(self.cls).parse(json_value)
     elif has_cls_attr(self.cls, PYCKSON_PARSER):
         return get_custom_parser(self.cls).parse(json_value)
     else:
         return ClassParser(self.cls, self.model_provider).parse(json_value)
Exemple #4
0
 def get(self, obj_type, parent_class, name_in_parent) -> Parser:
     if obj_type in BASIC_TYPES:
         return BasicParserWithCast(obj_type)
     if obj_type in EXTRA_TYPES:
         return BasicParser()
     if obj_type in DATE_TYPES:
         return DateParser(parent_class, obj_type)
     if obj_type is Decimal:
         return DecimalParser()
     if type(obj_type) is str or type(obj_type) is ForwardRef:
         return UnresolvedParser(TypeProvider(parent_class, obj_type),
                                 self.model_provider)
     if obj_type is list:
         type_info = get_cls_attr(parent_class, PYCKSON_TYPEINFO, dict())
         if name_in_parent in type_info:
             sub_type = type_info[name_in_parent]
             return ListParser(
                 self.get(sub_type, parent_class, name_in_parent))
         else:
             raise TypeError(
                 'list parameter {} in class {} has no subType'.format(
                     name_in_parent, parent_class.__name__))
     if obj_type is set:
         type_info = get_cls_attr(parent_class, PYCKSON_TYPEINFO, dict())
         if name_in_parent in type_info:
             sub_type = type_info[name_in_parent]
             return SetParser(
                 self.get(sub_type, parent_class, name_in_parent))
         else:
             raise TypeError(
                 'set parameter {} in class {} has no subType'.format(
                     name_in_parent, parent_class.__name__))
     if is_list_annotation(obj_type):
         return ListParser(
             self.get(obj_type.__args__[0], parent_class, name_in_parent))
     if is_set_annotation(obj_type):
         return SetParser(
             self.get(obj_type.__args__[0], parent_class, name_in_parent))
     if is_enum_annotation(obj_type):
         options = get_cls_attr(obj_type, PYCKSON_ENUM_OPTIONS, {})
         if options.get(ENUM_CASE_INSENSITIVE, False):
             return CaseInsensitiveEnumParser(obj_type)
         else:
             return DefaultEnumParser(obj_type)
     if is_basic_dict_annotation(obj_type):
         return BasicDictParser()
     if is_typing_dict_annotation(obj_type):
         if obj_type.__args__[0] != str:
             raise TypeError(
                 'typing.Dict key can only be str in class {}'.format(
                     parent_class))
         return TypingDictParser(
             self.get(obj_type.__args__[1], parent_class, name_in_parent))
     if has_cls_attr(obj_type, PYCKSON_PARSER):
         return CustomDeferredParser(obj_type)
     return ClassParser(obj_type, self.model_provider)
Exemple #5
0
def get_class_use_explicit_nulls(cls) -> bool:
    if has_cls_attr(cls, PYCKSON_EXPLICIT_NULLS):
        return get_cls_attr(cls, PYCKSON_EXPLICIT_NULLS)
    else:
        return get_use_explicit_nulls()
Exemple #6
0
def get_class_date_formatter(cls) -> DateFormatter:
    if has_cls_attr(cls, PYCKSON_DATE_FORMATTER):
        return get_cls_attr(cls, PYCKSON_DATE_FORMATTER)
    else:
        return get_date_formatter()
Exemple #7
0
def set_defaults(*args):
    global global_defaults
    for arg in args:
        if not has_cls_attr(arg, PYCKSON_RULE_ATTR):
            raise ValueError('Cannot use {} as a default rule'.format(arg.__name__))
        global_defaults.append(arg)
Exemple #8
0
def apply_defaults(cls):
    global global_defaults
    for default in global_defaults:
        if not has_cls_attr(cls, get_cls_attr(default, PYCKSON_RULE_ATTR)):
            default(cls)