Professions -> Weapons not an array? — Guild Wars 2 Forums

ArenaNet Forum Chat for May: This month’s forum conversation about the original Guilds Wars is open now. Member posts will be accepted through Friday afternoon Pacific Time (5/25) and devs will be answering questions and sharing insights through June 15th in the AFC subforum.

Professions -> Weapons not an array?

blobfish.6712blobfish.6712 Member
edited May 10, 2018 in API Development

Hello,

Looking at the api documentation here: https://wiki.guildwars2.com/wiki/API:2/professions it says that "weapons" is supposed to be an array ("weapons (array of objects)"). If i look at an actual request, for example: https://api.guildwars2.com/v2/professions/Engineer the weapon array is sent like this: "weapons": {...} when it should be "weapons":[{...}, {...}, ...] or am I misunderstanding something?

Comments

  • The wiki is wrong. It's an object with the possible weapons as keys.

  • I've fixed it. Thanks for noticing!

  • Would it not be better to just put the weapons in an array instead though, how come ArenaNet chose this approach instead?

  • SlippyCheeze.5483SlippyCheeze.5483 Member ✭✭✭
    edited May 11, 2018

    @blobfish.6712 said:
    Would it not be better to just put the weapons in an array instead though, how come ArenaNet chose this approach instead?

    Probably not. Enumerating the direct properties of a JSON Object is trivial, and it binds the name and value in a way that is popular. (I mean, personally, I strongly prefer fully internal data, so I agree with you, but ... this is a popular choice, and it does work.)

    If you prefer an array of objects, simply use some variant of the code:

    var result = new Array()
    for (var key in <weapons>) {
        var weapon = <weapons>[key]
        weapon.name = key
        result.push(weapon)
    }
    // et viola
    
  • blobfish.6712blobfish.6712 Member
    edited May 12, 2018

    @SlippyCheeze.5483 said:
    Probably not. Enumerating the direct properties of a JSON Object is trivial, and it binds the name and value in a way that is popular. (I mean, personally, I strongly prefer fully internal data, so I agree with you, but ... this is a popular choice, and it does work.)

    If you prefer an array of objects, simply use some variant of the code:

    var result = new Array()
    for (var key in <weapons>) {
        var weapon = <weapons>[key]
        weapon.name = key
        result.push(weapon)
    }
    // et viola
    

    The problem this approach brings to me is that the library i use (newtonsoft.json) can't de-serialize the weapons into a dictionary of type <string, Weapon>(at least i couldn't get it to). This means i have to make a big ugly class containing one JsonProperty per weapon and then i have to check which ones has been initialized. This will also be a problem for me when they add new weapons and i then have to add those to the "Weapons" class.

©2010–2018 ArenaNet, LLC. All rights reserved. Guild Wars, Guild Wars 2, Heart of Thorns, Guild Wars 2: Path of Fire, ArenaNet, NCSOFT, the Interlocking NC Logo, and all associated logos and designs are trademarks or registered trademarks of NCSOFT Corporation. All other trademarks are the property of their respective owners.