cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
201
Views
10
Helpful
2
Replies
tqhuy
Beginner

Leaf-list with duplicate elements

Hello,

 

I'm experiencing an issue when trying to decode JSON payload to Python entity. 

 

For example, I have a JSON like this:

{
"key1": {
"key2": [0.56,11.2,8.3,6.4e2,0.56,27.8]
}
}

And the model like this:

 
container key1 {
  leaf-list key2 {
    type decimal64 {
      fraction-digits 5;
    }
}}

When I decode this JSON string, I got an error:

"ydk.errors.YModelError: Duplicate leaf-list item detected"

 

This is expected because the values in a leaf-list must be unique.

 

However, I would like to model an array of decimal64 values which may not be unique.

 

Do you have any idea how to solve this problem? Thank you very much for your help.

 

Cheers,

Huy

1 ACCEPTED SOLUTION

Accepted Solutions
yangorelik
Participant

Hello tqhuy

The ability for the leaf-list to have duplicate values is a YANG-1.1 feature. This feature support will be available in new YDK release 0.9.0.1, which is coming at the end of this month. Please read this announcement for details.

If you are trying only decode the JSON payload to some model object, then I would suggest to use JsonSubtreeCodec. It does not do the data verification and should allow duplicate values in the leaf-list.

Yan Gorelik
YDK Solutions

View solution in original post

2 REPLIES 2
yangorelik
Participant

Hello tqhuy

The ability for the leaf-list to have duplicate values is a YANG-1.1 feature. This feature support will be available in new YDK release 0.9.0.1, which is coming at the end of this month. Please read this announcement for details.

If you are trying only decode the JSON payload to some model object, then I would suggest to use JsonSubtreeCodec. It does not do the data verification and should allow duplicate values in the leaf-list.

Yan Gorelik
YDK Solutions

View solution in original post

Hi Yan,

 

Thank you for your reply.

 

Your suggestion helped me to deal with the issue. I leave the code snippet here in case someone may need it.

from ydk.entity_utils import JsonSubtreeCodec
from ydk.models.<bundle>.<module> import TestEntity

PAYLOAD = """{
"key1": {
"key2": [0.56,11.2,8.3,6.4e2,0.56,27.8]
}
}""" jcodec = JsonSubtreeCodec() entity = jcodec.decode(PAYLOAD,TestEntity())

 

However, if I tried to encode back the entity to JSON using the CodecService and CodecServiceProvider

from ydk.providers import CodecServiceProvider
from ydk.services import CodecService

codec = CodecService()
codec_provider = CodecServiceProvider()
payload = codec.encode(codec_provider,entity) # the entity instance in the snippet above

 

I got an error on the value type. 

ydk.errors.YModelError:  Invalid value "6.4e2" ...

 

If I use the JsonSubtreeCodec, it works fine with unique-element Json array

codec_provider = CodecServiceProvider()
bundle_name = _get_bundle_name(entity) yang_path = _get_yang_path(entity) codec_provider.initialize(bundle_name,yang_path) repo = Repository(yang_path) codec_provider._initialize_root_schema(bundle_name, repo) jpayload = jcodec.encode(entity,codec_provider.get_root_schema(bundle_name),True) print(jpayload)

 

Thus, I have some questions on these issues:

 

1/ Is it normal to be able to encode (using JsonSubtreeCodec) the entity (which is decoded from JSON payload by JsonSubtreeCodec), but not able to do so with CodecService.encode?

 

2/ Where should I find the source codes of the JsonSubtreeCodec class and its methods, since I couldn't find them in the installed ydk package directory? => I figured out the answer myself: the ydk Python wrapper invokes cpp/core/src/json_subtree_codec.cpp.

 

3/ Is it the correct way to implement theJsonSubtreeCodec.decode in the last snippet, according to you?

 

Once again, thank you very much for your help. I'm also looking forward to the new YDK.

 

Cheers,

Huy

Content for Community-Ad