Wikipedia:Lua/Modul/JSONutil/en
Erscheinungsbild
Vorlagenprogrammierung | Diskussionen | Lua | Test | Unterseiten | |||
Modul | Deutsch | English
|
Modul: | Dokumentation |
JSONutil
– Module with functions for preparation and analysis of JSON code strings.
Line breaks and tabulators within string values will be accepted and converted.
Rather than a global “Invalid” message it will be tried to detect some typical errors and communicate them together with context.
Functions for templates
- failsafe
- Version ID:
2020-11-08
- optional parameter
1
– required version, or keywordwikidata
or keyword~
- Returns:
- empty, if minimal version condition not matched
- local version otherwise, or registered version on Wikidata (
2020-11-08
) when keywordwikidata
- empty, if keyword
~
and a registered version on Wikidata is the same as local; otherwise local version ID ()
Functions for Lua modules (API)
All functions described above can be used by other modules:
local lucky, JSONutil = pcall( require, "Module:JSONutil" )
if type( JSONutil ) == "table" then
JSONutil = JSONutil.JSONutil()
else
-- failure; JSONutil is the error message
return "<span class=\"error\">" .. JSONutil .. "</span>"
end
Subsequently there are available:
- JSONutil.failsafe(atleast)
- Version ID
- atleast
optional
nil or required version or"wikidata"
- atleast
- returns: string or false
- JSONutil.fair(apply)
- Reduce enhanced JSON data to strict JSON and analyse for errors.
- apply
string with JSON code
- apply
- returns: string or false
- string with error keyword, or
false
if no complaints. - string with error context, or clean JSON code if no complaints.
- string with error keyword, or
- JSONutil.fault(alert, add, adapt)
- Retrieve formatted message in best language
- alert
string with error keyword - add
optional
string with error context - adapt
optional – language preferences
- alert
- returns: string with HTML error message.
- JSONutil.fetch(apply, always, adapt)
- Convert JSON robust into Lua
- apply
string with JSON code - always
optional
true
, if always preemptive test for free line format and strict JSON code being performed
Meaningful for migration HHVM→PHP7 2019 to discover problems also in HHVM mode. - adapt
optional – language preferences
- apply
- returns: string or table
- string with HTML error message in best language.
- table with JSON data.
Language preferences
The argument adapt
provides adaption of message language and might be one of these types:
function
– Callback- Return value is best message text
- Two arguments:
- table, with mapping: language code → message text
- string, optional, with message text if table fails
- i18n@Multilingual would work
string
– space-separated list of preferred language codes- false/nil – project language, else English
Dependencies
None.
Usage
Useful for Lua programming only.
Internationalisation
- commons:Data:I18n/Module:JSONutil.tab – Text elements
- Error messages will be shown in user or project language, if available.
See also
- jsonDebug@PerfektesChaos – User JavaScript for detailed analysis of fatal JSON errors