Index

JsonParser

MMO\core · 1 symbol · generated 5 Oct 2026

class

JsonParser

JsonParser.php:14

A hand-written, tolerant JSON parser (Decode/DecodeFile): accepts standard JSON, plus a few lenient extensions with a warning logged (not thrown) -- single-quoted strings, and unquoted barewords as string values. Malformed input (that isn't one of those tolerated leniencies) throws eJson.

class MMO\core\JsonParser extends MMO\core\classes\aParser
Namespace
MMO\core
Extends
aParser
JsonParser::Decode('{"a": 1, \'b\': 2}'); // ['a' => 1, 'b' => 2] -- single quotes tolerated

Index

WHITE_SPACES
filter
addError($token, $message, $context)
addWarning($token, $message, $context)A non-fatal parse issue (aParser::addError -- just logs/accumulates, doesn't throw), for tolerated leniencies (decodeText/decodeValue).
createTokenizer()
Decode($text, $callback) staticParse json
decodeAny()Decode one JSON value of any type, dispatching on the current token: {//quote start a nested object/array/string, anything else is a bare value ([decodeValue).
decodeArray()Decode a [...] array (the tokenizer's current position is just past the opening [).
DecodeFile($filename, $callback) static
decodeObject()Decode a {...} object (the tokenizer's current position is just past the opening {).
decodeText()Decode a quoted string token (starting at the tokenizer's current position, a ' or " token) into its real string value, resuming the tokenizer past its closing quote.
decodeValue($token)Decode a bare (unquoted) value token: a number, true/false/null (case-insensitively), or -- tolerated with a warning, not valid JSON -- an unquoted bareword, returned as a plain string.
process($opts)

Constants1

WHITE_SPACES

const WHITE_SPACES = [' ', ' ', ' ', ' ']

Properties1

filter

protected $filter = null

Methods11

addError($token, $message, $context)

JsonParser.php:54
protected function addError(?MMO\core\helpers\Token $token, string $message, array $context = []): string
See
aParser::addError -- overridden to be fatal: unlike the base parser (which just logs and accumulates errors, tolerating malformed input), any real syntax error immediately aborts parsing.
$tokennull | Token
$messagestring
$context = []array
returnsstring
throwsalways.

createTokenizer()

JsonParser.php:66
function createTokenizer(): MMO\core\helpers\Tokenizer
See
aParser::createTokenizer -- recognizes '/"-quoted strings, [...]/{...} groups (each nestable inside the other), and a ':' token inside objects.
returnsTokenizer

Decode($text, $callback)

staticJsonParser.php:29

Parse json

static function Decode($text, ?callable $callback = null): mixed
$textthe text or resource to read from
$callback = nullnull | callablefunction(array $data, Tokenizer $tokenizer): mixed -- called once per array/object found (innermost first), with $data being that structure's already-decoded content; its return value replaces that structure in the final result (e.g. to turn a decoded object into a real class instance).
returnsmixed
throwsif $text isn't valid (tolerantly-extended) JSON.

decodeArray()

JsonParser.php:193

Decode a [...] array (the tokenizer's current position is just past the opening [).

protected function decodeArray()
returnsthe decoded list, passed through $filter if set.
throwson any syntax error (missing ,, or unterminated array).

DecodeFile($filename, $callback)

staticJsonParser.php:39
static function DecodeFile(string $filename, ?callable $callback = null): mixed
$filenamestring
$callback = nullnull | callableDecode.
returnsmixed
throwsif $filename can't be opened.
throwsif its content isn't valid JSON.

decodeObject()

JsonParser.php:160

Decode a {...} object (the tokenizer's current position is just past the opening {).

protected function decodeObject()
returnsthe decoded array (string keys => decoded values), passed through $filter if set.
throwson any syntax error (missing :/,, or unterminated object).

decodeText()

JsonParser.php:118

Decode a quoted string token (starting at the tokenizer's current position, a ' or " token) into its real string value, resuming the tokenizer past its closing quote. Single-quoted strings are tolerated (with a warning -- the class docblock) and converted to an equivalent double-quoted form.

protected function decodeText(): string
returnsstring

decodeValue($token)

JsonParser.php:98

Decode a bare (unquoted) value token: a number, true/false/null (case-insensitively), or -- tolerated with a warning, not valid JSON -- an unquoted bareword, returned as a plain string.

protected function decodeValue(MMO\core\helpers\Token $token): mixed
$tokenToken
returnsmixed
Inherited members (56)
ConstantsDeclared byDescription
ALERTiLogLevelAlert: Action must be taken immediately.
ALLiLogLevelMask for all
ALL_PLUSiLogLeveldebug everything
BACKTRACEiLogLevel
CRITICALiLogLevelCritical: Critical conditions.
DEBUGiLogLevelDebug: Detailed debug information.
DEEPiLogLeveldebug everything with arguments
EMERGENCYiLogLevelEmergency: System is unusable.
ERRORiLogLevelError: Runtime errors that do not require immediate action but should typically be logged and monitored.
ERROR_PLUSiLogLevelMask for all error
FATALiLogLevelMask for all fatal error
INFOiLogLevelInfo: Interesting events.
INFO_PLUSiLogLevelMask for all errors/warning/notices/info
MEMORYiLogLevelinclude backtrace object
NOTICEiLogLevelNotice: Normal but significant events.
NOTICE_PLUSiLogLevelMask for all errors/warning/notices/info
TRACEiLogLevelinclude backtrace as origins
WARNINGiLogLevelWarning: Exceptional occurrences that are not errors.
WARNING_PLUSiLogLevelMask for all errors and warnings
PropertiesDeclared byDescription
errorsaParser
lockaParser
tokenizeraParser
verbosityaModule
MethodsDeclared byDescription
alert($message, $context)aModuleAction must be taken immediately.
backtraces($message, $context)aModuleForce-log a message at INFO level together with a full stack trace/memory snapshot (iLogLevel::BACKTRACE), bypassing the verbosity mask entirely (any non-zero verbosity is enough to trigger it).
Configure($config)aModuleMerge $config into this module's configuration in the shared Configuration.
critical($message, $context)aModuleCritical conditions. No-op if CRITICAL is currently filtered out by the verbosity mask.
debug($message, $context)aModuleDetailed debug information.
debugName()aModuleHuman-readable identifier used as the default {origin} in this instance's log messages.
DefaultConfig()aModuleThe config a module starts from before anything loaded via Configuration is merged in.
emergency($message, $context)aModuleSystem is unusable. No-op if EMERGENCY is currently filtered out by the verbosity mask.
error($message, $context)aModuleRuntime errors that do not require immediate action but should typically be logged and monitored.
getErrors()aParser
getTokenizer()aParser
getVerbosity()aModule
hasErrors()aParser
info($message, $context)aModuleInteresting events. No-op if INFO is currently filtered out by the verbosity mask.
Instantiate($config)aModule(Re)create the shared instance immediately, configured with $config merged into the class's existing config.
isLocked()aParser
IsShared()aModule
log($level, $message, $context)aModuleLogs with an arbitrary level.
notice($message, $context)aModuleNormal but significant events.
offsetExists($key)aModule\ArrayAccess::offsetExists -- tells whether $key is a declared property at all, regardless of whether it is currently readable/writable through array access.
offsetGet($key)aModule\ArrayAccess::offsetGet -- reads a property directly, unless it has a #[Serialize(serialize: 'methodName')] attribute, in which case that zero-argument method is called instead (e.g.
offsetSet($key, $value)aModule\ArrayAccess::offsetSet -- sets a property directly, unless it has a #[Serialize(unserialize: 'methodName')] attribute, in which case that method is called with $value instead (e.g.
offsetUnset($key)aModule\ArrayAccess::offsetUnset -- equivalent to $module[$key] = null.
parse($text, $opts)aParserParse $text: resets getErrors, (re)builds/reuses the tokenizer, runs process, then always clears the tokenizer's tokens from memory (even if process() throws).
serialize($path, $serializer)aModule\MMO\core\interfaces\iSerializable::serialize -- exports every property (via
setVerbosity($verb)aModuleSet the current verbosity mask, replacing whatever it was before.
Shared()aModuleGet the shared instance, creating (and configuring) it on first call.
testVerbosity($level)aModuleTest whether a given level (or set of levels) is currently enabled by this instance's verbosity mask.
traitUnshare($serialize)aModuleRemove the shared instance, so the next Shared() call creates a fresh one.
unserialize($data, $path, $instances)aModule\MMO\core\interfaces\iSerializable::unserialize -- merges $data onto DefaultConfig, runs it through the (possibly overridden) constructor, then applies each resulting value to its property (via
Unshare($serialize)aModuleLike tSingleton::Unshare, but also disconnects this module's instance from every event it was listening to (via Event::DisconnectListeners) before dropping it.
warning($message, $context)aModuleExceptional occurrences that are not errors.
__construct($config)aModuleprotected constructor, so it is not possible to do new Module(...) called by the serialization process just before being injected with data.