Jelajahi Sumber

[python] clean up json related python api, still no success with using native decoding yet

frabbit 10 tahun lalu
induk
melakukan
523435a984

+ 2 - 10
std/python/_std/haxe/Json.hx

@@ -26,18 +26,10 @@ import python.Syntax;
 class Json {
 
 	public static inline function parse( text : String ) : Dynamic {
-		return python.lib.Json.loads(text, null, null, python.Lib.dictToAnon);
+		return python.lib.Json.loads(text, { object_hook : python.Lib.dictToAnon });
 	}
 
 	public static inline function stringify( value : Dynamic, ?replacer:Dynamic -> Dynamic -> Dynamic, ?space : String ) : String {
-
 		return haxe.format.JsonPrinter.print(value, replacer, space);
-		/*
-		function def (o:Dynamic) {
-			trace(o);
-			return python.Lib.anonToDict(o);
-		}
-		return python.lib.Json.dumps(value, false, false, true, true, null, null, null, def);
-		*/
 	}
-}
+}

+ 22 - 11
std/python/lib/Json.hx

@@ -23,21 +23,32 @@ package python.lib;
 
 import python.KwArgs;
 import python.Dict;
+import python.lib.json.JSONEncoder;
 import python.Tuple.Tuple2;
 
+typedef JsonDumpsOptions = {
+	@:optional var skipkeys : Bool;
+	@:optional var ensure_ascii : Bool;
+	@:optional var check_circular : Bool;
+	@:optional var allow_nan : Bool;
+	@:optional var cls : Dynamic;
+	@:optional var indent : String;
+	@:optional var separators:Tuple2<String,String>;
+	@:optional @:native("default") var def:Dynamic->String;
+	@:optional var sort_keys:Bool;
+
+}
+
+typedef JsonLoadsOptions = {
+	@:optional var encoding:String;
+	@:optional var cls : Dynamic;
+	@:optional var object_hook:Dict<String, Dynamic>->Dynamic;
+}
+
 @:pythonImport("json")
 extern class Json {
 
-
-	public static function loads (
-		s:String,
-		encoding:Null<String> = null,
-		cls : Null<Dynamic> = null,
-		object_hook:Null<Dict<String, Dynamic>->Dynamic> = null
-		):Dict<String, Dynamic>;
-	public static function dumps (x:Dynamic, skipkeys:Bool=false, ensure_ascii:Bool=true, check_circular:Bool=true,
-		allow_nan:Bool=true,
-		cls:Null<Dynamic> = null, indent:Null<String> = null,
-		separators:Null<Tuple2<String,String>>, /*default*/def:Null<Dynamic->String> = null, sort_keys:Bool=false, kw:KwArgs<Dynamic> = null):String;
+	public static function loads ( s:String, ?options:KwArgs<JsonLoadsOptions>):Dict<String, Dynamic>;
+	public static function dumps (x:Dynamic, ?options:KwArgs<JsonDumpsOptions>):String;
 
 }

+ 41 - 0
std/python/lib/json/JSONDecoder.hx

@@ -0,0 +1,41 @@
+/*
+ * Copyright (C)2005-2012 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package python.lib.json;
+
+import python.Tuple;
+
+typedef JSONDecoderOptions = {
+	@:optional var object_hook : Dict<String, Dynamic>->Dynamic;
+	@:optional var parse_float : String->Float;
+	@:optional var parse_int : String->Int;
+	@:optional var parse_constant : String->Dynamic;
+	@:optional var strict:Bool;
+	@:optional var object_pairs_hook : Array<Tuple2<String,String>>->Dict<String,Dynamic>;
+}
+
+@:pythonImport("json", "JSONDecoder")
+extern class JSONDecoder {
+	public function new (?options:KwArgs<JSONDecoderOptions>):Void;
+
+	public function decode (o:String):Dynamic;
+	public function raw_decode (o:String):Tuple2<Dynamic, Int>;
+}

+ 17 - 0
std/python/lib/json/JSONEncoder.hx

@@ -21,6 +21,23 @@
  */
 package python.lib.json;
 
+import python.Tuple;
+
+typedef JSONEncoderOptions = {
+	@:optional var skipkeys : Bool;
+	@:optional var ensure_ascii : Bool;
+	@:optional var check_circular : Bool;
+	@:optional var allow_nan : Bool;
+	@:optional var sort_keys:Bool;
+	@:optional var indent : String;
+	@:optional var separators:Tuple2<String,String>;
+	@:optional @:native("default") var def:Dynamic->String;
+}
 @:pythonImport("json", "JSONEncoder")
 extern class JSONEncoder {
+	public function new (?options:KwArgs<JSONEncoderOptions>):Void;
+
+	@:native("default") public function def (o:Dynamic):Dynamic;
+
+	public function encode (o:Dynamic):String;
 }