hxjson5

A port of Json5 to Haxe.
https://git.gay/Rayn/hxjson5

To install, run:

haxelib install hxjson5 1.1.0 

See using Haxelib in Haxelib documentation for more information.

README.md

HxJson5

A port of Json5 to Haxe.

Installation

via haxelib

Link: https://lib.haxe.org/p/hxjson5

haxelib install hxjson5

via Git

haxelib git hxjson5 https://git.gay/Rayn/hxjson5.git

Usage

import hxjson5.Json5;

var json5 = '{
  // comments
  unquoted: \'and you can quote me on that\',
  singleQuotes: \'I can use "double quotes" here\',
  lineBreaks: "Look, Mom! \\
No \\\\n\'s!",
  hexadecimal: 0xdecaf,
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  positiveSign: +1,
  negativeSign: -1,
  trailingComma: \'in objects\', andIn: [\'arrays\',],
  "backwardsCompatible": "with JSON",
}';
var json = Json5.parse(json5);

To have custom serialization you need to do this: - Define json5_circular in your compiler arguments then do the following: - For a anonymous structure, you need to set the toJSON5 or toJSON fields to a function with the signature function(key:String, value:Dynamic, holder:Dynamic):Dynamic. Do not remove the arguments, it is required, and causes instabilities if you remove it. - For a class, you have to implement the interface hxjson5.Stringifier.IJson5Serializable or hxjson5.Stringifier.IJsonSerializable

If you want circular references to throw instead of freezing the program, you need to define json5_circular in your compiler arguments. (fyi: haxe.Json.stringify does not check for circular references), Though, it reduces the performance of stringifying.

If you want to enable the toJSON5 and toJSON methods, you need to define json5_tojson5 in your compiler arguments. (fyi: haxe.Json.stringify does not have any toJSON methods), Enabling this will reduce the performance of stringifying.

Performance

Json5.parse is slower by ~2.05x compared to haxe.Json.parse. Json5.stringify is slower by ~1.23x compared to haxe.Json.stringify.

These tests were done without the following defines: json5_circular, json5_tojson5.

Whilst the performance hit is medium, json5 still offers much more flexability, if you are parsing handwritten json you should use hxjson5. If you are parsing computer generated json, you should use haxe.Json.

If you wanna run these benchmarks yourself run haxe benchmark.hxml. (It runs without the defines)

Possible future improvements: - Rewrite the parser to use recursion instead of a stack

Comparison with other libraries

These benchmarks were done without the following defines: json5_circular, json5_tojson5. Using the benchmark json file in tests/benchmark.json.

Fyi whilst tink_json is the fastest, including it requires the entire tink dependency to be compiled.

You can run the benchmarks yourself by running haxe benchmark-thirdparty.hxml or haxe benchmark-thirdparty-debug.hxml.

To generate the images yourself, run cd tests && python scripts/benchmark.py

Notices about compatibility

If you encounter any issues, please open an issue.

Keep in mind that there are some cross platform issues which cause some tests to fail. If you wanna help with fixing the issues, you can find the broken tests in tests/src/Main.hx folder. The issues are marked with a #if

Another thing to note is that, unicode is currently not supported. Sorry for the inconvenience. It is gonna be supported in the future, but for now i haven't found a way to do it.

Example:

Json5.stringify({a: 1, b: 2}, function(k:String, v:Dynamic, o:Dynamic):Dynamic {
	return (k == 'a') ? 2 : v;
});

or use a arrow function (not recommended): because you have to cast the return value to Dynamic. due to the way Haxe compiles it, only for HashLink, C++, C# and Java though. The rest are fine.

Json5.stringify({a: 1, b: 2}, (k, v, o) -> (k == 'a') ? (2 : Dynamic) : (v : Dynamic));

Targets that haven't been tested yet: - Cppia - Haxe 3.x.x

Contributors
Rayn
Version
1.1.0
Published
2 months ago
License
MIT

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub