Static data load

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

static_data_load(path, callback)

static_data_load is used to get load static data from a JSON, CSV or text file.

Return value

This function won't return any value.

Arguments

# Argument Type Description
1 path String The location of the static file inside the resource folder (*.csv, *.json or *.txt).
2 callback Function (Optional) Function is called when data has been loaded. Data argument contains the data, is nil on error.

Example (JSON)

Example person.json file:

{
  "firstName": "John",
  "lastName": "Smith",
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    "212 555-1234",
    "646 555-4567",
    "123 456-7890"
  ]
}

Synchronous

Lua code to read JSON example above synchronously.

data = static_data_load("person.json")

-- Print first name
print(data["firstName"])

-- Print street address
print(data["address"]["streetAddress"])

-- Print all phone numbers
for key,value in pairs(data["phoneNumbers"]) do
  print(value)
end

Asynchronous

Lua code to read JSON example above asynchronously.

static_data_load("person.json", function(data)
  -- Print first name
  print(data["firstName"])

  -- Print street address
  print(data["address"]["streetAddress"])
  
  -- Print all phone numbers
  for key,value in pairs(data["phoneNumbers"]) do
    print(value)
  end
end)

Example (CSV)

Example cars.csv file:

 Year,Make,Model,Description,Price
 1997,Ford,E350,"ac, abs, moon",3000.00
 1999,Chevy,"Venture ""Extended Edition""","",4900.00
 1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
 1996,Jeep,Grand Cherokee,"MUST SELL!

Synchronous

Lua code to read CSV example above synchronously.

data = static_data_load("cars.csv")
-- Print all cars
for key,value in pairs(data) do
  print(value["Make"] .. " " .. value["Model"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
end

Asynchronous

Lua code to read CSV example above asynchronously.

static_data_load("cars.csv", function(data)
  -- Print all cars
  for key,value in pairs(data) do
    print(value["Make"] .. " " .. value["Model"] .. " from " .. value["Year"] .. ", now only for " .. value["Price"] .. "!")
  end
end)

Example (Text)

Example cars.txt file:

 Ford
 Chevy
 Jeep

Synchronous

Lua code to read Txt example above synchronously. Data will be split in lines. The new line character can be either \r\n (Windows) or \n (Unix).

data = static_data_load("cars.txt")
-- Print all cars
for key, value in pairs(data) do
  print("line " .. key .. "has car: " .. value)
end

Asynchronous

Lua code to read Txt example above asynchronously. Data will be split in lines. The new line character can be either \r\n (Windows) or \n (Unix).

static_data_load("cars.txt", function(data)
  -- Print all cars
  for key, value in pairs(data) do
    print("line " .. key .. "has car: " .. value)
  end
end)