Bug - Fixed Javascript toJson leaves object keys in underscore_format rather than camelCase

soolar

Member
Per the title. I imagine this function isn't widely used (I wouldn't be surprised if Yorick and now tome are the only scripts making use of it), so I don't THINK changing the behavior would cause widespread issues. It seems likely unintentional to me that the keys aren't transformed to the format that js scripts themselves use.

Simple example:
Code:
js toJson(Familiar.get("Hobo in Sheep's Clothing"))
Returned: {"other_action_during_combat":false,"poke_level_3_power":0,"drop_item":"grubby wool","physical_damage":false,"owner_id":2463557,"drops_limit":-1,"delevel":false,"combat":false,"other_action_after_combat":false,"experience":279,"poke_level_3_hp":0,"elemental_damage":false,"block":false,"id":290,"owner":"soolar the second","feasted":false,"poke_level":0,"image":"hobosheep.gif","poke_level_2_power":0,"poke_level_4_power":0,"mp_after_combat":false,"passive":false,"poke_level_2_hp":0,"poke_attribute":"","hp_during_combat":false,"poke_level_4_hp":0,"poke_move_2":"","poke_move_3":"","charges":0,"fights_today":0,"poke_move_1":"","name":"Patsy E. Jackson","variable":false,"drop_name":"grubby wool","underwater":false,"attributes":"","hatchling":"unoccupied sheep suit","mp_during_combat":false,"hp_after_combat":false,"fights_limit":0,"drops_today":4}

Ideally (for my use case at least) this would result in keys in the returned object like otherActionDuringCombat rather than other_action_during_combat.
 

MCroft

Developer
Staff member
Per the title. I imagine this function isn't widely used (I wouldn't be surprised if Yorick and now tome are the only scripts making use of it), so I don't THINK changing the behavior would cause widespread issues. It seems likely unintentional to me that the keys aren't transformed to the format that js scripts themselves use.
Looks like it's in Garbo's outfit code.

JavaScript:
scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia62.toJson)(spec), "!")));

scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia63.toJson)(spec))));

scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia64.toJson)(spec), "!")));

scripts/garbage-collector/garbo.js:        var mergingOutfit = Outfit.from(initialSpec, new Error("Failed to build outfit from ".concat((0, import_kolmafia85.toJson)(initialSpec))));
 

Veracity

Developer
Staff member
So you are calling an ASH function - to_json - and it does what it does - and you are unhappy that the return value isn’t magically transmuted when it is passed up to a Javascript caller.

Is there something special about the string return value such that we should know to look inside it and munge it in a particular way?

Perhaps somebody should look at intercepting to_json and calling a bespoke JS version which knows the caller is JS.
 

Ryo_Sangnoir

Developer
Staff member
Given
Code:
> js toJson({"underscore_thing": "other_thing"})

Returned:    {"underscore_thing":"other_thing"}
I think this is going to be a bit annoying to fix: you want it so that if you pass in a proxy record, the keys are transformed similarly to the way the JavascriptRuntime transforms the keys, but if you pass in a standard aggregate, the keys aren't transformed.
 
Looks like it's in Garbo's outfit code.

JavaScript:
scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia62.toJson)(spec), "!")));

scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia63.toJson)(spec))));

scripts/garbage-collector/garbo.js:  var outfit3 = Outfit.from(spec, new Error("Failed to construct outfit from spec ".concat((0, import_kolmafia64.toJson)(spec), "!")));

scripts/garbage-collector/garbo.js:        var mergingOutfit = Outfit.from(initialSpec, new Error("Failed to build outfit from ".concat((0, import_kolmafia85.toJson)(initialSpec))));
Garbo (and one or two other scripts I maintain that have used toJson) doesn't actually care about this change, because we're never translating mafia constants to json, but rather relying on it to make objects that contain mafia constants legible:
> js toJson({ foo: Item.get("seal-clubbing club") });

Returned: {"foo":"seal-clubbing club"}

> js JSON.stringify({ foo: Item.get("seal-clubbing club") });

Returned: {"foo":{}}
In general, I agree that this is somewhat counterintuitive, but I'm not actually sure of a use case where this would impact anything (other than causing understandable confusion before finding the right answer)
 

soolar

Member
The complexity of not wanting it to modify actual objects with underscores is a very good point. I'll just write something to make it adapt the results where I'm using it. The goal is to have objects that mirror what they would look like in normal kolmafia javascript scripts available in the browser. And I also hadn't considered at the time that I suppose this function might be used for data storage in ash scripts. Consider this request/report/whatever retracted.
 

Veracity

Developer
Staff member
My BeachComber script uses to_json for data storage. It was an ASH function before JS was a Thing in Kolmafia.
 

Veracity

Developer
Staff member
In spite of “Won’t Fix”, I think gausie fixed this.
If you can confirm, I’ll change it to Fixed.
 
> js JSON.stringify(Item.get("seal-clubbing club"))

Returned: {"id":1,"name":"seal-clubbing club","plural":"seal-clubbing clubs","descid":"868780591","image":"club.gif","smallimage":"club.gif","levelreq":0,"quality":"","adventures":"","muscle":"","mysticality":"","moxie":"","fullness":0,"inebriety":0,"spleen":0,"minhp":0,"maxhp":0,"minmp":0,"maxmp":0,"dailyusesleft":2147483647,"notes":"","quest":false,"gift":false,"tradeable":true,"discardable":true,"combat":false,"combatReusable":false,"usable":false,"reusable":false,"multi":false,"fancy":false,"pasteable":true,"smithable":true,"cookable":false,"mixable":false,"candy":false,"candyType":"none","chocolate":false,"potion":false,"seller":"none","buyer":"none","nameLength":18,"noobSkill":"none","tcrsName":"seal-clubbing club","skill":"none","recipe":"none"}
and
> js toJson(Item.get("seal-clubbing club"))

Returned: {"gift":false,"seller":"none","maxhp":0,"cookable":false,"fancy":false,"plural":"seal-clubbing clubs","notes":"","discardable":true,"minmp":0,"descid":"868780591","candy":false,"noob_skill":"none","moxie":"","fullness":0,"recipe":"none","combat":false,"mixable":false,"name_length":18,"inebriety":0,"quest":false,"multi":false,"usable":false,"muscle":"","skill":"none","mysticality":"","tradeable":true,"id":1,"adventures":"","image":"club.gif","potion":false,"candy_type":"none","spleen":0,"levelreq":0,"maxmp":0,"minhp":0,"smallimage":"club.gif","quality":"","reusable":false,"buyer":"none","tcrs_name":"seal-clubbing club","pasteable":true,"name":"seal-clubbing club","smithable":true,"dailyusesleft":2147483647,"combat_reusable":false,"chocolate":false}
Interesting that the keys are in a different order, but looks more or less correct
 

fronobulax

Developer
Staff member
In spite of “Won’t Fix”, I think gausie fixed this.
If you can confirm, I’ll change it to Fixed.
I change it because I (mis?) understood

Garbo (and one or two other scripts I maintain that have used toJson) doesn't actually care about this change, because we're never translating mafia constants to json, but rather relying on it to make objects that contain mafia constants legible:

In general, I agree that this is somewhat counterintuitive, but I'm not actually sure of a use case where this would impact anything (other than causing understandable confusion before finding the right answer)

I was wrong. I could argue this was a Feature request and not a bug but regardless I will change the status.
 
Top