Reference to target array or associative array to write to.
Source JSONValue to sync the contents with.
Whether or not to save string keys in lowercase.
Whether or not to save final string values in lowercase.
1 import std.json : JSONType, JSONValue; 2 3 { 4 long[string] aa = 5 [ 6 "abc" : 123, 7 "def" : 456, 8 "ghi" : 789, 9 ]; 10 11 JSONValue j = JSONValue(aa); 12 typeof(aa) fromJSON; 13 14 foreach (immutable key, const value; j.objectNoRef) 15 { 16 fromJSON[key] = value.integer; 17 } 18 19 assert(aa == fromJSON); // not is 20 21 auto aaCopy = aa.dup; 22 23 aa["jlk"] = 12; 24 assert(aa != fromJSON); 25 26 aa = typeof(aa).init; 27 populateFromJSON(aa, j); 28 assert(aa == aaCopy); 29 } 30 { 31 auto aa = 32 [ 33 "abc" : true, 34 "def" : false, 35 "ghi" : true, 36 ]; 37 38 JSONValue j = JSONValue(aa); 39 typeof(aa) fromJSON; 40 41 foreach (immutable key, const value; j.objectNoRef) 42 { 43 if (value.type == JSONType.true_) fromJSON[key] = true; 44 else if (value.type == JSONType.false_) fromJSON[key] = false; 45 else 46 { 47 assert(0); 48 } 49 } 50 51 assert(aa == fromJSON); // not is 52 53 auto aaCopy = aa.dup; 54 55 aa["jkl"] = false; 56 assert(aa != fromJSON); 57 58 aa = typeof(aa).init; 59 populateFromJSON(aa, j); 60 assert(aa == aaCopy); 61 } 62 { 63 auto arr = [ "abc", "def", "ghi", "jkl" ]; 64 65 JSONValue j = JSONValue(arr); 66 typeof(arr) fromJSON; 67 68 foreach (const value; j.arrayNoRef) 69 { 70 fromJSON ~= value.str; 71 } 72 73 assert(arr == fromJSON); // not is 74 75 auto arrCopy = arr.dup; 76 77 arr[0] = "no"; 78 assert(arr != arrCopy); 79 80 arr = []; 81 populateFromJSON(arr, j); 82 assert(arr == arrCopy); 83 } 84 { 85 auto aa = 86 [ 87 "abc" : [ "def", "ghi", "jkl" ], 88 "def" : [ "MNO", "PQR", "STU" ], 89 ]; 90 91 JSONValue j = JSONValue(aa); 92 typeof(aa)fromJSON; 93 94 foreach (immutable key, const arrJSON; j.objectNoRef) 95 { 96 foreach (const entry; arrJSON.arrayNoRef) 97 { 98 fromJSON[key] ~= entry.str; 99 } 100 } 101 102 assert(aa == fromJSON); // not is 103 104 auto aaCopy = aa.dup; 105 aaCopy["abc"] = aa["abc"].dup; 106 107 aa["abc"][0] = "no"; 108 aa["ghi"] ~= "VWXYZ"; 109 assert(aa != fromJSON); 110 111 aa = typeof(aa).init; 112 populateFromJSON(aa, j); 113 assert(aa == aaCopy); 114 } 115 { 116 int[3] arr = [ 1, 2, 3 ]; 117 118 JSONValue j = JSONValue(arr); 119 120 int[3] arr2; 121 arr2.populateFromJSON(j); 122 assert(arr2 == arr); 123 }
Recursively populates a passed associative or dynamic array with the contents of a JSONValue.
This is used where we want to store information on disk but keep it in memory without the overhead of dealing with JSONValues.
Note: This only works with JSONValues that conform to arrays and associative arrays, not such that mix element/value types.