| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using Invercargill;
- using Invercargill.DataStructures;
- using InvercargillJson;
- using Statum;
- namespace ClankerSaddle {
- /**
- * Helpers for shaping Statum slot state. Because Statum's GObjectMapping
- * cannot serialise array/list fields, lists are built as a JsonArray and set
- * on a PropertyDictionary under a key, where stm-for-{x}-in can iterate them.
- */
- public class StateJson : Object {
- /**
- * Builds a JSON array element from a lot of GObject row models, mapping
- * each row via GObjectMapping. Use the result as a slot public-data value:
- * `dict["tasks"] = StateJson.list<TaskView>(rows);`
- */
- public static JsonElement list<T>(Lot<T> items) throws GLib.Error {
- var arr = new JsonArray();
- foreach (var item in items) {
- var props = GObjectMapping.to_properties((Object) item);
- arr.add(new JsonElement.from_properties(props));
- }
- return new JsonElement.from_elements(arr);
- }
- /** Same as {@link list} but accepts a Vala array. */
- public static JsonElement list_array<T>(T[] items) throws GLib.Error {
- var arr = new JsonArray();
- foreach (var item in items) {
- var props = GObjectMapping.to_properties((Object) item);
- arr.add(new JsonElement.from_properties(props));
- }
- return new JsonElement.from_elements(arr);
- }
- /**
- * A JSON array of plain strings (e.g. a block's multiple-choice
- * options).
- */
- public static JsonElement strings(string[] values) throws GLib.Error {
- var arr = new JsonArray();
- foreach (var value in values) {
- arr.add(new JsonElement.from_element(new NativeElement<string>(value)));
- }
- return new JsonElement.from_elements(arr);
- }
- /**
- * Per-row enrichment hook for {@link list_rich}: after each item
- * is mapped, the enricher may add array/complex values the plain
- * GObject mapping cannot serialise (e.g. a block's option list).
- */
- public delegate void PropertyEnricher(Object item, PropertyDictionary props) throws GLib.Error;
- /**
- * Like {@link list}, but every row's property dictionary passes
- * through `enrich` before serialisation so callers can attach
- * fields GObjectMapping drops (arrays, nested objects).
- */
- public static JsonElement list_rich<T>(Lot<T> items, owned PropertyEnricher enrich) throws GLib.Error {
- var arr = new JsonArray();
- foreach (var item in items) {
- var props = (PropertyDictionary) GObjectMapping.to_properties((Object) item);
- enrich((Object) item, props);
- arr.add(new JsonElement.from_properties(props));
- }
- return new JsonElement.from_elements(arr);
- }
- }
- }
|