Skip to content
TheFlyingFiddle edited this page Jul 11, 2015 · 11 revisions

In this tutorial we will see how to decode an integer stored in the TIER format. The decoding process is very similar to the encoding process highlighted in the Encoding a value tutorial.

We open the file that contains our encoded integer. This is the same file we encoded the integer to in the previous tutorial.

local input_stream = assert(io.open("Encoded.dat", "rb"))

Then we create a decoder object and a reader that reads from that file.

local reader  = tier.reader(input_stream)
local decoder = tier.decoder(reader)

And a mapping from int32 to a lua number. It's important that we use the same mapping for decoding as we did for encoding. Otherwise we would not read the value correctly.

local mapping = tier.primitive.int32

Finally we decode the integer and make sure that the meaning of life is still 42

local the_meaning_of_life = decoder:decode(mapping)
assert(the_meaning_of_life == 42)

It is also proper to close the file and decoder after we have used them.

decoder:close()
input_stream:close()

This completes this tutorial. You should now be able to both encode and decode simple values with the api. In the next tutorial we will get a more indepth view of the encoding api. In the Basic API walkthrough tutorial.

###Full code of the tutorial

local tier = require"tier"

local input_stream = assert(io.open("Encoding.dat", "rb"))
local reader       = tier.reader(input_stream)
local decoder      = tier.decoder(reader)

local mapping = tier.primitive.int32
local the_meaning_of_life = decoder:decode(mapping)
assert(the_meaning_of_life == 42)

Clone this wiki locally