Repository: yt-dlp/yt-dlp
File: yt_dlp/YoutubeDL.py (Python)

Code before:

        def create_key(outer_mobj):
            if not outer_mobj.group('has_key'):
                return outer_mobj.group(0)
            key = outer_mobj.group('key')
            mobj = re.match(INTERNAL_FORMAT_RE, key)
            initial_field = mobj.group('fields').split('.')[-1] if mobj else ''
            value, default = None, na
            while mobj:
                mobj = mobj.groupdict()
                default = mobj['default'] if mobj['default'] is not None else default
                value = get_value(mobj)
                if value is None and mobj['alternate']:
                    mobj = re.match(INTERNAL_FORMAT_RE, mobj['alternate'][1:])
                else:
                    break

            fmt = outer_mobj.group('format')
            if fmt == 's' and value is not None and key in field_size_compat_map.keys():
                fmt = '0{:d}d'.format(field_size_compat_map[key])

            value = default if value is None else value

            flags = outer_mobj.group('conversion') or ''
            str_fmt = f'{fmt[:-1]}s'
            if fmt[-1] == 'l':  # list
                delim = '\n' if '#' in flags else ', '
                value, fmt = delim.join(variadic(value)), str_fmt
            elif fmt[-1] == 'j':  # json
                value, fmt = json.dumps(value, default=_dumpjson_default, indent=4 if '#' in flags else None), str_fmt
            elif fmt[-1] == 'q':  # quoted
                value = map(str, variadic(value) if '#' in flags else [value])
                value, fmt = ' '.join(map(compat_shlex_quote, value)), str_fmt
            elif fmt[-1] == 'B':  # bytes
                value = f'%{str_fmt}'.encode('utf-8') % str(value).encode('utf-8')
                value, fmt = value.decode('utf-8', 'ignore'), 's'
            elif fmt[-1] == 'U':  # unicode normalized
                value, fmt = unicodedata.normalize(
                    # "+" = compatibility equivalence, "#" = NFD
                    'NF%s%s' % ('K' if '+' in flags else '', 'D' if '#' in flags else 'C'),
                    value), str_fmt
            elif fmt[-1] == 'c':
                if value:
                    value = str(value)[0]
                else:
                    fmt = str_fmt
            elif fmt[-1] not in 'rs':  # numeric
                value = float_or_none(value)
                if value is None:
                    value, fmt = default, 's'


Review comment (suggestion):
```suggestion
            value = default if value is None else value if replacement is None else replacement
```

Code after:
            raise TypeError(f'Object of type {type(obj).__name__} is not JSON serializable')

        def create_key(outer_mobj):
            if not outer_mobj.group('has_key'):
                return outer_mobj.group(0)
            key = outer_mobj.group('key')
            mobj = re.match(INTERNAL_FORMAT_RE, key)
            initial_field = mobj.group('fields').split('.')[-1] if mobj else ''
            value, replacement, default = None, None, na
            while mobj:
                mobj = mobj.groupdict()
                default = mobj['default'] if mobj['default'] is not None else default
                value = get_value(mobj)
                replacement = mobj['replacement']
                if value is None and mobj['alternate']:
                    mobj = re.match(INTERNAL_FORMAT_RE, mobj['alternate'][1:])
                else:
                    break

            fmt = outer_mobj.group('format')
            if fmt == 's' and value is not None and key in field_size_compat_map.keys():
                fmt = '0{:d}d'.format(field_size_compat_map[key])

            value = default if value is None else value if replacement is None else replacement

            flags = outer_mobj.group('conversion') or ''
            str_fmt = f'{fmt[:-1]}s'
            if fmt[-1] == 'l':  # list
                delim = '\n' if '#' in flags else ', '
                value, fmt = delim.join(variadic(value)), str_fmt
            elif fmt[-1] == 'j':  # json
                value, fmt = json.dumps(value, default=_dumpjson_default, indent=4 if '#' in flags else None), str_fmt
            elif fmt[-1] == 'q':  # quoted
                value = map(str, variadic(value) if '#' in flags else [value])
                value, fmt = ' '.join(map(compat_shlex_quote, value)), str_fmt
            elif fmt[-1] == 'B':  # bytes
                value = f'%{str_fmt}'.encode('utf-8') % str(value).encode('utf-8')
                value, fmt = value.decode('utf-8', 'ignore'), 's'
            elif fmt[-1] == 'U':  # unicode normalized
                value, fmt = unicodedata.normalize(
                    # "+" = compatibility equivalence, "#" = NFD
                    'NF%s%s' % ('K' if '+' in flags else '', 'D' if '#' in flags else 'C'),
                    value), str_fmt
            elif fmt[-1] == 'c':
                if value:
                    value = str(value)[0]
                else:
                    fmt = str_fmt
            elif fmt[-1] not in 'rs':  # numeric
                value = float_or_none(value)
                if value is None: