Skip to content
zeganstyl edited this page Sep 18, 2020 · 1 revision

Thelema have singleton object JSON, that may be used to parse strings and stringify objects to json.

You can create json object by JSON.printObject and use DSL. For example let's write object to string:

val jsonString = JSON.printObject {
	set("someNumber", 123)
	set("someNestedObject") {
		setArray("someArray") {
			add("someString")
		}
	}
}

Read object with DSL:

JSON.parseObject(jsonString) {
	val someNumber = int("someNumber")
	obj("someNestedObject") {
		array("someArray") {
			strings {
				println(it)
			}
		}
	}
}

To make some of your class serializable/deserializable you can implement IJsonObjectIO or IJsonArrayIO. If class implements one of those interfaces, you can set instances of this class: set("someObject", objInstance)

For example IJsonObjectIO requires to implement read() and write() methods:

override fun read(json: IJsonObjectIO) {
	someProperty = json.int("someValue")
}

override fun write(json: IJsonObjectIO) {
	json["someValue"] = someProperty
}

If you want to create your own JSON backend, then, you have to implement interface IJSON. See jvm.json package as example of implementation.

See test

Clone this wiki locally