Anyone knows this use of json.get?

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Nildik
Cave Troll
Posts: 76
Joined: Mon Oct 31, 2011 7:32 pm

Anyone knows this use of json.get?

Post by Nildik »

I'm surprised with the result of this use of json.get:

[h: json1 = json.set("","shield",4,"sword",2,"bastard sword",1)]
[h: json1 = replace(json1,"bastard ","")]
[r: json.get(json1,"sword")]

The result is:
[2,1]

A json object can have 2 elements with the same key!?
And if you get the value of this key, you get a json array with all of the values of this key in the json object!!

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: Anyone knows this use of json.get?

Post by CoveredInFish »

Using replace() you have manipulated the JSON as a string - you are "breaking the rules".

The json-functions shouldnt let you assing a key multiple times - it will replace the value instead. But it seems like all values are preserved when you read a json from a string.


User avatar
Bone White
Great Wyrm
Posts: 1124
Joined: Tue Aug 23, 2011 11:41 am
Location: Cornwall, UK

Re: Anyone knows this use of json.get?

Post by Bone White »

It just looks like a failsafe on the hands on the developers of MapTool, so you don't have a crash it returns all values for keys of the string "Sword".

Seems intentional to me, but I'd probably have coded in an error instead.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Anyone knows this use of json.get?

Post by aliasmask »

If I coded it, I'd just return the first value it comes across and delete the other.

Nildik
Cave Troll
Posts: 76
Joined: Mon Oct 31, 2011 7:32 pm

Re: Anyone knows this use of json.get?

Post by Nildik »

I notice that ramdomly.
I use json objects to store effects in my framework, something like that:
ACmods: {"armor":4 , "dodge": 1 , "deflection": 2}
When a new effect is aplied to the token, I merge the json objects with a user function to store all bonuses
{"armor":4 , "dodge": 1 , "deflection": 2} + {"deflection" : 1} = {"armor":4 , "dodge": 1 , "deflection": "[2,1]"}
I made this for purposes of stacking or not and to keep stored all bonus for when the effect has finish.
With this "new function" I get a way to do it easily.

Post Reply

Return to “Macros”