Macaulay2 » Documentation
Packages » MRDI » saveMRDI » addSaveMethod
next | previous | forward | backward | up | index | toc

addSaveMethod -- register a method for serializing a type to MRDI format

Description

This function registers a serialization method for the given type so that saveMRDI knows how to convert objects of that type into MRDI JSON format.

The zero-argument form addSaveMethod T is for types with no data and no parameters (the MRDI output will contain only a _type field).

The one-argument form addSaveMethod(T, dataFunc) is for basic (non-parametric) types whose MRDI representation needs only a _type name and data. The dataFunc receives the object and should return the data portion.

The two-argument form addSaveMethod(T, paramsFunc, dataFunc) is for parametric types. The paramsFunc returns an object representing the type's parameter (which will be recursively serialized into the _type.params field), and dataFunc returns the data.

The UseID option causes objects of this type to be assigned RFC 9562 version 4 UUIDs upon serialization. This is important for types like polynomial rings that may be shared by many objects: the ring is stored once in the _refs section and referenced by UUID elsewhere.

The Namespace option allows registering save methods for different namespaces. This is how OSCAR serialization support is implemented alongside Macaulay2's own namespace.

Here we register a save method for a custom namespace.

i1 : addNamespace("MySystem", "https://example.com", "1.0")
i2 : addSaveMethod(ZZ, identity, Name => "MyInt", Namespace => "MySystem")
i3 : saveMRDI(42, Namespace => "MySystem")

o3 = {"_ns": {"MySystem": ["https://example.com", "1.0"]}, "_type": "MyInt",
     "data": "42"}

See also

Ways to use addSaveMethod:

  • addSaveMethod(Type)
  • addSaveMethod(Type,Function)
  • addSaveMethod(Type,Function,Function)

For the programmer

The object addSaveMethod 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:736:0.