<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Leaf-list with duplicate elements in Tools</title>
    <link>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387091#M1322</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm experiencing an issue when trying to decode JSON payload to Python entity.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have a JSON like this:&lt;/P&gt;&lt;PRE&gt;{&lt;BR /&gt;"key1": {&lt;BR /&gt;  "key2": [0.56,11.2,8.3,6.4e2,0.56,27.8]&lt;BR /&gt;}&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;And the model like this:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;container&amp;nbsp;key1 {
&amp;nbsp; leaf-list&amp;nbsp;key2 {
&amp;nbsp; &amp;nbsp; type decimal64 {
&amp;nbsp; &amp;nbsp; &amp;nbsp; fraction-digits 5;
&amp;nbsp; &amp;nbsp; }
}}&lt;/PRE&gt;&lt;P&gt;When I decode this JSON string, I got an error:&lt;/P&gt;&lt;PRE&gt;"ydk.errors.YModelError: Duplicate leaf-list item detected"&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is expected because the values in a leaf-list must be unique.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I would like to model an array of decimal64 values which may not be unique.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any idea how to solve this problem? Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Huy&lt;/P&gt;</description>
    <pubDate>Wed, 14 Apr 2021 21:03:25 GMT</pubDate>
    <dc:creator>tqhuy</dc:creator>
    <dc:date>2021-04-14T21:03:25Z</dc:date>
    <item>
      <title>Leaf-list with duplicate elements</title>
      <link>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387091#M1322</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm experiencing an issue when trying to decode JSON payload to Python entity.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, I have a JSON like this:&lt;/P&gt;&lt;PRE&gt;{&lt;BR /&gt;"key1": {&lt;BR /&gt;  "key2": [0.56,11.2,8.3,6.4e2,0.56,27.8]&lt;BR /&gt;}&lt;BR /&gt;}&lt;/PRE&gt;&lt;P&gt;And the model like this:&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;container&amp;nbsp;key1 {
&amp;nbsp; leaf-list&amp;nbsp;key2 {
&amp;nbsp; &amp;nbsp; type decimal64 {
&amp;nbsp; &amp;nbsp; &amp;nbsp; fraction-digits 5;
&amp;nbsp; &amp;nbsp; }
}}&lt;/PRE&gt;&lt;P&gt;When I decode this JSON string, I got an error:&lt;/P&gt;&lt;PRE&gt;"ydk.errors.YModelError: Duplicate leaf-list item detected"&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is expected because the values in a leaf-list must be unique.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I would like to model an array of decimal64 values which may not be unique.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any idea how to solve this problem? Thank you very much for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Huy&lt;/P&gt;</description>
      <pubDate>Wed, 14 Apr 2021 21:03:25 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387091#M1322</guid>
      <dc:creator>tqhuy</dc:creator>
      <dc:date>2021-04-14T21:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: Leaf-list with duplicate elements</title>
      <link>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387200#M1323</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;A href="https://community.cisco.com/t5/user/viewprofilepage/user-id/1184623" target="_blank"&gt;tqhuy&lt;/A&gt;&lt;/P&gt;&lt;P&gt;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 &lt;A href="https://community.cisco.com/t5/yang-tools/heads-up-new-ydk-release-with-yang1-1-support-is-coming-soon/td-p/4306512" target="_self"&gt;this&lt;/A&gt; announcement for details.&lt;/P&gt;&lt;P&gt;If you are trying &lt;STRONG&gt;only&lt;/STRONG&gt; decode the JSON payload to some model object, then I would suggest to use &lt;EM&gt;JsonSubtreeCodec&lt;/EM&gt;. It does not do the data verification and should allow duplicate values in the leaf-list.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 01:50:50 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387200#M1323</guid>
      <dc:creator>yangorelik</dc:creator>
      <dc:date>2021-04-15T01:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: Leaf-list with duplicate elements</title>
      <link>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387520#M1324</link>
      <description>&lt;P&gt;Hi Yan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your suggestion helped me to deal with the issue. I leave the code snippet here in case someone may need it.&lt;/P&gt;&lt;PRE&gt;from ydk.entity_utils import JsonSubtreeCodec
from ydk.models.&amp;lt;bundle&amp;gt;.&amp;lt;module&amp;gt; import TestEntity

PAYLOAD = """{&lt;BR /&gt;"key1": {&lt;BR /&gt;"key2": [0.56,11.2,8.3,6.4e2,0.56,27.8]&lt;BR /&gt;}&lt;BR /&gt;}"""

jcodec = JsonSubtreeCodec()
entity = jcodec.decode(PAYLOAD,TestEntity())&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if I tried to encode back the entity to JSON using the CodecService and CodecServiceProvider&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;ydk.providers &lt;SPAN&gt;import &lt;/SPAN&gt;CodecServiceProvider&lt;BR /&gt;&lt;SPAN&gt;from &lt;/SPAN&gt;ydk.services &lt;SPAN&gt;import &lt;/SPAN&gt;CodecService&lt;BR /&gt;&lt;BR /&gt;codec = CodecService()&lt;BR /&gt;codec_provider = CodecServiceProvider()&lt;BR /&gt;payload = codec.encode(codec_provider,entity) # the entity instance in the snippet above&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got an error on the value type.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;ydk.errors.YModelError:  Invalid value "6.4e2" ...&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I use the&amp;nbsp;JsonSubtreeCodec, it works fine with unique-element Json array&lt;/P&gt;&lt;PRE&gt;codec_provider = CodecServiceProvider()&lt;BR /&gt;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)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thus, I have some questions on these issues:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;CodecService.encode?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2/ Where should I find the source codes of the&amp;nbsp;JsonSubtreeCodec class and its methods, since I couldn't find them in the installed ydk package directory? =&amp;gt; I figured out the answer myself: the ydk Python wrapper invokes&amp;nbsp;cpp/core/src/json_subtree_codec.cpp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3/ Is it the correct way to implement theJsonSubtreeCodec.decode in the last snippet, according to you?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, thank you very much for your help. I'm also looking forward to the new YDK.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Huy&lt;/P&gt;</description>
      <pubDate>Thu, 15 Apr 2021 18:06:02 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4387520#M1324</guid>
      <dc:creator>tqhuy</dc:creator>
      <dc:date>2021-04-15T18:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Leaf-list with duplicate elements</title>
      <link>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4447372#M1325</link>
      <description>&lt;P&gt;1. Yes, it is normal, because they use different underlying third party software. The &lt;EM&gt;JsonSubtreeCodec&lt;/EM&gt; uses common use software&amp;nbsp;&lt;EM&gt;json.hpp&lt;/EM&gt;; 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:&lt;/P&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;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.&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;2. Correct. The location in YDK git repository is &lt;EM&gt;ydk-gen/sdk/cpp/core/src/json_subtree_codec.cpp.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;3. It should work. The only issue is that you create root schema under &lt;EM&gt;CodecServiceProvider&lt;/EM&gt;, but use it in &lt;EM&gt;JsonSubtreeCodec&lt;/EM&gt;. I would do it slightly different by initializing root schema under Repository.&lt;/P&gt;&lt;PRE&gt;from ydk.entity_utils import JsonSubtreeCodec
from ydk.models import bundle as my_bundle&lt;BR /&gt;&lt;BR /&gt;PAYLOAD = '''{&lt;BR /&gt;"key1": {&lt;BR /&gt;"key2": [0.56,11.2,8.3,6.4,0.56,27.8]&lt;BR /&gt;}&lt;BR /&gt;}'''&lt;BR /&gt;&lt;BR /&gt;repo = Repository(my_bundle.&lt;SPAN&gt;__path__&lt;/SPAN&gt;[&lt;SPAN&gt;0&lt;/SPAN&gt;]+&lt;SPAN&gt;'/_yang'&lt;/SPAN&gt;)&lt;BR /&gt;root_schema = repo.create_root_schema([])&lt;BR /&gt;jcodec = JsonSubtreeCodec() &lt;BR /&gt;entity = jcodec.decode(PAYLOAD, my_bundle.TestEntity())&lt;BR /&gt;json = jcodec.encode(entity, root_schema, True)&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Aug 2021 20:49:57 GMT</pubDate>
      <guid>https://community.cisco.com/t5/tools/leaf-list-with-duplicate-elements/m-p/4447372#M1325</guid>
      <dc:creator>yangorelik</dc:creator>
      <dc:date>2021-08-10T20:49:57Z</dc:date>
    </item>
  </channel>
</rss>

