Skip to content

Archmage.ParseError

The first invalid value and the first JSON value of the wrong type found while the generated code converts the parsed JSON of a config item.

Inherits: RefCounted

The generated load_json creates one, so each item, and each thread, has its own. It passes it to every from_json, which records an invalid value, such as "abc" for a duration, with fail() and returns early. Each caller then adds its own part of the path with at() or message_at(), so that load_json returns a message such as [3].respawn: invalid duration "abc".

A JSON value of the wrong type, such as "abc" for an int field, is recorded with mismatch() instead. The conversion continues with the zero value, and load_json checks mismatched once for each config entry, so the message locates the entry, not the field: [3]: expected a number, got "abc".

var failed: bool = false

Whether an invalid value was found.

var message: String = ""

What is wrong with the value, such as invalid duration "abc".

var path: String = ""

Where the value is, relative to the item, such as [3].respawn.

var mismatched: bool = false

Whether a JSON value of the wrong type was found.

var mismatch_message: String = ""

What is wrong with the type of the first such value, such as expected a number, got "abc".

func fail(msg: String) -> void

Records msg unless an error is already recorded.

func mismatch(msg: String) -> void

Records msg as a type mismatch unless one is already recorded. It does not set failed.

func at(segment: String) -> Variant

Adds segment to the front of path and returns null, so that a from_json can return e.at(".field").

func message_at(segment: String) -> String

Adds segment to the front of path and returns the error message, "<path>: <message>", with message when failed is true, or with mismatch_message otherwise.

static func json_text(v: Variant) -> String

Returns v, a parsed JSON value, as compact JSON for an error message. Unlike JSON.stringify(), it writes a float that is an integer without a fraction, as in the config files: [5,1], not [5.0,1.0].

static func report(e: Archmage.ParseError, msg: String) -> void

Records msg in e, or reports it with push_error() when e is null, as when user code calls a from_json directly.

static func report_mismatch(e: Archmage.ParseError, expected: String, v: Variant, type_name: String = "") -> void

Records in e that v, a parsed JSON value, has the wrong type, or reports it with push_error() when e is null. expected describes the expected value, such as "a number". The message is expected <expected>, got <v>, where an array or an object is described by its kind and any other value by its JSON text, such as expected a number, got "abc". When type_name is not empty, the message starts with it, such as HeroTable.Stats: expected an object, got 5.