class JsonFile (View source)

Reads/writes json files.

Constants

LAX_SCHEMA

STRICT_SCHEMA

JSON_UNESCAPED_SLASHES

JSON_PRETTY_PRINT

JSON_UNESCAPED_UNICODE

Methods

__construct(string $path, RemoteFilesystem $rfs = null, IOInterface $io = null)

Initializes json file reader/parser.

string
getPath()

No description

bool
exists()

Checks whether json file exists.

mixed
read()

Reads json file.

write(array $hash, int $options = 448)

Writes json file.

bool
validateSchema(int $schema = self::STRICT_SCHEMA)

Validates the schema of the current json file according to composer-schema.json rules

static string
encode(mixed $data, int $options = 448)

Encodes an array into (optionally pretty-printed) JSON

static mixed
parseJson(string $json, string $file = null)

Parses json string and returns hash.

static bool
validateSyntax(string $json, string $file = null)

Validates the syntax of a JSON string

Details

__construct(string $path, RemoteFilesystem $rfs = null, IOInterface $io = null)

Initializes json file reader/parser.

Parameters

string $path path to a lockfile
RemoteFilesystem $rfs required for loading http/https json files
IOInterface $io

Exceptions

InvalidArgumentException

string getPath()

Return Value

string

bool exists()

Checks whether json file exists.

Return Value

bool

mixed read()

Reads json file.

Return Value

mixed

Exceptions

RuntimeException

write(array $hash, int $options = 448)

Writes json file.

Parameters

array $hash writes hash into json file
int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)

Exceptions

Exception

bool validateSchema(int $schema = self::STRICT_SCHEMA)

Validates the schema of the current json file according to composer-schema.json rules

Parameters

int $schema a JsonFile::*_SCHEMA constant

Return Value

bool true on success

Exceptions

JsonValidationException

static string encode(mixed $data, int $options = 448)

Encodes an array into (optionally pretty-printed) JSON

Parameters

mixed $data Data to encode into a formatted JSON string
int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)

Return Value

string Encoded json

static mixed parseJson(string $json, string $file = null)

Parses json string and returns hash.

Parameters

string $json json string
string $file the json file

Return Value

mixed

static protected bool validateSyntax(string $json, string $file = null)

Validates the syntax of a JSON string

Parameters

string $json
string $file

Return Value

bool true on success

Exceptions

UnexpectedValueException
ParsingException