How to use Fostrian

Fostrian is a file format that instead of stroing data as text, Fostrian stores it as binary. This makes Fostrian to have really small files while storing the same information.

To use Fostrian, first we need it.

The official NuGet package of Fostrian is available and we can use either the NuGet Package Store (in Visual Studio) or dotnet add package (in everything else, including VSCode). Here’s the link.

After adding Fostrian to our .NET project, we can now use it in our code.

Add the reference to Fostrian by opening your class file and:

  • for Visual Basic: add Imports LibFoster to top
  • for C#: add using LibFoster; to top
  • for F#: add open LibFoster to top

After that, the real fun is getting started.

C#
// Let's generate a root node to start with.
        var rootNode = Fostrian.GenerateRootNode();

        // Set an encoding for our strings. Also we should set our start and end bytes.
        rootNode.Encoding = System.Text.Encoding.UTF8;

        // Fun fact: these values are actually from UTF-8 standard.
        rootNode.StartByte = 0x02;
        rootNode.EndByte = 0x03;

        // Let's add a number.
        rootNode.Add(7);

        // Let's add another node but also get that created node.

        var myNode = rootNode.Add("Hello Fostrian!");

        // We can add nodes inside of nodes! And we can add booleans too!

        myNode.Add(true);

        // We can chain these as well!

        var chainEndNode = rootNode.Add("Chain Start!").Add(1).Add('f').Add("Chain end!");

        Console.WriteLine(chainEndNode.DataAsString);

        // Now we have this nodes:
        /*
            - Root Node
               - 7
               - "Hello Fostrian!"
                  - true
               - "Chain Start!"
                  - 1
                    - f
                      - "Chain End!"

        */

        // File itself

        var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Fostrian.fff");

        // Let's save our Fostrian nodes.

        rootNode.Recreate(fileName);

        // and load it back!

        var myLoadedRoot = Fostrian.Parse(fileName);

        // Let's read some nodes.

        Console.WriteLine(myLoadedRoot[0].DataAsInt32);
        Console.WriteLine(myLoadedRoot[1].DataAsString);

        // If you try to read a value that isn't what you think it is, Fostrian will throw an exception.

        try
        {
            // Let's try to load our "Chain Start!" node as a char.
            Console.WriteLine(myLoadedRoot[2].DataAsChar);
        }
        catch (Exception ex)
        {
            // It will throw an exception, so let's print the entire thing up!
            Console.WriteLine(ex.ToString());
        }
Scroll to Top