Page MenuHomePhabricator

Provide a loadJsonData with same characteristics as loadData
Closed, ResolvedPublic

Description

This is a followon task to some of the tangential commentary in T144475: Make it possible to create json pages in the module namespace, specifically T144475#4894433 and T144475#4898734. The objective is for both modules and gadgets to share the same data without modules needing to suffer the load-penalty of mw.text.jsonDecode alone.

Maybe this task is trivial per Lego's comment and can be done in calling code already. I don't understand the infrastructure enough to know whether it is or isn't.

Event Timeline

Maybe this task is trivial per Lego's comment and can be done in calling code already. I don't understand the infrastructure enough to know whether it is or isn't.

The workaround suggested was, if you have "FooBarData.json", create "Module:FooBarData" as

return mw.text.jsonDecode( mw.title.new( 'FooBarData.json' ):getContent() )

Then load it from your code with mw.loadData( 'Module:FooBarData' ).

Does that load the json each time you invoke the module on a page though?

mw.loadData() will work with that in exactly the same way it would work if "Module:FooBarData" contained something like

return {
    foo = "bar",
    bar = { 1, 2, 3, 4, 5 }
}

(or whatever Lua code matches your JSON).

Izno triaged this task as Lowest priority.Mar 4 2019, 4:43 PM

Okay, I've triaged as lowest. No objection if you want to decline also.

Change 833990 had a related patch set uploaded (by Legoktm; author: Legoktm):

[mediawiki/extensions/Scribunto@master] Add mw.loadJsonData()

https://gerrit.wikimedia.org/r/833990

FWIW, I spent a while playing with integrating JSON support into the existing mw.loadData() (because then I wouldn't have to duplicate all the test cases), but in the end I decided to split them as proposed in this task just because I felt the semantics would be different enough (JSON files can be in any namespace, Lua tables have to be in "Module") that having separate functions would be cleaner.

This function, if implemented, would definitely be cleaner than using the workaround of a separate data-loader module.

Worth noting is that Module:Class, used on ~17% of enwiki, uses this workaround to load Module:Class/definition.json (the loader module is Module:Class/configuration, which also stores some default data in Lua form).

Change 833990 merged by jenkins-bot:

[mediawiki/extensions/Scribunto@master] Add mw.loadJsonData()

https://gerrit.wikimedia.org/r/833990

Change 853713 had a related patch set uploaded (by Brian Wolff; author: Legoktm):

[mediawiki/extensions/Scribunto@REL1_39] Add mw.loadJsonData()

https://gerrit.wikimedia.org/r/853713

Change 853713 merged by jenkins-bot:

[mediawiki/extensions/Scribunto@REL1_39] Add mw.loadJsonData()

https://gerrit.wikimedia.org/r/853713