rdx/jsonpathstreamer

1.0 2017-11-20 11:24 UTC

This package is auto-updated.

Last update: 2024-08-31 00:31:15 UTC


README

Build Status Scrutinizer Code Quality

使用 salsify/jsonstreamingparser 解析 JSON 文件,并流式传输其标记。此包为此过程添加了功能。

在一个非常大的 JSON 文件中,知道解析器的位置非常重要,以了解哪些部分需要保留。它为此提供了一个接口。对于非常简单的 JSON 解析,还有一个可配置的方法,无需任何更多的解析/JSON 格式化逻辑。

查看 examples/ 获取更多示例。运行 examples/speed.php 进行速度比较。

DIY - 手术精度

// MUST implement gotPath() and gotValue()
class MyListener extends \rdx\jsonpathstreamer\PathAwareJsonListener {
	public function gotPath(array $path) {
		// Ignore valueless paths (empty arrays etc)
	}

	public function gotValue(array $path, $value) {
		// Save only values within {"foo": {"bar": {...}}}
		if (array_slice($path, 0, 2) == ['foo', 'bar']) {
			// Ignore long "description" texts
			if (end($path) != 'description') {
				$this->rememberValue(array_slice($path, 2), $value);
			}
		}
	}

	// Optional
	public function stopAfter() {
		// Stop parsing after foo/bar because there's nothing I want there
		return ['#foo/bar/#'];
	}
}

可配置 - 简单

// MUST implement getRules()
class MyListener extends \rdx\jsonpathstreamer\RegexConfigJsonListener {
	public function getRules() {
		// Save only "name", for all users into their original position
		return [
			'#^users/[^/]+/(name)(/|$)#',
			'#^offices/[^/]+/(name)(/|$)#',
		];
	}
}

可配置 - 转换

// MUST implement getRules()
class MyListener extends \rdx\jsonpathstreamer\RegexTargetConfigJsonListener {
	public function getRules() {
		// Save only "name", for all users and offices, into the same list
		return [
			'#^users/([^/]+)/(name)(/|$)#' => 'entities/$1/$2',
			'#^offices/([^/]+)/(name)(/|$)#' => 'entities/$1/$2',
		];
	}
}