import base64


def fix_string_attr(tfjs_node):
    """
    Older tfjs models store strings as lists of ints (representing byte values). This function finds and replaces
    those strings, so protobuf can correctly decode the json.
    """
    def fix(v):
        if isinstance(v, list):
            return base64.encodebytes(bytes(v)).decode()
        return v
    if 'attr' not in tfjs_node:
        return
    for v in tfjs_node['attr'].values():
        if 's' in v:
            v['s'] = fix(v['s'])
        if 'list' in v and 's' in v['list']:
            for i, x in enumerate(v['list']['s']):
                v['list']['s'][i] = fix(x)