JsonMap and JsonableArray
This commit is contained in:
19
projects/kiss-tools/src/kiss_tools/JsonMap.hx
Normal file
19
projects/kiss-tools/src/kiss_tools/JsonMap.hx
Normal file
@@ -0,0 +1,19 @@
|
||||
// TODO bug in kiss-vscode new class: it takes the first line of the currently open file,
|
||||
// instead of intelligently picking a package declaration
|
||||
package kiss_tools;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
|
||||
import haxe.ds.Map;
|
||||
import haxe.Json;
|
||||
import haxe.DynamicAccess;
|
||||
import sys.io.File;
|
||||
|
||||
typedef Jsonable<T> = {
|
||||
function stringify():String;
|
||||
function parse(s:String):T;
|
||||
}
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class JsonMap<T:Jsonable<T>> {}
|
||||
25
projects/kiss-tools/src/kiss_tools/JsonMap.kiss
Normal file
25
projects/kiss-tools/src/kiss_tools/JsonMap.kiss
Normal file
@@ -0,0 +1,25 @@
|
||||
(method _parseFrom [:String representation]
|
||||
(let [:DynamicAccess<String> json (Json.parse representation)]
|
||||
(doFor =>key representation json
|
||||
(dictSet m key (defaultVal.parse representation)))))
|
||||
|
||||
(method stringify []
|
||||
(Json.stringify
|
||||
(for =>k v m =>k (v.stringify))
|
||||
null
|
||||
"\t"))
|
||||
|
||||
(defNew [&prop :String jsonPath &prop :T defaultVal]
|
||||
[:Map<String,T> m (new Map)]
|
||||
|
||||
(_parseFrom (File.getContent jsonPath)))
|
||||
|
||||
(method put [:String key :T value]
|
||||
(dictSet m key value)
|
||||
(File.saveContent jsonPath (stringify)))
|
||||
|
||||
(method get [:String key]
|
||||
(unless (m.exists key)
|
||||
(put key defaultVal))
|
||||
(dictGet m key))
|
||||
|
||||
10
projects/kiss-tools/src/kiss_tools/JsonableArray.hx
Normal file
10
projects/kiss-tools/src/kiss_tools/JsonableArray.hx
Normal file
@@ -0,0 +1,10 @@
|
||||
package kiss_tools;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
import kiss_tools.JsonMap;
|
||||
import haxe.Json;
|
||||
import haxe.DynamicAccess;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class JsonableArray<T:Jsonable<T>> {}
|
||||
12
projects/kiss-tools/src/kiss_tools/JsonableArray.kiss
Normal file
12
projects/kiss-tools/src/kiss_tools/JsonableArray.kiss
Normal file
@@ -0,0 +1,12 @@
|
||||
(defNew [&prop :Array<T> elements
|
||||
&prop :T defaultVal])
|
||||
|
||||
(method parse [:String representation]
|
||||
(let [:Array<String> arr (Json.parse representation)]
|
||||
(new JsonableArray<T>
|
||||
(for elem arr
|
||||
(defaultVal.parse elem))
|
||||
defaultVal)))
|
||||
|
||||
(method stringify []
|
||||
(Json.stringify (for elem elements (elem.stringify))))
|
||||
@@ -1,8 +0,0 @@
|
||||
package kiss_tools;
|
||||
|
||||
import kiss.Kiss;
|
||||
import kiss.Prelude;
|
||||
import kiss.List;
|
||||
|
||||
@:build(kiss.Kiss.build())
|
||||
class Main {}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Test FuzzyJson
|
||||
(load "FuzzyJson.kiss")
|
||||
|
||||
(loadFuzzyJson "dogs" "test/fuzzy.json")
|
||||
(loadFuzzyJson "dogs" "test/fuzzy2.json")
|
||||
|
||||
(assert (= "is a very good dog" (getFuzzyJson "dogs" "Albort")))
|
||||
// takeFuzzyJson removes the match to make following fuzzyJson retrievals save time:
|
||||
(assert (= "is a very good dog" (takeFuzzyJson "dogs" "Albort")))
|
||||
// No good match will cause crash at compile time:
|
||||
(assertThrowsAtCompileTime (getFuzzyJson "dogs" "Albort"))
|
||||
// duplicate definitions throw an error at compile time:
|
||||
(assertThrowsAtCompileTime (getFuzzyJson "dogs" "Rangie"))
|
||||
Reference in New Issue
Block a user