Macaulay2 » Documentation
Packages » MRDI » loadMRDI » addLoadMethod
next | previous | forward | backward | up | index | toc

addLoadMethod -- register a method for deserializing a type from MRDI format

Description

This methods registers a deserialization method so that loadMRDI knows how to reconstruct objects of a given type within a given namespace.

The namespace allows the same file format to be used across different computer algebra systems. The Macaulay2 namespace is "Macaulay2" and the OSCAR namespace is "Oscar". Each namespace maintains its own type registry. Use addNamespace to register a new namespace before adding load methods to it.

The loading function f receives two arguments:

  • params: For parametric types (e.g., RingElement), this is the already-deserialized parameter (e.g., the parent ring). For non-parametric types (e.g., ZZ), this is null.
  • data: The contents of the data field from the JSON, recursively deserialized.

The List form allows registering the same function for multiple type names at once, which is useful when systems use different names for equivalent types.

Here we register a load method for a custom namespace.

i1 : addNamespace("MySystem", "https://example.com", "1.0")
i2 : addLoadMethod("MyInt",
         (params, data) -> value data,
         Namespace => "MySystem")

o2 = FunctionClosure[currentString:3:4-3:32]

o2 : FunctionClosure
i3 : loadMRDI "{\"_ns\":{\"MySystem\":[\"https://example.com\",\"1.0\"]},\"_type\":\"MyInt\",\"data\":\"42\"}"

o3 = 42

See also

Ways to use addLoadMethod:

  • addLoadMethod(List,Function)
  • addLoadMethod(String,Function)

For the programmer

The object addLoadMethod is a method function with options.


The source of this document is in /build/reproducible-path/macaulay2-1.26.05+ds/M2/Macaulay2/packages/MRDI.m2:795:0.