Skip to contents

Reads a document of type: checks "$type", the versions in "$types", and the declarations in "$declarations", validates the value, and returns it. A type the document names is looked up among the registered types only; the document never selects code to load.

Usage

r8from_json(type, json, load = NULL, declaration = "registered")

Arguments

type

An R8 type object.

json

Character: The JSON document.

load

Optional Character: Packages to load, with loadNamespace(), before the read, such as the package that declares an implementor of a dyn() field.

declaration

Character {"registered", "any"}: "registered" refuses a document that was written under another declaration of a type than the registered one, unless it holds the declarations (r8to_json(declarations = TRUE)) and its fields mean the same under both, as r8update() states. "any" reads it as one of the registered declaration. A document without "$declarations", as one written by hand, claims none.

Value

An R8 value. A document that names types no loaded package declares raises r8_unknown_type_error, which names each namespace and the installed packages that declare it.

Examples

op <- options(r8.namespace = "demo")
Point <- new_struct("Point", fields = list(x = type_double(), y = type_double()))
r8from_json(Point, r8to_json(Point(x = 1, y = 2)))
#> <demo::Point>
#>   x: 1
#>   y: 2
options(op)