Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert tables to strings and vice versa
#1
Since somebody asked how this could be done, and a while ago I wrote this for my own server, I decided to post it here Smile  Maybe it'll come handy for somebody. It's probably not perfect. If you have ideas on how to improve it, please feel free to tell me.

The converted strings look something like this, and only strings of this fashion can be converted back to tables: {y{5090.97}x{47532.7}z{20632.1}angle{333.455}}

tableToString(table)

Code:
 
// -- Converts a table to a string, but other than tostring() it keeps the values and keys in stringformat -- //
local function tableToString(table)
{
    local fullString = null;
    
    foreach(key, value in table)
    {
        if (type(value) == "table")
        {
            // -- Calls itself again to convert tables within the table -- //
            
            // -- If the table is empty, the value will be "NULLTABLE" -- //
            if (value.len() > 0)
            {
                value = tableToString(value);
            }
            else
            {
                value = "NULLTABLE";
            }
        }
        else
        {
            if (value == null)
            {
                value = "NULL";
            }
        }
        
        
        if (fullString != null)
        {
            fullString += key + "{" + value + "}";
        }
        else
        {
            fullString = key + "{" + value + "}";
        }
    }
    
    if (table.len() == 0)
    {
        fullString = "NULLTABLE";
    }
    
    return fullString;
}

stringToTable(string)
Code:
// -- Converts a string that was converted with tableToString back to a table -- //
local function stringToTable(string)
{
    local keyStart = 0;
    local keyEnd = 0;
    local valueStart = 0;
    local valueEnd = 0;
    local nextValueStart = 0;
    local nextValueEnd = 0;
    
    local tableStart = 0;
    local tableEnd = 0;
    
    local key = null;
    local value = null;
    
    local currLetter = 0;
    local returnTable = {};
    
    for(;;)
    {
        keyStart = currLetter;
        if (keyStart == null)
        {
            break;
        }
        
        // -- Key ends with the next "{" -- //
        keyEnd = string.find("{", keyStart);
        
        if (keyEnd == null)
        {
            break;
        }
        // -- directly after the key, the value starts -- //
        valueStart = keyEnd + 1;
        
        // -- The value ends at the next "}" from valueStart -- //
        valueEnd = string.find("}", valueStart);
        
        // -- Already find the start of the next value -- //
        nextValueStart = string.find("{", valueStart);
        
        // -- If another valueStart was found, find it's end or otherwise set the start to the length of the string so
        // -- nextValueEnd will remain 0 -- //
        if (nextValueStart != null)
        {
            nextValueEnd = string.find("}", nextValueStart);
        }
        else
        {
            nextValueStart = string.len();
        }
        
        // -- Key goes from keyStart to keyEnd -- //
        key = string.slice(keyStart, keyEnd);
        
        // -- Value goes from valueStart to valueEnd -- //
        value = string.slice(valueStart, valueEnd);
        
        // -- Change currLetter for the next loop -- //
        currLetter = valueEnd + 1;
        
        // -- When a bracket was found without the last bracket being closed ( = Found another table) -- //
        if (nextValueStart < valueEnd)
        {
            tableStart = nextValueStart;
            tableEnd = tableStart;
            local klammernOffen = 1;
            local klammernZu = 0;
            local start = tableStart;
            
            // -- As long as there are still opened brackets that haven't yet been closed by another bracket -- //
            for (;klammernOffen != klammernZu;)
            {
                local currKlammerOffen = string.find("{", start);
                local currKlammerZu = string.find("}", start);
                
                // -- What kind of bracket is found next will be increased in order to go through all of them -- //
                if (currKlammerOffen != null && currKlammerOffen < currKlammerZu)
                {
                    klammernOffen++;
                    start = currKlammerOffen + 1;
                    tableEnd = string.find("}", tableEnd) + 1;
                }
                else
                {
                    klammernZu++;
                    start = currKlammerZu + 1;
                }
            }
            // -- Set currLetter to the end ob the table -- //
            currLetter = tableEnd + 1;
            
            // -- Calls itself again to convert a table within the table -- //
            value = stringToTable(string.slice(valueStart, tableEnd));
        }
        
        // -- Assign returnTable the current key (Could be another table) -- //
        
        if (value == "NULLTABLE")
        {
            value = {};
        }
        else if (value == "NULL")
        {
            value = null;
        }
        
        returnTable[key] <- value;
    }
    return returnTable;
}
Reply


Forum Jump:


Users browsing this thread: