Skip to content

Instantly share code, notes, and snippets.

@RealyUniqueName RealyUniqueName/Json.hx
Last active Aug 6, 2018

Embed
What would you like to do?
Sample PHP externs for Haxe
-main Main
-php bin
#this flag is required if you're using Haxe 3.4
-D php7
{
"require": {
"zendframework/zendframework": "^3.0"
}
}
//You can use any package name here.
//It's not required to match the namespace of php file.
package;
import php.NativeStructArray;
typedef EncodingOptions = {
?prettyPrint:Bool,
?enableJsonExprFinder:Bool,
}
@:native('Zend\\Json\\Json')
extern class Json {
@:phpClassConst static var TYPE_ARRAY(default,never):Int;
@:phpClassConst static var TYPE_OBJECT(default,never):Int;
static var useBuiltinEncoderDecoder:Bool;
static function decode(encodedValue:String, ?objectDecodeType:Int):Any;
static function encode(valueToEncode:Any, ?cycleCheck:Bool, ?options:NativeStructArray<EncodingOptions>):String;
static function prettyPrint(json:String, ?options:NativeStructArray<{?indent:String}>):String;
}
import php.Global;
import php.Const;
import php.NativeArray;
class Main {
static function __init__() {
//Assuming you're using Composer for the dependency management
//and the `vendor` directory is located in the root of your project.
Global.require_once(Const.__DIR__ + '/../../vendor/autoload.php');
}
static function main() {
var obj:NativeArray = Json.decode('{"field":"value"}', Json.TYPE_ARRAY);
trace(obj['field']);
var str = Json.encode(obj, false, {prettyPrint:true});
trace(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.