Skip to content
/ BJSON Public

Simple and Fast Json Serializer/Deserializer for Beeflang. Focused on simple usage and memory safety.

Notifications You must be signed in to change notification settings

M0n7y5/BJSON

Repository files navigation

BJSON

A high-performance JSON serializer and deserializer for the Beef programming language.

Features

  • RFC 8259 compliant
  • Result-based error handling
  • Pretty-print support
  • Stream-based parsing
  • Comment support (optional)
  • Optimized performance

Installation

  • Clone the repository or download the latest release from the Releases page
  • Add BJSON to your workspace via IDE

Known Issues

⚠️ Be sure you have latest nightly of Beef IDE installed due to some syntax not working properly in older versions. This is now fixed in the compiler.

Usage

Basic Deserialization

let jsonString = "{\"name\":\"BJSON\",\"version\":1.0}";
var result = Json.Deserialize(jsonString);
defer result.Dispose();

if (result case .Ok(let jsonValue))
{
    // we expect object
    if (let root = jsonValue.AsObject())
    {
        if (StringView name = root.GetValue("name"))
            Console.WriteLine(name);
    }
}
else if (result case .Err(let error))
{
    Console.WriteLine("Error: {}", error);
}
//Note: You can also use switch case statement as well

Basic Serialization

let json = JsonObject()
    {
        ("firstName", "John"),
        ("lastName", "Smith"),
        ("isAlive", true),
        ("age", 27)
    };
defer json.Dispose();

let output = scope String();
Json.Serialize(json, output);
Console.WriteLine(output);

Pretty-Print

let json = JsonObject()
    {
        ("firstName", "John"),
        ("lastName", "Smith"),
        ("isAlive", true),
        ("age", 27),
        ("phoneNumbers", JsonArray()
            {
                JsonObject()
                    {
                        ("type", "home"),
                        ("number", "212 555-1234")
                    },

                JsonObject()
                    {
                        ("type", "office"),
                        ("number", "646 555-4567")
                    }
            })
    };
defer json.Dispose();
let output = scope String();
let options = JsonWriterOptions() { Indented = true };
Json.Serialize(json, output, options);
Console.WriteLine(output);

Comment Support

Enable JSONC (JSON with Comments) parsing:

// JSONC (JSON with Comments)
var config = DeserializerConfig() { EnableComments = true };
var deserializer = scope Deserializer(config);

let jsonWithComments = """
{
    // Single-line comment
    "setting": "bing bong",
    /* Multi-line comment */
    "enabled": true
}
""";

var result = deserializer.Deserialize(jsonWithComments);
defer result.Dispose();

if (result case .Ok(let val))
{
    /* YOLO Errors
    StringView settings = val["setting"];
    Console.WriteLine(scope $"Settings value: {settings}");
    */

    // or safer way
    if (let root = val.AsObject())
    {
        if (StringView test = root.GetValue("setting"))
        {
            Console.WriteLine(test);
        }
    }
}
else if (result case .Err(let err))
{
    Console.WriteLine(err);
}

API Reference

Main Classes

  • Json - Static methods for serialization and deserialization
  • Deserializer - Configurable JSON parser
  • JsonWriter - Output serialization
  • JsonReader - Stream-based input parsing

JsonValue Types

  • JsonNull
  • JsonBool
  • JsonNumber
  • JsonString
  • JsonArray
  • JsonObject

Configuration

DeserializerConfig

  • EnableComments - Allow C-style comments (default: false)

JsonWriterOptions

  • Indented - Enable pretty-printing (default: false)
  • IndentString - Indentation string (default: " ")

Testing

The library includes comprehensive test suites covering RFC compliance, edge cases, and error handling.

Running Tests

BeefBuild.exe -test

Or build the test project via IDE.

Test suites include:

  • JSON.org standard tests
  • Native JSON benchmark roundtrips
  • NST JSON test suite
  • Big List of Naughty Strings

License

MIT License

About

Simple and Fast Json Serializer/Deserializer for Beeflang. Focused on simple usage and memory safety.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages