mirror of
https://github.com/halaxa/json-machine.git
synced 2025-01-16 20:48:17 +01:00
Recursive focused performace optimizations. Ops outside the main foreach in Parser now matter as new Parser is created for each level
This commit is contained in:
parent
1df75dd782
commit
21bea751d0
@ -21,6 +21,11 @@ class ExtJsonDecoder implements ItemDecoder
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var self
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
public function __construct($assoc = false, $depth = 512, $options = 0)
|
||||
{
|
||||
$this->assoc = $assoc;
|
||||
@ -37,4 +42,13 @@ class ExtJsonDecoder implements ItemDecoder
|
||||
|
||||
return new ValidResult($decoded);
|
||||
}
|
||||
|
||||
public static function instance(): self
|
||||
{
|
||||
if ( ! self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,11 @@ class StringOnlyDecoder implements ItemDecoder
|
||||
/** @var ItemDecoder */
|
||||
private $innerDecoder;
|
||||
|
||||
/**
|
||||
* @var self
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
public function __construct(ItemDecoder $innerDecoder)
|
||||
{
|
||||
$this->innerDecoder = $innerDecoder;
|
||||
@ -22,4 +27,13 @@ class StringOnlyDecoder implements ItemDecoder
|
||||
|
||||
return new ValidResult($jsonValue);
|
||||
}
|
||||
|
||||
public static function instance(ItemDecoder $innerDecoder): self
|
||||
{
|
||||
if ( ! self::$instance) {
|
||||
self::$instance = new self($innerDecoder);
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ class Parser implements \IteratorAggregate, PositionAware
|
||||
private $recursive;
|
||||
|
||||
/** @var array */
|
||||
private static $allBytes;
|
||||
private static $tokenTypes;
|
||||
|
||||
/**
|
||||
* @param array|string $jsonPointer Follows json pointer RFC https://tools.ietf.org/html/rfc6901
|
||||
@ -97,10 +97,15 @@ class Parser implements \IteratorAggregate, PositionAware
|
||||
throw new InvalidArgumentException('$tokens must be either an instance of Iterator or IteratorAggregate.');
|
||||
}
|
||||
|
||||
$this->jsonDecoder = $jsonDecoder ?: new ExtJsonDecoder();
|
||||
if ($recursive) {
|
||||
$this->jsonDecoder = new StringOnlyDecoder($this->jsonDecoder);
|
||||
if ($jsonDecoder instanceof StringOnlyDecoder) {
|
||||
$this->jsonDecoder = $jsonDecoder;
|
||||
} else {
|
||||
$this->jsonDecoder = $jsonDecoder ?: new ExtJsonDecoder();
|
||||
if ($recursive) {
|
||||
$this->jsonDecoder = new StringOnlyDecoder($this->jsonDecoder);
|
||||
}
|
||||
}
|
||||
|
||||
$this->recursive = $recursive;
|
||||
}
|
||||
|
||||
@ -127,11 +132,11 @@ class Parser implements \IteratorAggregate, PositionAware
|
||||
*/
|
||||
private function createGenerator(): Generator
|
||||
{
|
||||
if ( ! self::$allBytes) {
|
||||
self::$allBytes = $this->tokenTypes();
|
||||
if ( ! self::$tokenTypes) {
|
||||
self::$tokenTypes = $this->tokenTypes();
|
||||
}
|
||||
|
||||
$tokenTypes = self::$allBytes;
|
||||
$tokenTypes = self::$tokenTypes;
|
||||
|
||||
$iteratorStruct = null;
|
||||
$currentPath = &$this->currentPath;
|
||||
@ -333,9 +338,6 @@ class Parser implements \IteratorAggregate, PositionAware
|
||||
$generator = $this->getIterator();
|
||||
|
||||
while ($generator->valid()) {
|
||||
// var_dump(is_object($generator->current()) ? get_class($generator->current()) : $generator->current());
|
||||
// $generator->key();
|
||||
// $generator->current();
|
||||
$generator->next();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user