|
1 | 1 | # LuaFluidJson |
2 | | -An json Parser for lua that doesnt require any adaptation |
| 2 | +An json Parser for lua that doesn't require any adaptation |
3 | 3 |
|
4 | 4 |
|
| 5 | +Beside others parsers that requires adaptation to load and dump jsons, these LuaFluidJson use |
| 6 | +the native mechanics of lua to implement the load and dump processing, allowing a super easy way |
| 7 | +to deal with jsons. |
5 | 8 |
|
6 | | -s |
| 9 | +<h3 style="color:red;">THESE LIB ITS NOT PRODUCTION READY USE WITH RESPONSIBILITY</h3> |
| 10 | + |
| 11 | +### Platforms supported: |
| 12 | +**Linux** (Tested on Fedora and Ubuntu) |
| 13 | + |
| 14 | +### Installation |
| 15 | +The instalation doesnt require lua rocks or any package manager, just download the |
| 16 | +[download link](aaa) into your project and copy in the root of your main,and you can start using |
| 17 | + |
| 18 | +### Dumping Json file |
| 19 | +these code dumps the following struct into a json file |
| 20 | +```lua |
| 21 | +local json = require("luaFluidJson/luaFluidJson") |
| 22 | + |
| 23 | +local user ={ |
| 24 | + |
| 25 | + name='Nateus', |
| 26 | + age=27, |
| 27 | + maried=false, |
| 28 | + sons={ |
| 29 | + {name='son1',maried='null'} |
| 30 | + } |
| 31 | +} |
| 32 | +local ident = true |
| 33 | +json.dumps_to_file(user,"test/target/out.json",ident); |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +### Dumping a json String |
| 38 | +if you need to dump a JSON String its also easy, you can just call the **dumps_to_string** function |
| 39 | + |
| 40 | +```lua |
| 41 | +local json = require("luaFluidJson/luaFluidJson") |
| 42 | + |
| 43 | +local user ={ |
| 44 | + |
| 45 | + name='Nateus', |
| 46 | + age=27, |
| 47 | + maried=true, |
| 48 | + sons={ |
| 49 | + {name='son1',maried=false } |
| 50 | + } |
| 51 | +} |
| 52 | +local ident = true |
| 53 | +local value = json.dumps_to_string(user,ident); |
| 54 | + |
| 55 | +print(value) |
| 56 | +``` |
| 57 | + |
| 58 | +### Loading a Json from File |
| 59 | +you can easly load a json direct from file |
| 60 | +```lua |
| 61 | + |
| 62 | +local json = require("luaFluidJson/luaFluidJson") |
| 63 | + |
| 64 | + |
| 65 | +local parsed = json.load_from_file("test/target/element.json"); |
| 66 | + |
| 67 | +print("name:"..parsed.name) |
| 68 | +print("age:"..parsed.age) |
| 69 | +print("maried:"..tostring(parsed.maried)) |
| 70 | +for i,v in ipairs(parsed.sons) do |
| 71 | + local son_name = v["name"] |
| 72 | + print("son name:"..son_name) |
| 73 | +end |
| 74 | +``` |
| 75 | +### Loading a Json from String |
| 76 | +its also possible to load a json from string |
| 77 | + |
| 78 | +```lua |
| 79 | + |
| 80 | +local json = require("luaFluidJson/luaFluidJson") |
| 81 | + |
| 82 | +local test = '{"name":"mateus","age":27}' |
| 83 | + |
| 84 | +local parsed = json.load_from_string(test); |
| 85 | + |
| 86 | +print("name:"..parsed.name) |
| 87 | +print("age:"..parsed.age) |
| 88 | + |
| 89 | +``` |
| 90 | +### The Null Terminator |
| 91 | +since the lua nill doesn't work as other languages "nulls" ,the lib consider null as a string |
| 92 | +so if you put "null" into your code, the lib will consider as a null value |
| 93 | + |
| 94 | +```lua |
| 95 | +local json = require("luaFluidJson/luaFluidJson") |
| 96 | + |
| 97 | +local user ={ |
| 98 | + |
| 99 | + name='Nateus', |
| 100 | + age=27, |
| 101 | + maried=false, |
| 102 | + sons='null' |
| 103 | +} |
| 104 | +local ident = true |
| 105 | +json.dumps_to_file(user,"test/target/out.json",ident); |
| 106 | +``` |
| 107 | +### Null Scape |
| 108 | +its also possible to scape null putting another code into its place |
| 109 | + |
| 110 | +```lua |
| 111 | + |
| 112 | +local json = require("luaFluidJson/luaFluidJson") |
| 113 | +json.set_null_code("custom_null") |
| 114 | +local user ={ |
| 115 | + |
| 116 | + name='Nateus', |
| 117 | + age=27, |
| 118 | + maried=false, |
| 119 | + sons='custom_null' |
| 120 | +} |
| 121 | +local ident = true |
| 122 | +json.dumps_to_file(user,"test/target/out.json",ident); |
| 123 | +``` |
| 124 | + |
| 125 | +### Identifying objects |
| 126 | +if you want to ensure the following table will be parsed as a object, you can just call the |
| 127 | +**is_table_a_object** function |
| 128 | + |
| 129 | +```lua |
| 130 | + |
| 131 | +local json = require("luaFluidJson/luaFluidJson") |
| 132 | + |
| 133 | +local test = {1,2,3} |
| 134 | +print(json.is_table_a_object(test)) |
| 135 | + |
| 136 | +local test2 = {a=20,b=30} |
| 137 | + |
| 138 | +print(json.is_table_a_object(test2)) |
| 139 | + |
| 140 | +``` |
0 commit comments