1

I am trying to read data from file.lua.
file.lua contains:

Settings = {
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [1]
    {
        ["msg"] = "цлк25н ппал шп маг ршам рдру сова (осколки анрол) 5 слотов",
        ["aut"] = "Savagemode",
        ["cha"] = "5. Поиск спутников",
    }, -- [2]
    {
        ["msg"] = "В СА25 танк/хил/дд/рдд",
        ["aut"] = "Dralo",
        ["cha"] = "5. Поиск спутников",
    }, -- [3]
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [4]
    {
        ["msg"] = "продам |cffa335ee|Hitem:36919:0:0:0:0:0:0:0:80|h[Багровый рубин]|h|rх6 по 120г",
        ["aut"] = "Аматин",
        ["cha"] = "2. Торговля: Город",
    }, -- [5]
}

How I'am able to read this file with java code? Where Settings is full log of messages. msg - message, aut - message author, cha - message channel
This is what I'm trying to use: my pom.xml, java code, and run result

<dependency>
      <groupId>com.googlecode.json-simple</groupId>
      <artifactId>json-simple</artifactId>
      <version>1.1.1</version>
</dependency>

JSONParser jsonParser = new JSONParser();

try (FileReader reader = new FileReader(filePath))
{
    //Read JSON file
    Object obj = jsonParser.parse(reader);

    JSONArray employeeList = (JSONArray) obj;
    System.out.println(employeeList);

    //Iterate over employee array
    //employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );

} 

//Run result:

Unexpected character (S) at position 2.
    at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
    at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
    at org.json.simple.parser.JSONParser.parse(JSONParser.java:92)
    at Main.main(Main.java:21)


I want to get any advice what should I use. Because after I read this file I want to check what is in variable msg by just calling getMsg() method;
I cant read this in one string but how I can work with data?

File myObj = new File(filePath);
      Scanner myReader = new Scanner(myObj);
      while (myReader.hasNextLine()) {
      String data = myReader.nextLine();
      System.out.println(data);
}
myReader.close();

//result
Settings = {
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [1]
    {
        ["msg"] = "цлк25н ппал шп маг ршам рдру сова (осколки анрол) 5 слотов",
        ["aut"] = "Savagemode",
        ["cha"] = "5. Поиск спутников",
    }, -- [2]
    {
        ["msg"] = "В СА25 танк/хил/дд/рдд",
        ["aut"] = "Dralo",
        ["cha"] = "5. Поиск спутников",
    }, -- [3]
    {
        ["msg"] = "в ич об нид мдд/рдд",
        ["aut"] = "Lightmur",
        ["cha"] = "5. Поиск спутников",
    }, -- [4]
    {
        ["msg"] = "продам |cffa335ee|Hitem:36919:0:0:0:0:0:0:0:80|h[Багровый рубин]|h|rх6 по 120г",
        ["aut"] = "Аматин",
        ["cha"] = "2. Торговля: Город",
    }, -- [5]
}

Process finished with exit code 0


Pshemo
  • 122,468
  • 25
  • 185
  • 269
Ilja Tarasovs
  • 181
  • 2
  • 13
  • 6
    Why are you reading a lua file with a JSON parser? They are not the same. – mwarren Apr 01 '20 at 13:01
  • 2
    Your file is an lua script file. To use Gson your file has to follow JSON convention. This isn't – Hassam Abdelillah Apr 01 '20 at 13:02
  • any ideas how to read this correctly? I need to read this file in one String then write it to file.txt and read it again with json ? – Ilja Tarasovs Apr 01 '20 at 13:03
  • 2
    [Use LuaJ](https://stackoverflow.com/q/2113432/1847592) – Egor Skriptunoff Apr 01 '20 at 13:17
  • 1
    I tried to improve title of this question since you are NOT asking how to read any file, but how read data from lua file. – Pshemo Apr 01 '20 at 14:01
  • 1
    There are tons of tutorials on how to read files in Java. What is wrong with them? – Piglet Apr 02 '20 at 07:11
  • 1
    The easiest will be to install lua and use some json library to dump that data structure as a json file, then read it from java as normal json – DarkWiiPlayer Apr 02 '20 at 07:44
  • @Piglet There is no problem to read file using java. But I'm looking for correct way how to do this. I cant just simply read this line by line and check if string contains necessary info. But as I know it's not good for performance. – Ilja Tarasovs Apr 02 '20 at 10:36
  • 1
    ideally you run that Lua code from Java which will give you access to the data. if you insist on parsing the Lua file you won't get around reading the file line by line and check it's contents. if performance is an issue while reading a short script file you probably should think about your overalll design. – Piglet Apr 02 '20 at 10:59

1 Answers1

2

Lua is a programming language, not a data language, and so the file should not be parsed, but instead executed.

You can write a Lua script, to execute this script and encode it as JSON.

  • Start by installing luarocks with the Lua interpreter (luarocks for windows comes bundled with that, if on linux, install lua and luarocks using your packages manager).
  • Install the json-lua library to encode the data as JSON:
luarocks install json-lua
  • Write the following Lua script in script.lua:
local JSON = require("JSON") --The JSON library

local source = "file.lua"
local destination = "file.json"

dofile(source)

--Now after executing the file.lua script, the data has been stored in the global table Settings.

--Encode into JSON data.
local jsonData = JSON:encode_pretty(Settings)

--Open the destination file for writing.
local file = assert(io.open(destination, "w"))

--Write the JSON data.
assert(file:write(jsonData))

--Close the file.
file:close()

print("Wrote file.json successfully!")
  • Place script.lua and file.lua in the same folder, and run script.lua from there:
lua script.lua
  • The data should be encoded into JSON in the file file.json, now you can parse it under Java.
Rami Sabbagh
  • 125
  • 9
  • "_not a data language_" -- Lua is considered a data description language. The Lua _About_ webpage says right at the top: ["_It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description._"](https://www.lua.org/about.html) _Programming in Lua_ says that "_Data description has been one of the main applications of Lua since its creation in 1993._" – ad absurdum Jul 26 '20 at 16:36
  • I meant it's not a language like XML, JSON, HTML, etc.. What would you suggest to edit? – Rami Sabbagh Jul 26 '20 at 18:27