Zum Inhalt springen

Wikipedia:Lua/Modul/JSONutil/en

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 18. Juni 2019 um 17:42 Uhr durch PerfektesChaos (Diskussion | Beiträge) (k). Sie kann sich erheblich von der aktuellen Version unterscheiden.
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 keyword wikidata
Returns:
  • (empty), if minimal version condition not matched
  • local version otherwise, or registered on Wikidata (2020-11-08) when wikidata

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"
returns: string or false
JSONutil.fair(apply)
Reduce enhanced JSON data to strict JSON and analyse for errors.
  • apply
    string with JSON code
returns: string or false
  1. string with error keyword, or false if no complaints.
  2. string with error context, or clean JSON code if no complaints.
JSONutil.fault(alert, add, adapt)
Retrieve formatted message in best language
  • alert
    string with error keyword
  • add
    optional
    string with error context
  • adapt
    optionallanguage preferences
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
    optionallanguage preferences
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:
      1. table, with mapping: language code → message text
      2. 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

See also