DrawJson

object DrawJson

The library's own file format: a list of shapes written as JSON, losslessly. Everything a shape is comes back as it was — styles (with their pictures), rings and holes, vertex ids and metadata, labels, descriptions, drawing order — so a drawing can be saved and loaded again, and two saves compared.

val text = DrawJson.encode(repository.entities.value)
val shapes = DrawJson.decode(text)

The result is a String: where it goes (a file, a database, the network) is the host's business. A picture given as bytes is stored once in the document, in base64, however many shapes use it; one given as a URL is only its URL, so loading it later needs the network.

Every document says which VERSION of the format it is. Newer fields can be added without a new version (an old file just lacks them, and a reader ignores the ones it doesn't know); a file of a newer version than this code knows is refused rather than half understood.

Properties

Link copied to clipboard
const val FORMAT: String

What the format field of every document says, so a stray JSON file is told apart from ours.

Link copied to clipboard
const val VERSION: Int = 1

The version of the format this code writes and the newest it reads.

Functions

Link copied to clipboard

The shapes in text, in the order they were written.

Link copied to clipboard
fun encode(entities: List<DrawableEntity>, pretty: Boolean = false): String

The JSON for entities, in their order. pretty adds line breaks and indentation, for a person to read; the content is the same.