WebFioriAPI ReferenceLearnDownloadContributeLearn{{result.parent_page}} > {{result.title}}{{result.title}}Classes{{result.class_name}}{{result.summary}}Methods{{result.name}}{{result.summary}}

namespace \webfiori\json

class Json

Class Attributes Summary

const SPECIAL_CHARSAn array that contains JSON special characters.const SPECIAL_CHARS_ESCAn array that contains escaped JSON special characters.

Class Methods Summary

public function &get(string$key)Returns the value at the given key.public function __construct(array|string$initialData, boolean$isFormatted)Creates new instance of the class.public function __toString()Returns the data on the object as a JSON string.public function add(string$key, mixed$value, array$arrayAsObj)Adds a new value to the JSON string.public function addArray(string$key, array$value, boolean$asObject)Adds an array to the JSON.public function addBoolean(string$key, boolean$val)Adds a boolean value (true or false) to the JSON data.public function addMultiple(array$arr)Adds multiple values to the object.public function addNumber(string$key, int|double$value)Adds a number to the JSON data.public function addObject(string$key, JsonI|Json|object$val)Adds an object to the JSON string.public function addString(string$key, string$val)Adds a new key to the JSON data with its value as string.public static function decode(string$jsonStr)Converts a JSON-like string to JSON object.public static function escapeJSONSpecialChars(string$string)Escape JSON special characters from string.public static function fromJsonFile(string$pathToJsonFile)Reads JSON data from a file and convert it to an object of type 'Json'.public function getPropStyle()Returns the style at which the names of the properties will use.public function getProperties()Returns an array that holds all added attributes.public function getPropsNames()Returns an array that contains the names of all added properties.public function hasKey(string$key)Checks if Json instance has the given key or not.public function isFormatted()Checks if the final JSON output will be formatted or not.public function remove(string$keyName)Removes a property from the instance.public function setIsFormatted(boolean$bool)Makes the JSON output appears readable or not.public function setPropsStyle(string$style)Sets the style at which the names of the properties will use.public function toJSONString()Creates and returns a well formatted JSON string that will be created using provided data.public function toJSONxString()Creates and returns a well formatted XML string that will be created using provided data.

Class Attributes Details

const SPECIAL_CHARSAn array that contains JSON special characters. The array contains the following characters:
  • \
  • /
  • "
  • \t
  • \r
  • \n
  • \f
const SPECIAL_CHARS_ESCAn array that contains escaped JSON special characters.

Class Methods Details

public function &get(string$key)Returns the value at the given key.

Parameters:

  • string $key The value of the key. Note that the style of the key does not matter.
Returns: Json|mixed|nullThe return type will depends on the value which was set by any method which can be used to add props. It can be a number, a boolean, string, an object or null if does not exist.
public function __construct(array|string$initialData, boolean$isFormatted)Creates new instance of the class.

Parameters:

  • array|string $initialData Initial data which is used to initialize the object. It can be a string which looks like JSON or it can be an associative array. If it is an associative array, then the keys will be acting as properties and the value of each key will be the value of the property.
  • boolean $isFormatted If this attribute is set to true, the generated JSON will be indented and have new lines (readable). Note that the parameter will be ignored if the constant 'WF_VERBOSE' is defined and is set to true.
public function __toString()Returns the data on the object as a JSON string. Returns: string public function add(string$key, mixed$value, array$arrayAsObj)Adds a new value to the JSON string. This method can be used to add an integer, a double, a string, an array or an object. If null is given, the method will set the value at the given key to null. If the given value or key is invalid, the method will not add the value and will return false.

Parameters:

  • string $key The value of the key.
  • mixed $value The value of the key.
  • array $arrayAsObj This parameter is used only if the given value is an array. If set to true, the array will be added as an object. Default is false.
Returns: booleanThe method will return true if the value is set. If the given value or key is invalid, the method will return false.
public function addArray(string$key, array$value, boolean$asObject)Adds an array to the JSON.

Parameters:

  • string $key The name of the key.
  • array $value The array that will be added.
  • boolean $asObject If this parameter is set to true, the array will be added as an object in JSON string. Note that if the array is associative, each index will be added as an object. Default is false.
Returns: booleanThe method will return false if the given key is invalid or the given value is not an array.
public function addBoolean(string$key, boolean$val)Adds a boolean value (true or false) to the JSON data.

Parameters:

  • string $key The name of the key.
  • boolean $val true or false. If not specified, The default will be true.
Returns: booleanThe method will return true in case the value is set. If the given value is not a boolean or the key value is invalid string, the method will return false.
public function addMultiple(array$arr)Adds multiple values to the object.

Parameters:

  • array $arr An associative array. The keys will act as object keys in JSON and the values of the keys will be the values in JSON.
public function addNumber(string$key, int|double$value)
Adds a number to the JSON data. Note that if the given number is the constant INF or the constant NAN, The method will add them as a string. The 'INF' will be added as the string "Infinity" and the 'NAN' will be added as the string "Nan".

Parameters:

Returns: booleanThe method will return true in case the number is added. If the given value is not a number or the key value is invalid string, the method will return false.
public function addObject(string$key, JsonI|Json|object$val)
Adds an object to the JSON string. The object that will be added can implement the interface JsonI to make the generated JSON string customizable. Also, the object can be of type Json. If the given value is an object that does not implement the interface JsonI or it is not of type Json, The method will try to extract object information based on its "getXxxxx()" public methods. Assuming that the object has 2 public methods with names getFirstProp() and getSecondProp(). In that case, the generated JSON will be on the formate {"FirstProp":"prop-1","SecondProp":""}.

Parameters:

  • string $key The key value.
  • JsonI|Json|object $val The object that will be added.
Returns: booleanThe method will return true if the object is added. If the key value is invalid string, the method will return false.
public function addString(string$key, string$val)Adds a new key to the JSON data with its value as string.

Parameters:

  • string $key The name of the key. Must be non empty string.
  • string $val The value of the string.
Returns: booleanThe method will return true in case the string is added. If the given value is not a string or the given key is invalid, the method will return false.
public static function decode(string$jsonStr)Converts a JSON-like string to JSON object. Note that this method uses the function 'json_decode()' to parse the given JSON string. This means same rules which applies to 'json_decode()' applies here.

Parameters:

  • string $jsonStr A string which looks like JSON object.
Returns: array|JsonIf the given string represents A valid JSON, it will be converted to Json object and returned. Other than that, the method will return an array that contains information about parsing error. The array will have two indices, 'error-code' and 'error-message'.
public static function escapeJSONSpecialChars(string$string)Escape JSON special characters from string. If the given string is null,the method will return empty string.

Parameters:

  • string $string A value of one of JSON object properties.
Returns: stringAn escaped version of the string.
public static function fromJsonFile(string$pathToJsonFile)Reads JSON data from a file and convert it to an object of type 'Json'.

Parameters:

  • string $pathToJsonFile The full path to a file that contains JSON data.
Returns: Json|null|arrayIf the method was able to read the whole data and convert it to Json instance, the method will return an object of type Json. If the method was unable to convert file data to an object of type Json, it will return an array that contains error information. The array will have two indices, 'error-code' and 'error-message' Other than that, it will return null.
public function getPropStyle()Returns the style at which the names of the properties will use. Returns: stringThe method will return one of the following values:
  • snake
  • kebab
  • camel
  • none
The default value is 'none'.
public function getProperties()Returns an array that holds all added attributes. Returns: arrayAn array that holds objects of type 'Property'.public function getPropsNames()Returns an array that contains the names of all added properties. Note that the names may differ if properties style is changed after adding them.Returns: arrayAn array that contains the names of all added properties.public function hasKey(string$key)Checks if Json instance has the given key or not. Note that if properties style is set to 'none', the value of the key must be exactly the same as when the property was added or the method will consider the key as does not exist. For other styles, the method will convert the key to selected style and check for its existence.

Parameters:

  • string $key The name of the key.
Returns: booleanThe method will return true if the key exists. false if not.
public function isFormatted()Checks if the final JSON output will be formatted or not. This can be used to make the generated output readable by adding indentation and new lines.Returns: booleanTrue if will be formatted. False otherwise.public function remove(string$keyName)Removes a property from the instance.

Parameters:

  • string $keyName The name of the property.
Returns: Property|nullThe method will return the property as object if removed. Other than that, the method will return null.
public function setIsFormatted(boolean$bool)Makes the JSON output appears readable or not. If the output is formatted, the generated output will look like a tree. If not formatted, the output string will be generated as one line.

Parameters:

  • boolean $bool True to make the output formatted and false to make it not.
public function setPropsStyle(string$style)Sets the style at which the names of the properties will use. Another way to set the style that will be used by the instance is to define the global constant 'JSONX_PROP_STYLE' and set its value to the desired style. Note that the method will change already added properties to the new style. Also, it will override the style which is set using the constant 'JSONX_PROP_STYLE'.

Parameters:

  • string $style The style that will be used. It can be one of the following values:
    • camel
    • kebab
    • snake
    • none
public function toJSONString()Creates and returns a well formatted JSON string that will be created using provided data. Returns: stringA well formatted JSON string that will be created using provided data.public function toJSONxString()Creates and returns a well formatted XML string that will be created using provided data. Returns: stringA well formatted JSONx string that will be created using provided data.
mdi-menuAll Classesmdi-chevron-leftAnchorBrCodeSnippetHTMLDocHTMLListHTMLNodeHTMLTableHeadNodeInputJsCodeLabelListItemOrderedListParagraphRadioGroupTableCellTableRowUnorderedListInvalidNodeNameExceptionTemplateNotFoundExceptionCaseConverterJsonJsonConverterJsonIJsonTypesPropertyAPIFilterAbstractWebServiceManagerInfoServiceParamTypesRequestRequestParameterResponseUriWebServicesManagerAccessAutoLoaderConfigConfigControllerDBEAbstractWebServiceExtendedWebServicesManagerFileLoggerPrivilegePrivilegesGroupThemeThemeLoaderUploadFileUploaderUserUtilWebFioriAppLanguageErrorBoxHTTPCodeViewMessageBoxServerErrViewStarterPageWebPageDatabaseSessionStorageDefaultSessionStorageMSSQLSessionDataTableMSSQLSessionsTableMySQLSessionDataTableMySQLSessionsTableSessionSessionOperationsSessionStorageSessionsManagerRouterRouterUriAbstractMiddlewareMiddlewareManagerEmailMessageSMTPAccountSMTPServerClassLoaderExceptionFileExceptionInitializationExceptionInvalidCRONExprExceptionMissingLangExceptionNoSuchThemeExceptionRoutingExceptionSMTPExceptionSessionExceptionUIExceptionAbstractJobCronCronEmailCronJobJobArgumentCronLoginViewCronTasksViewCronViewCronLoginServiceCronLogoutServiceCronServicesManagerForceCronExecutionServiceGetJobsServiceCLICLICommandInputStreamOutputStreamStdInStdOutClassWriterLangClassWriterQueryClassWriterServiceHolderThemeClassWriterWebServiceWriterCreateCLIClassHelperCreateCronJobCreateMiddlewareCreateTableCreateTableObjCreateThemeHelperCreateWebServiceAddCommandCreateCommandCronCommandHelpCommandListCronCommandListRoutesCommandListThemesCommandRunSQLQueryCommandSettingsCommandTestRouteCommandUpdateSettingsCommandUpdateTableCommandVersionCommandAbstractQueryColumnColumnFactoryConditionConnectionConnectionInfoDatabaseDatabaseExceptionDateTimeValidatorEntityMapperExpressionForeignKeyJoinTableResultSetSelectExpressionTableWhereExpressionMySQLColumnMySQLConnectionMySQLQueryMySQLTableMSSQLColumnMSSQLConnectionMSSQLQueryMSSQLTableAbstractCollectionComparableLinkedListNodeQueueStack
Powered By: WebFiori Framework, Vue and VuetifyAll Rights Reserved © 2018 - 2024