PHP's json_decode in TypeScript
✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { json_decode } from 'locutus/php/json/json_decode'.
Or with CommonJS: const { json_decode } = require('locutus/php/json/json_decode')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite ,
webpack ,
Rollup , and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
# code expected result
1 json_decode('[ 1 ]')[1]
Notes
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to PHP's json_decode looks like.
Copy code
import { setPhpRuntimeEntry } from '../_helpers/_phpRuntimeState.ts' type JsonPrimitive = string | number | boolean | null type JsonValue = JsonPrimitive | JsonValue [] | { [key : string ]: JsonValue }export function json_decode<T = JsonValue >(strJson : string ): T | null { const json = typeof JSON === 'object' && JSON !== null ? JSON : null const parse = json?.parse if (typeof parse !== 'function' ) { setPhpRuntimeEntry ('last_error_json' , 4 ) return null } try { const parsed = parse.call (json, strJson) setPhpRuntimeEntry ('last_error_json' , 0 ) return parsed } catch (err) { if (!(err instanceof SyntaxError )) { throw new Error ('Unexpected error type in json_decode()' ) } setPhpRuntimeEntry ('last_error_json' , 4 ) return null } }
import { setPhpRuntimeEntry } from '../_helpers/_phpRuntimeState.ts' export function json_decode (strJson ) { const json = typeof JSON === 'object' && JSON !== null ? JSON : null const parse = json?.parse if (typeof parse !== 'function' ) { setPhpRuntimeEntry ('last_error_json' , 4 ) return null } try { const parsed = parse.call (json, strJson) setPhpRuntimeEntry ('last_error_json' , 0 ) return parsed } catch (err) { if (!(err instanceof SyntaxError )) { throw new Error ('Unexpected error type in json_decode()' ) } setPhpRuntimeEntry ('last_error_json' , 4 ) return null } }
type PhpNullish = null | undefined type PhpInput = {} | PhpNullish type PhpList <T = PhpInput > = T[]type PhpAssoc <T = PhpInput > = { [key : string ]: T }interface IniEntry { local_value ?: PhpInput } type LocaleEntry = PhpAssoc <PhpInput > & { sorting ?: (left : PhpInput , right : PhpInput ) => number } type LocaleCategoryMap = PhpAssoc <string | undefined >interface LocutusRuntimeContainer { php ?: PhpAssoc <PhpInput > } interface PhpRuntimeKnownEntryMap { ini : PhpAssoc <IniEntry | undefined > locales : PhpAssoc <LocaleEntry | undefined > localeCategories : LocaleCategoryMap pointers : PhpList <PhpInput > locale_default : string locale : string uniqidSeed : number timeoutStatus : boolean last_error_json : number strtokleftOver : string } type GlobalWithLocutus = { $locutus?: LocutusRuntimeContainer [key : string ]: PhpInput } const globalContext : GlobalWithLocutus = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} const ensurePhpRuntimeObject = (): PhpAssoc <PhpInput > => { let locutus = globalContext.$locutus if (typeof locutus !== 'object' || locutus === null ) { locutus = {} globalContext.$locutus = locutus } let php = locutus.php if (typeof php !== 'object' || php === null ) { php = {} locutus.php = php } return php } function setPhpRuntimeEntry<TKey extends keyof PhpRuntimeKnownEntryMap >( key : TKey , value : PhpRuntimeKnownEntryMap [TKey ], ): void function setPhpRuntimeEntry (key : string , value : PhpInput ): void function setPhpRuntimeEntry (key : string , value : PhpInput ): void { const php = ensurePhpRuntimeObject () php[key] = value } type JsonPrimitive = string | number | boolean | null type JsonValue = JsonPrimitive | JsonValue [] | { [key : string ]: JsonValue }function json_decode<T = JsonValue >(strJson : string ): T | null { const json = typeof JSON === 'object' && JSON !== null ? JSON : null const parse = json?.parse if (typeof parse !== 'function' ) { setPhpRuntimeEntry ('last_error_json' , 4 ) return null } try { const parsed = parse.call (json, strJson) setPhpRuntimeEntry ('last_error_json' , 0 ) return parsed } catch (err) { if (!(err instanceof SyntaxError )) { throw new Error ('Unexpected error type in json_decode()' ) } setPhpRuntimeEntry ('last_error_json' , 4 ) return null } }
const globalContext = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {} const ensurePhpRuntimeObject = ( ) => { let locutus = globalContext.$locutus if (typeof locutus !== 'object' || locutus === null ) { locutus = {} globalContext.$locutus = locutus } let php = locutus.php if (typeof php !== 'object' || php === null ) { php = {} locutus.php = php } return php } function setPhpRuntimeEntry (key, value ) { const php = ensurePhpRuntimeObject () php[key] = value } function json_decode (strJson ) { const json = typeof JSON === 'object' && JSON !== null ? JSON : null const parse = json?.parse if (typeof parse !== 'function' ) { setPhpRuntimeEntry ('last_error_json' , 4 ) return null } try { const parsed = parse.call (json, strJson) setPhpRuntimeEntry ('last_error_json' , 0 ) return parsed } catch (err) { if (!(err instanceof SyntaxError )) { throw new Error ('Unexpected error type in json_decode()' ) } setPhpRuntimeEntry ('last_error_json' , 4 ) return null } }
Improve this function
Locutus is a community effort following
The McDonald's Theory :
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
View on GitHub
·
Edit on GitHub
·
View Raw
« More PHP json functions
Star