serializing lua tables

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

serializing lua tables

Adrian Sietsma

i have written a lua table / variable serializer (as i suppose many others have)

but it doesn't
a/   preserve order of named fields
2/   preserve comments on read/re-write
III/ preserve blank lines

i can see how to do a/ , but before i do any work on this i thought i would ask...

requirements :

a routine that can read a text file similar to :


-- this is a dummy config file
-- general prefs
name = "fred"
age = 12
colour = "red"

-- favorite things
prefers = {"fish", "wine", "tobacco" }

-- end config


into a table (that's easy), and then a routine to write it back (also easy), in source-file order (harder), with the comments (line comments only is good enough).

has someone done this ? it would seem to be a generally useful utility.

i'll probably end up writing it myself soon if not.

(why serialise via xml if you can do it in lua)

Adrian

Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Adam D. Moss
Adrian Sietsma wrote:
(why serialise via xml if you can do it in lua)

Because the other end of the exchange might not be using
Lua.  On the other hand, since the entirety of Lua is
smaller to embed than most XML parsers, that's a poor
excuse. ;)

--Adam
--
Adam D. Moss   -   [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: serializing lua tables

Framework Studios: Hugo
In reply to this post by Adrian Sietsma
> (why serialise via xml if you can do it in lua)

...because Xml is a standard.

Why make up yet another 'do it in lua' format when you have Xml?

In our case, we use Xml for example for serialising animation sequences,
editor settings, highscores, user settings and, of course, lua tables (and
HTML for the log-file ;) ... all can be read using the IE, to give just one
reason, saving space in the end by using just one Xml parser in stead of a
dozen custom format parsers is another.


Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Klaus Ripke
In reply to this post by Adam D. Moss
On Monday 21 March 2005 15:32, Adam D. Moss wrote:
> Because the other end of the exchange might not be using
> Lua.  On the other hand, since the entirety of Lua is
> smaller to embed than most XML parsers, that's a poor
> excuse. ;)
absolutely!
Especially, because the other end might not be using XML
-- the XML version you require, that is.

XML interoperability is a pita sometimes and impossible other times.
(When you need different parser implementations to grok
different documents but the environment won't let you link
to multiple parsers).

In general, at least since the days of the OSI stack one can say
that a protocol which defines so much that it's never implemented
completely and next to correctly is not well defined in practice.

"Lua 5", on the other hand, is very well defined, and the actual
parsing code for whatever style of serialization could even be sent
along with the data :)




Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Kriss@XIXs.com
In reply to this post by Adrian Sietsma
Adrian Sietsma wrote:


into a table (that's easy), and then a routine to write it back (also easy), in source-file order (harder), with the comments (line comments only is good enough).

has someone done this ? it would seem to be a generally useful utility.

I think I'm of the opinion that if the comments are important to maintain between reads/writes they don't belong in lua comments but instead as a comments field in the table data.

Well, its an easy solution at least :)

Same for the ordering of the tables, if thats ultra important then work it into the data.

A generic system to do what you want, probably comes under the heading of hard.


--

Kriss

http://XIXs.com -><- http://www.WetGenes.com



Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Adrian Sietsma
In reply to this post by Framework Studios: Hugo
Framework Studios: Hugo wrote:
(why serialise via xml if you can do it in lua)
...because Xml is a standard.

Why make up yet another 'do it in lua' format when you have Xml?

because i can just loadfile/dofile the config from lua. it's not another format, simply lua.

I was not saying there's no use for xml, just that i use lua as a simple configuration language for my app(s), and it works just fine; but - i have no simple way for the app to modify that config & rewrite it, preserving the original format.

i have lua read-write configs, but i'd like to keep the config format and comments.

in this case, interoperability (is that a word?) is not required, and a small lua text file/table is easier to read / document / edit than xml.

also, as Klaus Ripke comments, what is xml ?
if you support all the dtd/entities/embedded docs/encodings defined in xml, the parser is non-trivial, especially for a small app. if your xml parser supports only a subset of those, what standard is it ?

i have used xml for data interchange, but it's too wordy for my simple 10-variable config file.

i'm not trying to start a "which is best" discussion (pc/mac, windows/*nix, emacs/vi - we could be debating that for years).

Adrian

Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Adrian Sietsma
In reply to this post by Kriss@XIXs.com
Kriss wrote:
I think I'm of the opinion that if the comments are important to maintain between reads/writes they don't belong in lua comments but instead as a comments field in the table data.

Well, its an easy solution at least :)

i may have to live with that..

Same for the ordering of the tables, if thats ultra important then work it into the data.

it's not vital (i do without it now), but nice-to-have

i have a rough outline (use _newindex to build an array in declaration
order), but i don't want to re-invent the wheel if it's been done.

Adrian



Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Petite Abeille
In reply to this post by Adrian Sietsma

On Mar 21, 2005, at 16:59, Adrian Sietsma wrote:

i have lua read-write configs, but i'd like to keep the config format and comments.

What about perhaps reading your configuration file line by line into a table array to preserve all the content as well as the ordering. Out of this array, you could extract the configuration lines themselves by splitting each lines which are not comments into key value pairs. This gives you the configuration itself. After modifying a value, you can rewrite your configuration file as it was originally by merging the changes into the table array. That way you could preserve both ordering and comments. The only issue left is where to put new key value pairs. Perhaps dumping them at the end of the configuration file would be good enough?

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/


Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Rici Lake-2
In reply to this post by Adrian Sietsma

On 21-Mar-05, at 11:08 AM, Adrian Sietsma wrote:

i have a rough outline (use _newindex to build an array in declaration
order), but i don't want to re-invent the wheel if it's been done.

http://lua-users.org/wiki/OrderedTable


Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Aaron Brown-2
Rici wrote:

> http://lua-users.org/wiki/OrderedTable

where I found

> tab.firstName = "Rici"
> tab.lastName = "Lake"
> tab.maternalLastName = "Papert"

Rici, may I ask whether you happen to be related to computer
pioneer Seymour Papert?

-- 
Aaron


Reply | Threaded
Open this post in threaded view
|

Re: serializing lua tables

Michael T. Richter-3
In reply to this post by Framework Studios: Hugo

(why serialise via xml if you can do it in lua)

...because Xml is a standard.

And, as with most standards, it is mostly honoured by not implementing it correctly. (C.f. C, C++, Lisp, Haskell, ML, Forth, etc. for similar examples.)


Reply | Threaded
Open this post in threaded view
|

RE: serializing lua tables

Framework Studios: Hugo
> -----Original Message-----
> From: [hidden email]
> [[hidden email] Behalf Of Michael T.
> Richter
> Sent: woensdag 23 maart 2005 6:44
> To: Lua list
> Subject: Re: serializing lua tables
> 
> 
> 
> >>(why serialise via xml if you can do it in lua)
> >>    
> >>
> >
> >...because Xml is a standard.
> >
> >  
> >
> And, as with most standards, it is mostly honoured by not 
> implementing 
> it correctly.  (C.f. C, C++, Lisp, Haskell, ML, Forth, etc. 
> for similar 
> examples.)

hehe, not responsible for tinyxml here... ;)