Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to parse a Lua datastructure in C# / .Net

Tags:

c#

parsing

lua

Anyone know of an easy way to parse a Lua datastructure in C# or with any .Net library? This would be similar to JSON decoding, except for Lua instead of javascript.

At this point it looks like I'll need to write my own, but hoping there's something already out there.

like image 807
Frank Schwieterman Avatar asked May 19 '09 07:05

Frank Schwieterman


4 Answers

Thanks to both of you, I found what I was looking for using LuaInterface

Here's a datastructure in Lua I wanted to read ("c:\sample.lua"):

TestValues = {
    NumbericOneMillionth = 1e-006,
    NumbericOnehalf = 0.5,
    NumbericOne = 1,
    AString = "a string"
}

Here's some sample code reading that Lua datastructure using LuaInterface:

Lua lua = new Lua();

var result = lua.DoFile("C:\\sample.lua");

foreach (DictionaryEntry member in lua.GetTable("TestValues")) {
    Console.WriteLine("({0}) {1} = {2}", 
        member.Value.GetType().ToString(), 
        member.Key, 
        member.Value);
}

And here's what that sample code writes to the console:

(System.String) AString = a string
(System.Double) NumbericOneMillionth = 1E-06
(System.Double) NumbericOnehalf = 0.5
(System.Double) NumbericOne = 1

To figure out how to use the library I opened up the LuaInterface.dll in Reflector and google'd the member functions.

like image 194
Frank Schwieterman Avatar answered Nov 09 '22 23:11

Frank Schwieterman


What Alexander said. The lab is the home of Lua, after all.

Specifically, LuaInterface can allow a Lua interpreter to be embedded in your application so that you can use Lua's own parser to read the data. This is analogous to embedding Lua in a C/C++ application for use as a config/datafile language. The LuaCLR project might be fruitful at some point as well, but it may not be quite as mature.

like image 5
RBerteig Avatar answered Nov 09 '22 22:11

RBerteig


LsonLib can parse Lua data structures, manipulate them and serialize the result back to Lua text. Full disclosure: I am the author. It's pure C# and has no dependencies.

Given:

MY_VAR = { "Foo", ["Bar"] = "Baz" }
ANOTHER = { 235, nil }

Basic usage:

var d = LsonVars.Parse(File.ReadAllText(somefile));

d["MY_VAR"][1].GetString()     // returns "Foo"
d["MY_VAR"]["Bar"].GetString() // returns "Baz"
d["MY_VAR"][2]                 // throws

d["ANOTHER"][1].GetString()    // throws because it's an int
d["ANOTHER"][1].GetInt()       // returns 235
d["ANOTHER"][2]                // returns null
d["ANOTHER"][1].GetStringLenient() // returns "235"

d["ANOTHER"][1] = "blah";      // now { "blah", nil }
d["ANOTHER"].Remove(2);        // now { "blah" }

File.WriteAllText(somefile, LsonVars.ToString(d)); // save changes

(it's actually a fairly straightforward port of a JSON library we use internally, hence it has quite a few features and might have some JSON traces left over)

like image 5
Roman Starkov Avatar answered Nov 09 '22 23:11

Roman Starkov


You may (or may not) find what you need among Lablua projects.

In any case, do not hesitate to ask your question on Lua mailing list.

like image 1
Alexander Gladysh Avatar answered Nov 09 '22 21:11

Alexander Gladysh