1 module uim.json.array_; 2 3 import uim.json; 4 5 class DJSONArray : DJSONValue { 6 protected DJSONValue[] _values; 7 8 this() { } 9 this(DJSONArray value) { this.values = value.values; } 10 this(DJSONValue[] values) { this.values = values; } 11 this(DJSONObject[] values) { this.values = values; } 12 this(DJSONArray[] values) { this.values = values; } 13 this(string[] values) { foreach(v; values) _values ~= JSONValue(v); } 14 this(bool[] values) { foreach(v; values) _values ~= JSONValue(v); } 15 this(int[] values) { foreach(v; values) _values ~= JSONValue(v); } 16 this(double[] values) { foreach(v; values) _values ~= JSONValue(v); } 17 18 @property void values(DJSONValue[] newValues) { _values = null; foreach(v; newValues) _values ~= v.dup; } 19 @property void values(DJSONArray[] newValues) { _values = null; foreach(v; newValues) _values ~= v.dup; } 20 @property void values(DJSONObject[] newValues) { _values = null; foreach(v; newValues) _values ~= v.dup; } 21 @property auto values() { return _values; } 22 23 @property override size_t length() { return _values.length; } 24 @property void length(size_t newLength) { 25 if (newLength > length) foreach(i; length..newLength) _values ~= JSONNull; 26 else _values.length = newLength; 27 } 28 @property DJSONArray subArray(size_t start, size_t end) { 29 auto last = end; 30 if (last > length) last = length; 31 return JSONArray(_values[start..last]); 32 } 33 @property DJSONArray reverse() { 34 DJSONValue[] reversedValues; 35 foreach_reverse(v; values) reversedValues ~= v; 36 _values = reversedValues; 37 return this; 38 } 39 40 override DJSONValue opIndex(size_t index) { 41 if (index < _values.length) return _values[index]; 42 return null; 43 } 44 O opOpAssign(string op, this O)(DJSONValue value) if (op == "~") { 45 _values ~= value; 46 return cast(O)this; 47 } 48 O opBinary(string op, this O)(DJSONValue value) if (op == "~") { 49 _values ~= value; 50 return cast(O)this; 51 } 52 53 override DJSONValue dup() { return new DJSONArray(_values); } 54 override string toString() { 55 if (_values.length == 0) return "[ ]"; 56 57 import std..string; 58 string[] strings; 59 foreach(value; _values) strings ~= value.toString; 60 return `[ %s ]`.format(strings.join(", ")); 61 } 62 } 63 auto JSONArray() { return new DJSONArray(); } 64 auto JSONArray(DJSONArray value) { return new DJSONArray(value); } 65 auto JSONArray(T)(T[] values) { return new DJSONArray(values); } 66 67 auto JSONArray(string text) { return new DJSONArray(); }