Jump to content

net.lua2json : another bug detected (array[0] - index 0 incompatibility)


Recommended Posts

Posted (edited)

When you pass an array with a zero ('0') index, that index is not written to json when converting a table with net.lua2json.

This can be reproduced at will: take the warehouse inventory from any base, and convert that to json. The liquids are defined as an array, with liquids[0] being jet fuel. Now look at the json result of lua2json conversion and see that liquids never contains array item index 0. 

Edited by cfrag
Posted (edited)
2 hours ago, Flappie said:

I have no idea of to test this.

It's straightforward, here's script, explanation, demo etc.

Result:

image.png

*) Should say "No index 0" above

The script first gets the warehouse inventory for Anapa, and then dumps the table (method t.vardump2str)

Note that we have four entries into the array for liquids (side note: IMHO, whoever signed off on the idea to make liquids an array while all other inventories are dictionaries should be sent to bed without supper, and they shouldn't contribute anything to DCS until they get a modicum of design and coding experience. This is amateur stuff, even before the "brilliant" idea of using a 0-index array in Lua)

The script then invokes lua2json on the inventory, and dumps the resulting string. Note that the jetfuel index is missing (or rather, one is missing, since the array only holds 3 items. I know it's index 0 because I set the amounts during my testing to different values and was able to identify that index 0 is missing).

 

Here's the (trivial) script: 

Spoiler

t = {}

function t.dumpVar2Str(key, value, prefix, inrecursion)
    -- dumps to screen, not string 
    if not inrecursion then 
        -- output a marker to find in the log / screen
        trigger.action.outText("*** vardump START",30)
    end
    if not value then value = "nil" end
    if not prefix then prefix = "" end
    prefix = " " .. prefix
    if type(value) == "table" then 
        trigger.action.outText(prefix .. key .. ": [ ", 30)
        -- iterate through all kvp
        for k,v in pairs (value) do
            t.dumpVar2Str(k, v, prefix, true)
        end
        trigger.action.outText(prefix .. " ] - end " .. key, 30)
        
    elseif type(value) == "boolean" then 
        local b = "false"
        if value then b = "true" end
        trigger.action.outText(prefix .. key .. ": " .. b, 30)
        
    else -- simple var, show contents, ends recursion
        trigger.action.outText(prefix .. key .. ": " .. value, 30)
    end
    
    if not inrecursion then 
        -- output a marker to find in the log / screen
        trigger.action.outText("=== t vardump end", 30)
    end
end

function t.getFirstState()
    local allMyBase = world:getAirbases()
    for idx, theBase in pairs(allMyBase) do 
        local name = theBase:getName()
        local WH = theBase:getWarehouse()
        local inv = WH:getInventory()
        trigger.action.outText("read inventory for " .. name, 30)
        return inv -- simply return first inventory
    end 
end

function t.performTests()
    -- read first warehouse inventory
    local inv = t.getFirstState()
    -- dump it 
    t.dumpVar2Str("Inventory: ", inv)
    -- convert to json 
    local json = net.lua2json(inv)
    trigger.action.outText("json=" .. json, 30)
end

-- go!
t.performTests()
 

And here's the miz to play aground with. As you can see, the error can be re-produced at will.

Cheers,

-ch

 

t json.miz

Edited by cfrag
  • Like 2
  • Thanks 1
Posted

@Flappie

In my humble opinion ED would do well to have closer links with @cfrag. His work on the DML Tool box scripting utility for people (like me) who have no idea how to write scripts is simply outstanding. He clearly can demonstrate he knows what he is talking about when it comes good, solid coding and structure methodology.

No, I have not been paid by cfrag to say this, Yes I am "Fan-Boy" of his utility 🙂

 

Visit the Dangerdogz at www.dangerdogz.com. We are a group based on having fun (no command structure, no expectations of attendance, no formal skills required, that is not to say we can not get serious for special events, of which we have many). We play DCS and IL2 GBS. We have two groups one based in North America / Canada and one UK / Europe. Come check us out. 

Posted
28 minutes ago, cfrag said:

It's straightforward

For you, it is. 😄

I studied programming a long time ago, but I'm too lazy.

Thanks a bunch, I'll check and report.

---

Posted
45 minutes ago, DD_Friar said:

@Flappie

In my humble opinion ED would do well to have closer links with @cfrag. His work on the DML Tool box scripting utility for people (like me) who have no idea how to write scripts is simply outstanding. He clearly can demonstrate he knows what he is talking about when it comes good, solid coding and structure methodology.

I strongly agree, for I've been reporting previous issues he made me aware of. But I don't work at ED, I can't "hire" him. 😋

  • Like 1

---

  • 2 weeks later...
Posted
On 2/5/2025 at 1:48 PM, Flappie said:

I strongly agree, for I've been reporting previous issues he made me aware of. But I don't work at ED, I can't "hire" him.

@Flappie So ED is outsourcing bug triage to unpaid volunteers like yourself?

  • Like 1
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...