TypeError:“ YANGBaseClass”类型的对象不可JSON序列化

时间:2019-05-12 19:39:19

标签: json pybind11 ietf-netmod-yang

我有一个Yang脚本,我在其中有一个控制器,我想通过UDP数据包发送此yang脚本json转储,但在此之前,我想转储一些json输入,但是使用json转储时出错。由于我不熟悉yang脚本和json转储,因此我无法理解为什么它不起作用。

module wireless_to_orc{

    namespace "https://wireless.com/wireless_to_orc";

    prefix "wireless_to_orc";

    typedef ipv4-address{
          type string{
          pattern
             '(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
           +  '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
           + '(%[\p{N}\p{L}]+)?';
            }
    }
    typedef mac-address {
          type string {
              pattern '[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}';
                }
    }
    typedef throughput{
          type decimal64{
        fraction-digits 2;
                range "1 .. 3.14 | 10 | 20..max";
        }
    }
    typedef Signal{
          type decimal64{
        fraction-digits 2;
                range "1 .. 3.14 | 10 | 20..max";
        }
    }
    grouping client_one{
        leaf mac{
            type mac-address;
            }
        leaf ip{
            type ipv4-address;
            }
        leaf throughput{
            type throughput;
            }
        leaf Signal{
            type Signal;
            }
        }
    container ControllerMac{
        container Aps{
            container ApMac{
                list clients{
                    key "mac";
                    uses client_one;
                    }
                }
            }
    }
}

我正在尝试将json转储用于此模型,但是如上所述,出现此错误。

我尝试如下进行json转储。

import wireless_to_orc
from pyangbind.lib.xpathhelper import YANGPathHelper
from pyangbind.lib.pybindJSON import dumps,loads
from pyangbind.lib.serialise import pybindJSONDecoder
sw= wireless_to_orc.wireless_to_orc()

ob=sw.ControllerMac.Aps.ApMac.clients.add("01:23:45:67:89:ab")
ob.ip="192.168.1.1"

import json
lm=json.dumps(sw.get())
print(lm)

我得到的错误如下,我是使用yang脚本和使用json转储的新手。请让我知道怎么了。

错误:

Traceback (most recent call last):
  File "/home/teja/PycharmProjects/new/pub.py", line 11, in <module>
    lm=json.dumps(sw.get())
  File "/usr/lib/python3.6/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'YANGBaseClass' is not JSON serializable

0 个答案:

没有答案