Jump to content

Serialise a list to a string, preserving type, and deserialing the string to list


Mollymews
 Share

You are about to reply to a thread that has been inactive for 893 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

a conversation here

discussed serialising a list to a string and deserialising it back to a list. When the string is not formally serialised (like when is coming from an independent source for example) then we have to pick the string apart as @Coffee Pancake shows. If it is formally serialised then we can do it  as  @Profaitchikenz Haiku shows

picking up on Prof's method then a way to do this is:

edit add: Replaced magic numbers with the type constants as per feedback from @Quistess Alpha

// List2Serial - serialise a list to a string denoting element type
// Serial2List - deserialise the string back to a list restoring element type 
//
// public domain
//
// hat tips: Profaitchikenz Haiku, Coffee Pancake, Quistess Alpha


string List2Serial(list source, string seperator)
{   // prepend the list type to the serialised element
    string result;
    integer len = llGetListLength(source) - 1;
    integer i;    
    for (i = 0; i < len; ++i)
    {   
        result += (string)llGetListEntryType(source, i) + llList2String(source, i) + seperator;
    }
    return result += (string)llGetListEntryType(source, -1) + llList2String(source, -1);
}

list Serial2List(string source, string seperator)
{   // source conforms to List2Serial format
    list result;
    list buffer = llParseString2List(source, [seperator], []);
    integer len = llGetListLength(buffer);
    integer i;    
    for (i = 0; i < len; ++i)
    {   
       string element = llList2String(buffer, i);
       integer type = (integer)llGetSubString(element, 0, 0);
       element = llDeleteSubString(element, 0, 0);
       if (type == TYPE_ROTATION)  
          result += [(rotation)element];
       else if (type == TYPE_VECTOR)
          result += [(vector)element]; 
       else if (type == TYPE_KEY)
          result += [(key)element]; 
       else if (type == TYPE_FLOAT)
          result += [(float)element]; 
       else if (type == TYPE_INTEGER)
          result += [(integer)element]; 
       else  // TYPE_STRING or TYPE_INVALID
          result += [element];               
    }
    return result;
}

default
{
    state_entry()
    {
        list data = [ ZERO_ROTATION, < 0.1, 2, -3.4>, llGenerateKey(), 2.3, -1, "hello" ];      
        llOwnerSay("begin: " + llDumpList2String(data, "|"));
      
        string s = List2Serial(data, "|");
        llOwnerSay("serial: " + s);
      
        data = Serial2List(s, "|");
        llOwnerSay("end: " + llDumpList2String(data, "|"));
    }       
}

 

Edited by Mollymews
  • Like 2
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 893 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...