Python鞋面插件JSON编码

2024-04-25 22:39:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中使用了一个VAMP plugin

import vamp
import librosa
data, rate = librosa.load("example.mp3")
chroma = vamp.collect(data, rate, "nnls-chroma:nnls-chroma")

我必须在json对象中解码结果。因为Vamp插件使用一个特定的对象vampy.RealTime对象来包装数字,所以我为此写了一个Encoder,否则您将得到一个TypeError: (integer) is not JSON serializing-请参见此处了解此特定错误:"TypeError: (Integer) is not JSON serializable" when serializing JSON in Python?

出于这个原因,我编写了一个自定义JSON编码器,它使用从vampy.RealTime对象到数字的转换,该对象取自here

import json

class VampJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, vamp.vampyhost.RealTime):
            r = vamp.vampyhost.RealTime('seconds', obj)
             return r
        return super(VampJSONEncoder, self).default(obj)

但当我像这样运行它时

json.dumps(chroma, sort_keys=True, indent=4, cls=VampJSONEncoder)

我得到一份工作

RuntimeError: maximum recursion depth exceeded while calling a Python object

Tags: 对象importjsonobjdatarate数字realtime