Skip to content

JSON Examples

NebelFox edited this page Feb 18, 2022 · 2 revisions

styles.json

{
    "styles": {
        "common": {
            "dialogue": {
                "greeting": "Hello world!",
                "farewell": "See you soon"
            }
        },
        "guest": {
            "base": "common",
            "dialogue": {
                "greeting": "Welcome to authorization menu",
                "prompt-indicator": "[guest]~$ "
            }
        }
    }
}

boxes.json

{
    "boxes": {
        "common": {
            "style": "common",
            "commands": [
                {
                    "name": "exit",
                    "brief": "terminates the program"
                },
                {
                    "name": "help",
                    "brief": "shows help for a specific command/namespace",
                    "params": "name..."
                }
            ]
        },
        "guest": {
            "base": "common",
            "style": "guest",
            "insert": [
                {
                    "at": "begin",
                    "commands": [
                        {
                            "name": "login",
                            "brief": "authorize via specific credentials",
                            "params": [
                                "login",
                                "password"
                            ]
                        },
                        {
                            "name": "register",
                            "brief": "register a new account via specific credentials",
                            "params": [
                                "login",
                                "password"
                            ]
                        }
                    ]
                }
            ]
        },
        "user": {
            "base": "common",
            "style": {
                "base": "common",
                "dialogue": {
                    "greeting": "Welcome, user",
                    "prompt-indicator": "[user]~$ "
                }
            },
            "insert": [
                {
                    "before": "help",
                    "commands": [
                        "do-nothing",
                        {
                            "name": "print",
                            "commands": ["username"]
                        },
                        {
                            "name": "change-password",
                            "params": [
                                "current-password",
                                "new-password",
                                "repeat-new-password"
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using Verbox;
using Verbox.Text.Serialization;

namespace Foo
{
    class Program
    {
        private static void Main()
        {
            var serializer = new Serializer();
            serializer.Deserialize("styles.json");
            serializer.Deserialize("boxes.json");
            BoxBuilder builder = serializer.Boxes["user"];
            builder.SetActionsByPaths(new Dictionary<string, Action<Context>>
            {
                ["change-password"] = context => Console.WriteLine("Failed to change a password"),
                ["print username"] = _ => Console.WriteLine("User"),
                ["help"] = context => context.Box.Execute($"{string.Join(' ', (object[])context["name"])} --help"),
                ["exit"] = context => context.Box.Terminate()
            });
            builder.SetMissingActionsToDummy(); // sets action for "do-nothing"
            builder.Build().StartDialogue();
        }
    }
}
Clone this wiki locally