1 /** 2 Helper class for parsing JSON data. 3 4 Copyright: 5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. 6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) 7 Copyright (c) 2017-2018 Godot-D contributors 8 9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License) 10 11 12 */ 13 module godot.json; 14 import std.meta : AliasSeq, staticIndexOf; 15 import std.traits : Unqual; 16 import godot.d.traits; 17 import godot.core; 18 import godot.c; 19 import godot.d.bind; 20 import godot.d.reference; 21 import godot.globalenums; 22 import godot.object; 23 import godot.jsonparseresult; 24 /** 25 Helper class for parsing JSON data. 26 27 For usage example and other important hints, see $(D JSONParseResult). 28 */ 29 @GodotBaseClass struct JSONSingleton 30 { 31 package(godot) enum string _GODOT_internal_name = "_JSON"; 32 public: 33 @nogc nothrow: 34 union { /** */ godot_object _godot_object; /** */ GodotObject _GODOT_base; } 35 alias _GODOT_base this; 36 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 37 package(godot) __gshared bool _classBindingInitialized = false; 38 package(godot) static struct GDNativeClassBinding 39 { 40 __gshared: 41 godot_object _singleton; 42 immutable char* _singletonName = "JSON"; 43 @GodotName("parse") GodotMethod!(JSONParseResult, String) parse; 44 @GodotName("print") GodotMethod!(String, Variant, String, bool) print; 45 } 46 /// 47 pragma(inline, true) bool opEquals(in JSONSingleton other) const 48 { return _godot_object.ptr is other._godot_object.ptr; } 49 /// 50 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 51 { _godot_object.ptr = n; return null; } 52 /// 53 pragma(inline, true) bool opEquals(typeof(null) n) const 54 { return _godot_object.ptr is n; } 55 /// 56 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 57 mixin baseCasts; 58 /// Construct a new instance of JSONSingleton. 59 /// Note: use `memnew!JSONSingleton` instead. 60 static JSONSingleton _new() 61 { 62 static godot_class_constructor constructor; 63 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("_JSON"); 64 if(constructor is null) return typeof(this).init; 65 return cast(JSONSingleton)(constructor()); 66 } 67 @disable new(size_t s); 68 /** 69 Parses a JSON-encoded string and returns a $(D JSONParseResult) containing the result. 70 */ 71 Ref!JSONParseResult parse(in String json) 72 { 73 checkClassBinding!(typeof(this))(); 74 return ptrcall!(JSONParseResult)(GDNativeClassBinding.parse, _godot_object, json); 75 } 76 /** 77 Converts a $(D Variant) var to JSON text and returns the result. Useful for serializing data to store or send over the network. 78 $(B Note:) The JSON specification does not define integer or float types, but only a $(I number) type. Therefore, converting a Variant to JSON text will convert all numerical values to $(D double) types. 79 Use `indent` parameter to pretty print the output. 80 $(B Example output:) 81 82 83 ## JSON.print(my_dictionary) 84 {"name":"my_dictionary","version":"1.0.0","entities":$(D {"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"})} 85 86 ## JSON.print(my_dictionary, "\t") 87 { 88 "name": "my_dictionary", 89 "version": "1.0.0", 90 "entities": [ 91 { 92 "name": "entity_0", 93 "value": "value_0" 94 }, 95 { 96 "name": "entity_1", 97 "value": "value_1" 98 } 99 ] 100 } 101 102 103 */ 104 String print(VariantArg0)(in VariantArg0 value, in String indent = gs!"", in bool sort_keys = false) 105 { 106 checkClassBinding!(typeof(this))(); 107 return ptrcall!(String)(GDNativeClassBinding.print, _godot_object, value, indent, sort_keys); 108 } 109 } 110 /// Returns: the JSONSingleton 111 @property @nogc nothrow pragma(inline, true) 112 JSONSingleton JSON() 113 { 114 checkClassBinding!JSONSingleton(); 115 return JSONSingleton(JSONSingleton.GDNativeClassBinding._singleton); 116 }