Skip to content

Commit c0a798d

Browse files
committed
Add Workbooks. Update README.md.
1 parent 33b67cf commit c0a798d

File tree

4 files changed

+306
-1
lines changed

4 files changed

+306
-1
lines changed

README.md

Lines changed: 159 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,161 @@
11
# LUAON.NET
22

3-
Writing LUA table expression becomes easy.
3+
Writing LUA table expressions becomes easy. It's LUA Object Notation (or LUA Table Notation).
4+
5+
| Package | Status |
6+
| ---------------------------------------- | ---------------------------------------- |
7+
| [CXuesong.Luaon](https://www.nuget.org/packages/CXuesong.Luaon) | ![NuGet version (CXuesong.Luaon)](https://img.shields.io/nuget/vpre/CXuesong.Luaon.svg?style=flat-square) ![NuGet version (CXuesong.Luaon)](https://img.shields.io/nuget/dt/CXuesong.Luaon.svg?style=flat-square) |
8+
| [CXuesong.Luaon.Json](https://www.nuget.org/packages/CXuesong.Luaon.Json) | ![NuGet version (CXuesong.Luaon.Json)](https://img.shields.io/nuget/vpre/CXuesong.Luaon.Json.svg?style=flat-square) ![NuGet version (CXuesong.Luaon.Json)](https://img.shields.io/nuget/dt/CXuesong.Luaon.Json.svg?style=flat-square) |
9+
10+
[Try out the packages with Xamarin Workbooks!](Workbooks)
11+
12+
## CXuesong.Luaon
13+
14+
This package contains classes for writing Lua tables in a manner similar to `Newtonsoft.Json`, such as
15+
16+
Basic simple-value converter
17+
18+
```c#
19+
using Luaon;
20+
LuaConvert.ToString(12345)
21+
```
22+
23+
```lua
24+
12345
25+
```
26+
27+
```c#
28+
// Custom string delimiter
29+
LuaConvert.ToString("string expression\n\nNew line", "[=====[")
30+
```
31+
32+
```lua
33+
[=====[string expression
34+
New line]=====]
35+
```
36+
37+
A primitive counterpart of `Newtonsoft.Json.Linq`.
38+
39+
```c#
40+
using Luaon.Linq;
41+
var table = new LTable();
42+
table.Add("Item1");
43+
table.Add("Key1", 123);
44+
table.Add("Key with space", 789);
45+
table.Add("Item2");
46+
table.Add(100, "Value100");
47+
table.Add("ChildTable", new LTable(1, 2, 3, 4, 5));
48+
table.ToString();
49+
```
50+
51+
```lua
52+
{
53+
"Item1",
54+
Key1 = 123,
55+
["Key with space"] = 789,
56+
"Item2",
57+
[100] = "Value100",
58+
ChildTable = {
59+
1,
60+
2,
61+
3,
62+
4,
63+
5
64+
}
65+
}
66+
```
67+
68+
Access table values in a Lua way.
69+
70+
```c#
71+
table[2]
72+
```
73+
74+
```lua
75+
"Item2"
76+
```
77+
78+
And a Lua table writer
79+
80+
```c#
81+
using System.IO;
82+
using (var sw = new StringWriter())
83+
{
84+
using (var lw = new LuaTableTextWriter(sw) {
85+
Formatting = Formatting.Prettified,
86+
CloseWriter = false
87+
})
88+
{
89+
lw.WriteStartTable();
90+
lw.WriteLiteral("Records");
91+
lw.WriteKey("Updated");
92+
lw.WriteLiteral(DateTime.Now);
93+
lw.WriteLiteral("Value1");
94+
lw.WriteLiteral("Value2");
95+
lw.WriteLiteral("Value3");
96+
lw.WriteEndTable();
97+
}
98+
return sw.ToString();
99+
}
100+
```
101+
102+
```lua
103+
{
104+
"Records",
105+
Updated = "2018-01-12T23:13:27.7863687+08:00",
106+
"Value1",
107+
"Value2",
108+
"Value3"
109+
}
110+
```
111+
112+
## CXuesong.Luaon.Json
113+
114+
This package contains `JsonLuaWriter`, a `JsonWriter` implementation that writes Lua tables instead of JS objects.
115+
116+
```c#
117+
using Luaon.Json;
118+
using Newtonsoft.Json;
119+
using Newtonsoft.Json.Linq;
120+
using System.IO;
121+
122+
var obj = new {
123+
Name = "Graystripe",
124+
Affiliation = new {
125+
Current = "ThunderClan",
126+
Past = new [] { "RiverClan", "Kittypet", "Loner" },
127+
},
128+
Age = 132 // Months
129+
};
130+
131+
var serializer = new JsonSerializer();
132+
using (var sw = new StringWriter())
133+
{
134+
using (var jlw = new JsonLuaWriter(sw) {
135+
CloseOutput = false,
136+
Formatting = Formatting.Indented})
137+
{
138+
serializer.Serialize(jlw, obj);
139+
}
140+
return sw.ToString();
141+
}
142+
```
143+
144+
```lua
145+
{
146+
Name = "Graystripe",
147+
Affiliation = {
148+
Current = "ThunderClan",
149+
Past = {
150+
"RiverClan",
151+
"Kittypet",
152+
"Loner"
153+
}
154+
},
155+
Age = 132
156+
}
157+
```
158+
159+
## As for table readers
160+
161+
It's not my priority yet XD If you want it, consider opening an issue or PR to let me know ^ _ ^

Workbooks/Luaon.Json.workbook

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
uti: com.xamarin.workbook
3+
id: 28bfdbe0-89c3-48c2-9fb4-8a306c6758e3
4+
title: Luaon.Json
5+
platforms:
6+
- DotNetCore
7+
packages:
8+
- id: CXuesong.Luaon
9+
version: 0.1.1
10+
- id: CXuesong.Luaon.Json
11+
version: 0.1.0
12+
---
13+
14+
```csharp
15+
#r "Luaon.NET"
16+
#r "Luaon.Json"
17+
```
18+
19+
```csharp
20+
using Luaon.Json;
21+
using Newtonsoft.Json;
22+
using Newtonsoft.Json.Linq;
23+
```
24+
25+
We also have a `JsonWriter` implementation that allows you to write JSON in Lua way.
26+
27+
```csharp
28+
using System.IO;
29+
30+
var obj = new {
31+
Name = "Graystripe",
32+
Affiliation = new {
33+
Current = "ThunderClan",
34+
Past = new [] { "RiverClan", "Kittypet", "Loner" },
35+
},
36+
Age = 132 // Months
37+
};
38+
39+
var serializer = new JsonSerializer();
40+
using (var sw = new StringWriter())
41+
{
42+
using (var jlw = new JsonLuaWriter(sw) {
43+
CloseOutput = false,
44+
Formatting = Formatting.Indented})
45+
{
46+
serializer.Serialize(jlw, obj);
47+
}
48+
return sw.ToString();
49+
}
50+
```

Workbooks/Luaon.workbook

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
uti: com.xamarin.workbook
3+
id: 87471c2e-6d90-49ef-94e1-6ad4663015b0
4+
title: Luaon
5+
platforms:
6+
- DotNetCore
7+
packages:
8+
- id: CXuesong.Luaon
9+
version: 0.1.1
10+
- id: CXuesong.Luaon.Json
11+
version: 0.1.0
12+
---
13+
14+
```csharp
15+
#r "Luaon.NET"
16+
```
17+
18+
```csharp
19+
using Luaon;
20+
```
21+
22+
You can convert various values into Lua expressions with `LuaConvert`.
23+
24+
```csharp
25+
LuaConvert.ToString(12345)
26+
```
27+
28+
```csharp
29+
LuaConvert.ToString("string expression\n\nNew line")
30+
```
31+
32+
```csharp
33+
// Custom string delimiter
34+
LuaConvert.ToString("string expression\n\nNew line", "[=====[")
35+
```
36+
37+
Like `Newtonsoft.Json.Linq`, you can create a `LTable`, insert values into it, and convert it into string of Lua expression.
38+
39+
```csharp
40+
using Luaon.Linq;
41+
var table = new LTable();
42+
table.Add("Item1");
43+
table.Add("Item2");
44+
table.Add("Item3");
45+
table.Add("Key1", 123);
46+
table.Add("Key2", 456);
47+
table.Add("Key with space", 789);
48+
table.Add("Item4");
49+
table.Add(100, "Value100");
50+
table.Add("ChildTable", new LTable(1, 2, 3, 4, 5));
51+
table.ToString();
52+
```
53+
54+
```csharp
55+
// You can access table fields in a LUA way.
56+
(table[1], table[4], table["Key with space"], table[100], table["ChildTable"][2])
57+
```
58+
59+
```csharp
60+
// You can convert LValue from and to other basic data types, just like JValue
61+
(int)table["Key2"] * 100
62+
```
63+
64+
For advanced users, you can operate on `LuaTableTextWriter` directly. But the API of this class might be subject to some major changes. Anyway, I’m making it feel like `JsonTextWriter`, except that for now it does not provide auto-completion. You need to close the table expression manually.
65+
66+
```csharp
67+
using System.IO;
68+
using (var sw = new StringWriter())
69+
{
70+
using (var lw = new LuaTableTextWriter(sw) {
71+
Formatting = Formatting.Prettified,
72+
CloseWriter = false
73+
})
74+
{
75+
lw.WriteStartTable();
76+
lw.WriteLiteral("Records");
77+
lw.WriteKey("Updated");
78+
lw.WriteLiteral(DateTime.Now);
79+
80+
lw.WriteKey("Item1");
81+
lw.WriteStartTable();
82+
lw.WriteLiteral("Value1");
83+
lw.WriteLiteral("Value2");
84+
lw.WriteLiteral("Value3");
85+
lw.WriteEndTable();
86+
87+
lw.WriteKey("Item2");
88+
table["ChildTable"].WriteTo(lw);
89+
90+
lw.WriteEndTable();
91+
}
92+
return sw.ToString();
93+
}
94+
```

Workbooks/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Workbooks
2+
3+
This folder contains some usage examples that can be opened with [Xamarin Workbooks](https://github.com/Microsoft/workbooks). You may download the latest version of Xamarin Workbooks [here](https://github.com/Microsoft/workbooks/releases).

0 commit comments

Comments
 (0)