Logo

DOME

The comfortable framework for making games in Wren

Download the latest version here!

View the Project on GitHub domeengine/dome

< Back

json

The json module provides a simple interface to read and write json files and strings.

It contains the following classes:

Json

Static Methods

static encode(object: Object): String

static encode(object: Object, options: Num): String

Transform the object to a Json encoded string. With default or custom options. Encoding only works for primitive data types (Bool, Map, Num, Null, List, String). If a non primitive object is passed, the encoder will call it’s toString method.

static decode(value: String, options: Num): Object

static decode(value: String): Object

Returns a new Json object with default or custom options.

static load(path: String): Object

static load(path: String, options: Num): Object

Reads the contents of a file in path and returns a new Json object with default or custom options.

static save(path: String, object: Object)

static save(path: String, object: Object, options: Num)

This will encode the object and then save the result to a file specified in path. With default or custom options.

Examples

Encoding a Simple Object

A simple object made with only primitive data structures.

Json.encode({
 "is": true
})

Encoding a Complex Object

An object made with custom data structures.

import "json" for Json

class MyClass {
  // override toString to provide a serializable representation
  // of the object. This serialization will be called by the
  // Json.encode() method.
  toString { Json.encode(toMap) }
  toMap { { "is": isTrue } }

  isTrue { _isTrue }

  construct new(isTrue) {
    _isTrue = isTrue
  }
}

var obj = MyClass.new(true)

// prints: { "is":true }
System.print(Json.encode(obj))

JsonOptions

Constants

static nil: Num

No options selected.

static escapeSlashes: Num

This will encode solidus character (/). When converting a Map object to a String. This encoding is optional and is useful when you need to embed JSON inside HTML <script> tags. By default DOME does not escape slashes.

static abortOnError: Num

By default DOME aborts when there is a JSON parsing error (triggers a Fiber.abort() on parse error). Turn off this option if you want to capture the JsonError object.

static checkCircular: Num

By default DOME checks, when encoding, if your JSON contains circles, and aborts if it does. Turn off this option to not perform this check. This can speed up serializing, but will trigger an infinite recursion if the JSON indeed contains circles.

Example

Use Bitwise OR operator to select multiple options.

Json.decode(myString, JsonOptions.escapeSlashes | JsonOptions.abortOnError);

JsonError

This object is returned on calls to Json.decode only if the JsonOptions.abortOnError default behaviour is disabled and a parse error was found.

Instance Properties

line: Num

Stores the last parsed line number.

position: Num

Stores the last parsed cursor position.

message: String

Stores the generated error message.

found: Bool

Tells if an error was found.