Search code examples
jsonlualua-table

Lua JSON unexpected type `function`


I have following code for DCS:World scripting:

koUDPSocket = {}

koUDPSocket.host = "127.0.0.1"
--koTCPSocket.host = "85.221.224.254"
koUDPSocket.port = 52525
koUDPSocket.socket = socket.udp()
koUDPSocket.JSON = require "JSON"

function koUDPSocket:send(data, typee)
    local message = {
        typee = typee,
        data = data,
    }
    env.info("SENDING:")
    env.info(koEngine.TableSerialization(message))
    local jsonString = self.JSON:encode(message).." \n"

    socket.try(self.socket:sendto(jsonString, self.host, self.port))
    env.info("UDP DATA SENT")
 end
 


function countUnitsInTable(table)
    local count = 0
    for _, _ in pairs(table) do
        count = count + 1
    end
    return count
end

function koEngine.loadMissionDataShort()

    --debugee.pool()
    koEngine.debugText("koEngine.loadMissionDataShort")
    koEngine.debugText("loading shorted mission data")

    for categoryName, categoryTable in pairs(MissionData) do
        if type(categoryTable) == "table" and categoryName ~= "properties" then
            for objectiveName, objectiveTable in pairs(categoryTable) do
                if type(objectiveTable) == "table" then
                    local unitCount = 0
                    if objectiveTable.groups then
                        for _, group in pairs(objectiveTable.groups) do
                            if group.units then
                                unitCount = unitCount + countUnitsInTable(group.units)
                            end
                        end
                    end

                    local shortData = {
                        underAttack = objectiveTable.underAttack,
                        coa = objectiveTable.coa,
                        status = objectiveTable.status,
                        priority = objectiveTable.priority,
                        type = categoryName,
                        units = unitCount
                    }
                    missionDataShort[objectiveName] = shortData
                    koEngine.debugText("insert: " .. tostring(objectiveName))
                end
            end
        end
    end

    -- now send the data
    koUDPSocket:send(missionDataShort, "MissionDataShort")

    koEngine.debugText("END populating short data")
end

which gets all objectives from mission data, puts in table, and attempts to send to my Node.JS server to process further. Everything works, expect line with local jsonString = self.JSON:encode(message).." \n" which gives error "unecptected type: function"

interesting is that trying type(message) gives table, and not function

how can I determine where the real mistake is? And how can I debug data to really see if it's a function? What's the best way to do it?

Here is a fragment of data, outputted using env.info(koEngine.TableSerialization(message)) and previous line:

2023-08-23 19:27:44.840 INFO    SCRIPTING (Main): SENDING:
2023-08-23 19:27:44.848 INFO    SCRIPTING (Main): {
    ['data'] = {
        ['FARP Oche'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Antenna Bulls'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['FARP Tsvirmi'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Sukhumi-Babushara'] = {
            ['type'] = 'Aerodrome',
            ['underAttack'] = false,
            ['status'] = 'open',
            ['priority'] = 'strategic',
            ['units'] = 27,
            ['coa'] = 'blue',
        },
        ['FARP Lentehi'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Antenna Balkariya'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 3,
            ['coa'] = 'red',
        },
        ['Antenna Oche'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },

I wrote a code, I tested a possible problem, which it wasn't, and now I want to investigate further


Solution

  • idk I solved this, problem was with self.JSON:encode. Changed to JSON.encode, and worked