web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to differentiate a packed map from a packed list?

(0) ShareShare
ReportReport
Posted on by 805
Hi,

Is there any documentation about the container structure which is created when using map.pack() or list.pack() methods to pack corresponding objects to containers?

I am using RetailCommonWebAPI class, getMapFromJsonString method which "desterilize" Json to Map of containers (packed maps or lists).

I would like to do the following:
1. container con = map.lookup("somekey");
2. Depends on the packed object (list or map) to create a list or map using create static methods on the corresponding classes (List::create(con) or Map::create(con).

That's why I need to be able to differentiate the types of the packed objects.
Is that possible?
Regards,
I have the same question (0)
  • Evaldas Profile Picture
    1,800 on at

    The only difference I noticed is that the number of extra values at the beginning in it which in List is 3, and in Map is 4. It starts with a value 3 (could be version) on both, then follows type values and the number of elements.

    Quite vague structure to identify one or another.

    Instead, I would try to create a Map first, if that fails, then try to create a List.

  • Suggested answer
    Pedro Tornich Profile Picture
    955 on at

    Try to create one of them, if the object is not created, try the other one.

    That would be the easier path.

  • Evaldas Profile Picture
    1,800 on at

    That what I suggested.

  • ist Profile Picture
    805 on at

    I tried this approach. It is applicable only if try catch block is not inside a transaction block, because i.e. if we try to create a map from container that has been created from packed list, it will throw an exception that need to be handled in catch block before we try to create a list. As we know handling an exception in transaction is not possible.

  • Evaldas Profile Picture
    1,800 on at

    That's true. Do you have control of what's in the JSON file?

  • Evaldas Profile Picture
    1,800 on at

    Or you can always try to guess the structure. E.g. if the 3rd element value + 3 = conlen(container) then its the list. Or something like that.

  • Verified answer
    Martin Dráb Profile Picture
    237,801 Most Valuable Professional on at

    My recommendation is to stop using the retail function, because it's useful only a few specialized cases. It's not a general-purpose deserialization method and you seemed to run into one of many of its limitations.

    Deserialization should give you objects, which isn't happen in your case - you just get data serialized in a different way and you loose important information on the way. It just makes the problem worse.

    I would do the following:

    1. Create a C# project in the same solution as your X++ project.
    2. Create/open a C# class.
    3. Take a sample JSON string, put it into the clipboard and use Edit > Paste special > Paste JSON as classes.
      This will create all classes necessary to deserialize the JSON string. If the JSON contains a hierarchy of different objects, you need multiple classes to be able to deserialize this string to an object graph. That's where things fail with getMapFromJsonString(), because it's trying to deserialize a hierarchy to a map that can hold only a single level.
    4. Use Newtonsoft.Json library to do the deserialization.
    5. Wrap the code in a public method that you can easily call from X++.
    6. Build the C# project, add a project reference to the X++ project and call your method from X++.
    7. Use the deserialized objects in your code.
      Notice how easier and safer it is in comparison with the original solution, because you're now working with named classes and properties and you get code completion in code editor and name and type checking at compilation.
  • ist Profile Picture
    805 on at

    Hi Martin,

    Thanks for your recommendations and guidelines! I have read other articles written by you about this topic, so I am absolutely agree with you, that this is the right way.

    Lets forget about using the retail function and focus on the question. Is there a way to differentiate packed list from packed map?

    The only way I found is by the second element (0 for packed map and 7 for packed list) of the containers (based on their leading elements):

    Map:

    [0] 3

    [1] 0

    [2] 7

    [3] x (count)

    ...

    List:

    [0] 3

    [1] 7

    [2] x (count)

    …

    Do you think we can trust on that?

    Do you know if it's documented somewhere?

    Regards,

  • Evaldas Profile Picture
    1,800 on at

    Regarding packed map and list. The second parameter in the packed list defines the type of elements in the list.

    The second parameter in the packed map is the type of the map key. The 3rd is the type of the map value.

    You can use it if you know for sure that the types will not change.

  • ist Profile Picture
    805 on at

    Hi Evaldas,

    You mentioned that the 3rd is the type of the map value. Is there any official documentation for that?

    What does 7 mean in context of the map value?

    Regards,

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans