Module:Json parameter test: Difference between revisions

From Visual Data Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 6: Line 6:


     -- Debug: Gib die empfangenen JSON-Daten aus
     -- Debug: Gib die empfangenen JSON-Daten aus
     local debugInfo = "Received JSON Data: " .. tostring(jsonData) .. "\n"
     local debugInfo = "'''Received JSON Data: '''" .. tostring(jsonData) .. "\n"


     -- Überprüfe, ob jsonData gültig ist
     -- Überprüfe, ob jsonData gültig ist
     if not jsonData or jsonData == '' then
     if not jsonData or jsonData == '' then
         return "Keine JSON-Daten empfangen."
         return "'''Keine JSON-Daten empfangen.'''"
     end
     end


Line 17: Line 17:


     -- Debug: Gib die getrimmten JSON-Daten aus
     -- Debug: Gib die getrimmten JSON-Daten aus
     debugInfo = debugInfo .. "Trimmed JSON Data: " .. tostring(jsonData) .. "\n"
     debugInfo = debugInfo .. "'''Trimmed JSON Data: '''" .. tostring(jsonData) .. "\n"


     -- Debug: Gib die Länge der JSON-Daten aus
     -- Debug: Gib die Länge der JSON-Daten aus
     debugInfo = debugInfo .. "Length of JSON Data: " .. tostring(#jsonData) .. "\n"
     debugInfo = debugInfo .. "'''Length of JSON Data: '''" .. tostring(#jsonData) .. "\n"


     -- Debug: Gib die rohen JSON-Daten aus, um zu überprüfen, ob sie korrekt sind
     -- Debug: Gib die rohen JSON-Daten aus, um zu überprüfen, ob sie korrekt sind
     debugInfo = debugInfo .. "Raw JSON Data: " .. jsonData .. "\n"
     debugInfo = debugInfo .. "'''Raw JSON Data: '''" .. jsonData .. "\n"


     -- Ersetze nicht druckbare Zeichen durch sichtbare Platzhalter für Debug-Zwecke
     -- Ersetze nicht druckbare Zeichen durch sichtbare Platzhalter für Debug-Zwecke
     local jsonDataVisible = jsonData:gsub("%c", function(c) return string.format("\\x%02X", string.byte(c)) end)
     local jsonDataVisible = jsonData:gsub("%c", function(c) return string.format("\\x%02X", string.byte(c)) end)
     debugInfo = debugInfo .. "Visible JSON Data: " .. jsonDataVisible .. "\n"
     debugInfo = debugInfo .. "'''Visible JSON Data: '''" .. jsonDataVisible .. "\n"


     -- Überprüfe auf bekannte Platzhalter und entferne diese
     -- Überprüfe auf bekannte Platzhalter und entferne diese
Line 35: Line 35:
     local success, json = pcall(mw.text.jsonDecode, jsonData)
     local success, json = pcall(mw.text.jsonDecode, jsonData)
     if not success then
     if not success then
         return debugInfo .. "Fehler beim Dekodieren der JSON-Daten: " .. tostring(json)
         return debugInfo .. "'''Fehler beim Dekodieren der JSON-Daten: '''" .. tostring(json)
     end
     end


     debugInfo = debugInfo .. "Decoded JSON Data: " .. mw.dumpObject(json) .. "\n"
     debugInfo = debugInfo .. "'''Decoded JSON Data: '''" .. mw.dumpObject(json) .. "\n"


     local result = {}
     local result = {}

Revision as of 08:32, 27 May 2024

Documentation for this module may be created at Module:Json parameter test/doc

local p = {}

function p.generateClassLists(frame)
    -- JSON-Daten in einem Lua-String von der Parserfunktion abrufen
    local jsonData = frame:callParserFunction('#visualdataquery:[[unique title::+]]', { '?articleid', schema = 'Data classes', format = 'json-raw' })

    -- Debug: Gib die empfangenen JSON-Daten aus
    local debugInfo = "'''Received JSON Data: '''" .. tostring(jsonData) .. "\n"

    -- Überprüfe, ob jsonData gültig ist
    if not jsonData or jsonData == '' then
        return "'''Keine JSON-Daten empfangen.'''"
    end

    -- Entferne führende und nachfolgende Leerzeichen
    jsonData = mw.text.trim(jsonData)

    -- Debug: Gib die getrimmten JSON-Daten aus
    debugInfo = debugInfo .. "'''Trimmed JSON Data: '''" .. tostring(jsonData) .. "\n"

    -- Debug: Gib die Länge der JSON-Daten aus
    debugInfo = debugInfo .. "'''Length of JSON Data: '''" .. tostring(#jsonData) .. "\n"

    -- Debug: Gib die rohen JSON-Daten aus, um zu überprüfen, ob sie korrekt sind
    debugInfo = debugInfo .. "'''Raw JSON Data: '''" .. jsonData .. "\n"

    -- Ersetze nicht druckbare Zeichen durch sichtbare Platzhalter für Debug-Zwecke
    local jsonDataVisible = jsonData:gsub("%c", function(c) return string.format("\\x%02X", string.byte(c)) end)
    debugInfo = debugInfo .. "'''Visible JSON Data: '''" .. jsonDataVisible .. "\n"

    -- Überprüfe auf bekannte Platzhalter und entferne diese
    jsonData = jsonData:gsub("\127'\"`UNIQ%-%-.-%-%-QINU`\"'\127", "")

    -- JSON-Daten in Lua-Tabelle umwandeln
    local success, json = pcall(mw.text.jsonDecode, jsonData)
    if not success then
        return debugInfo .. "'''Fehler beim Dekodieren der JSON-Daten: '''" .. tostring(json)
    end

    debugInfo = debugInfo .. "'''Decoded JSON Data: '''" .. mw.dumpObject(json) .. "\n"

    local result = {}

    for _, item in ipairs(json) do
        local title = item.data.title
        local classNames = item.data["class names"]
        table.insert(result, "== " .. title .. " ==")
        table.insert(result, "* " .. table.concat(classNames, "\n* "))
    end

    return debugInfo .. table.concat(result, "\n\n")
end

return p