cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1211
Views
10
Helpful
3
Replies

Leaf-list with duplicate elements

tqhuy
Level 1
Level 1

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
Spotlight
Spotlight

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

3 Replies 3

yangorelik
Spotlight
Spotlight

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

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

1. Yes, it is normal, because they use different underlying third party software. The JsonSubtreeCodec uses common use software json.hpp; the CodecService - libyang. The last does verification for the data, making sure its compliant with YANG model. That is why CodecService fails in your case. In addition, the decimal64 value "6.4e2" is wrong and causes YModelError exception. Here is definition of 'decimal64' from RFC-7950:

A decimal64 value is lexically represented as an optional sign ("+" or "-"), followed by a sequence of decimal digits, optionally followed by a period ('.') as a decimal indicator and a sequence of decimal digits.

2. Correct. The location in YDK git repository is ydk-gen/sdk/cpp/core/src/json_subtree_codec.cpp.

3. It should work. The only issue is that you create root schema under CodecServiceProvider, but use it in JsonSubtreeCodec. I would do it slightly different by initializing root schema under Repository.

from ydk.entity_utils import JsonSubtreeCodec
from ydk.models import bundle as my_bundle

PAYLOAD = '''{
"key1": {
"key2": [0.56,11.2,8.3,6.4,0.56,27.8]
}
}'''

repo = Repository(my_bundle.__path__[0]+'/_yang')
root_schema = repo.create_root_schema([])
jcodec = JsonSubtreeCodec()
entity = jcodec.decode(PAYLOAD, my_bundle.TestEntity())
json = jcodec.encode(entity, root_schema, True) 

 

Yan Gorelik
YDK Solutions
Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: