A high-performance JSON serializer and deserializer for the Beef programming language.
- RFC 8259 compliant
- Result-based error handling
- Pretty-print support
- Stream-based parsing
- Comment support (optional)
- Optimized performance
- Clone the repository or download the latest release from the Releases page
- Add BJSON to your workspace via IDE
⚠️ 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.
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 welllet json = JsonObject()
{
("firstName", "John"),
("lastName", "Smith"),
("isAlive", true),
("age", 27)
};
defer json.Dispose();
let output = scope String();
Json.Serialize(json, output);
Console.WriteLine(output);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);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);
}Json- Static methods for serialization and deserializationDeserializer- Configurable JSON parserJsonWriter- Output serializationJsonReader- Stream-based input parsing
JsonNullJsonBoolJsonNumberJsonStringJsonArrayJsonObject
DeserializerConfig
EnableComments- Allow C-style comments (default: false)
JsonWriterOptions
Indented- Enable pretty-printing (default: false)IndentString- Indentation string (default: " ")
The library includes comprehensive test suites covering RFC compliance, edge cases, and error handling.
BeefBuild.exe -testOr 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
MIT License
