Hjson, the Human JSON. A configuration file format for humans. Relaxed syntax, fewer mistakes, more comments.
{
# specify rate in requests/second (because comments are helpful!)
rate: 1000
// prefer c-style comments?
/* feeling old fashioned? */
# did you notice that rate doesn't need quotes?
hey: look ma, no quotes for strings either!
# best of all
notice: []
anything: ?
# yes, commas are optional!
}
Tested on .net & Mono.
The C# implementation of Hjson is based on System.Json. For other platforms see hjson.org.
Install-Package Hjson
You can either
- use this libary directly
- or just convert Hjson to JSON and use it with your favorite JSON library.
// convert Hjson to JSON
var jsonString = HjsonValue.Load(filePath).ToString();
// convert JSON to Hjson
var hjsonString = JsonValue.Load("test.json").ToString(Stringify.Hjson);
var jsonObject = HjsonValue.Load(filePath).Qo();
HjsonValue.Load()
will accept both Hjson and JSON. You can use JsonValue.Load()
to accept JSON input only.
var jsonObject = HjsonValue.Parse("{\"name\":\"hugo\",\"age\":5}").Qo();
string name = jsonObject.Qs("name");
int age = jsonObject.Qi("age");
// or iterate over the members
foreach (var item in jsonObject)
{
Console.WriteLine("{0}: {1}", item.Key, item.Value);
}
var jsonArray = HjsonValue.Parse("[\"hugo\",5]").Qa();
string first = jsonArray[0];
// or iterate over the members
foreach (var item in jsonArray)
Console.WriteLine(item.ToValue());
var nested = HjsonValue.Parse("{\"partner\":{\"name\":\"John\",\"age\":23}}").Qo();
string name = nested.Qo("partner").Qs("name", "default");
int age = nested.Qo("partner").Qi("age", 77);
string gender = nested.Qo("partner").Qs("gender", "unknown");
var jsonObject = new JsonObject
{
{ "name", "John" },
{ "age", 23 },
};
// -> { "name": "John", "age", 23 }
JsonArray jsonArray = new JsonArray()
{
"John",
23,
};
// -> [ "John", 23 ]
jsonObject["name"] = "Hugo";
jsonObject.Remove("age");
HjsonValue.Save(jsonObject, "file.hjson"); // as Hjson
HjsonValue.Save(jsonObject, "file.json"); // as JSON
jsonObject.ToString(Stringify.Hjson); // Hjson output
jsonObject.ToString(Stringify.Formatted); // formatted JSON output
jsonObject.ToString(Stringify.Plain); // plain JSON output, default
jsonObject.ToString(); // plain
Also see the sample.
See api.md.
A commandline tool to convert from/to Hjson is available in the cli folder.
You can also install it from chocolatey.
For other tools see hjson.org.