Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1491 (assigned enhancement)

Opened 6 years ago

Last modified 5 years ago

new JSON parser implementation

Reported by: mwarning Assigned to: kris (accepted)
Priority: major Milestone: 2.0
Component: Tango Version: 0.99.7 Dominik
Keywords: Cc:

Description (Last modified by mandel)

I wrote a new JSON parser in hope to replace the current implementation with a better one.

Features:

  • fast
  • query chaining
  • custom allocators
  • can handle single quoted strings (template parameter)
  • can handle comments (template parameter)
  • fixes json tickets #1283, #1274, #1252 (sort of)
  • unittests

Use case:

auto pa = new JsonParser!();
auto root = pa.parseObject(`{ "a":{"a" : 0.1e23, "b" : [true, false, null]}}`);

alias builder.JsonString JsonString;
alias builder.JsonObject JsonObject;
alias builder.JsonValue JsonValue;

auto obj = new JsonObject();

obj["array"] = [cast(char[]) "a" : cast(uint[]) [1,2,3], "b" : [42]];
obj["string"] = "abc";
obj["number"] = 42.1234;
obj["bools"] = [true, false];
obj["numbers"] = [3, 1234567890];
obj["numbers"] ~= [42, 128];
obj["one bool"] = true;
obj["object"] = root;

assert(obj["array"]["b"][0] == 42);
assert(obj["string"] == "abc");
assert(obj["number"] == 42.1234);
assert(obj["string"].toString == "abc");
assert(obj["number"].toFloat == 42.1234);

if(obj["array"]["b"][0].ptr) { /* item exists */ }
if(!obj["array"]["21"][42][0].ptr) { /* item does not exist */ }

obj.print((char[] str){ /* Stdout(str); */ }, true); //formatted ouput
obj.print((char[] str){ /* Stdout(str); */ }, false); //compact output

foreach(char[] key, JsonValue val; obj)
{
	if(key == "integers")
	{
		uint[] integers = val.to!(uint[]);
		assert(integers == ia_test);
	}
}

The implementation consist of:

Change History

02/18/09 19:09:08 changed by mandel

Things that need to be done:

  • documentation (beat me)
  • design review, it isn't all shiny (kris?)
  • the printer stuff could be moved into JsonPrinter?.d

02/18/09 19:13:10 changed by mandel

  • description changed.

02/18/09 19:14:36 changed by mandel

* adapting to Tango style allocators and containers

02/19/09 04:19:49 changed by larsivi

  • type changed from defect to enhancement.

02/22/09 12:29:28 changed by mandel

Here are some benchmarks for dmd/gdc/ldc. They were better before.
As far as I can tell from further testing,
added encapsulation is the cause as well as my new CPU.

GDC: (a bunch of optimization flags)
1000000 iterations, 1.49 seconds: 415.53 MB/s
Tango: 1000000 iterations, 2.05 seconds: 301.42 MB/s

LDC: rebuild main.d -O5 -release -dc=ldc-posix-tango
1000000 iterations, 2.32 seconds: 266.91 MB/s
Tango: 1000000 iterations, 3.12 seconds: 198.56 MB/s

DMD: rebuild main.d -O -release -dc=dmd-posix-tango
1000000 iterations, 3.86 seconds: 160.51 MB/s
Tango: 1000000 iterations, 3.09 seconds: 200.40 MB/s

03/01/09 20:15:07 changed by mandel

I added the BSD license (PD would also be ok), some doc and usability enhancements.

03/02/09 11:56:38 changed by mandel

  • description changed.

03/29/09 09:31:22 changed by larsivi

  • milestone changed from 0.99.8 to 0.99.9.

09/09/09 06:39:29 changed by mwarning

Location has moved:

JsonParser.d
JsonBuilder.d
JsonAdditional.d

Also fixed: #1662

11/13/09 17:14:44 changed by kris

  • status changed from new to assigned.
  • milestone changed from 0.99.9 to 2.0.