Documentation

Lean.Data.Json.FromToJson.Basic

class Lean.FromJson (α : Type u) :

Types that can be decoded from JSON.

Use deriving FromJson to automatically generate an instance. See ToJson for details of auto-generated instances.

Instances
    class Lean.ToJson (α : Type u) :

    Types that can be encoded as JSON.

    Use deriving ToJson to automatically generate an instance. The following encoding strategy is employed by auto-generated instances:

    • Basic types corresponding to JSON values are encoded as these values.

      • Bool is encoded as true/false.

      • Strings are encoded as JSON strings.

      • Numeric types are encoded as JSON numbers, with the exception of:

        • UInt64 and USize which are encoded as JSON strings. This is because, although JSON numbers proper have unbounded range, in JavaScript they are parsed as Numbers and these can only represent integers up to $2^{53} - 1$ safely; so a roundtrip through JavaScript would result in truncation on these types. (We use these types in the JavaScript-based Lean infoview.)

        • Special Floats which are encoded as JSON strings: "NaN"/"Infinity"/"-Infinity".

    • Unit is encoded as {} (empty JSON object).

    • Arrays and Lists are encoded as JSON arrays.

    • Option.none is encoded as null, whereas some a has the same encoding as a. Note that this implies Option (Option α) does not roundtrip, since none and some none both become null.

    • General structures are encoded as JSON objects in the obvious way.

      • Option fields whose names end with ? have special support: the question mark is omitted from the JSON field name, and such a field is omitted from the JSON object when its value is none (as opposed to being encoded as { "someField": null }).
    • General inductive types are encoded on a per-constructor basis.

      • An argument-free constructor is encoded as its name (a JSON string).

      • A constructor with named arguments only is encoded as the JSON object { "ctorName": { "arg1": argVal1, ..., "argN": argValN } }.

      • A constructor with one unnamed argument is encoded as the JSON object { "ctorName": argVal }.

      • A constructor with more than one unnamed argument is encoded as the JSON object { "ctorName": [argVal1, ..., argValN] }.

    • Certain other types have special handling: see the instances below.

    • toJson : αJson
    Instances
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      • One or more equations did not get rendered due to their size.
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      @[instance_reducible]
      Equations
      def Lean.Array.fromJson? {α : Type u_1} [FromJson α] :
      Equations
      Instances For
        @[instance_reducible]
        instance Lean.instFromJsonArray {α : Type u_1} [FromJson α] :
        Equations
        def Lean.Array.toJson {α : Type u_1} [ToJson α] (a : Array α) :
        Equations
        Instances For
          @[instance_reducible]
          instance Lean.instToJsonArray {α : Type u_1} [ToJson α] :
          Equations
          @[instance_reducible]
          instance Lean.instFromJsonList {α : Type u_1} [FromJson α] :
          Equations
          def Lean.List.toJson {α : Type u_1} [ToJson α] (a : List α) :
          Equations
          Instances For
            @[instance_reducible]
            instance Lean.instToJsonList {α : Type u_1} [ToJson α] :
            Equations
            @[instance_reducible]
            instance Lean.instFromJsonOption {α : Type u_1} [FromJson α] :
            Equations
            @[instance_reducible]
            instance Lean.instToJsonOption {α : Type u_1} [ToJson α] :
            Equations
            def Lean.Prod.fromJson? {α : Type u} {β : Type v} [FromJson α] [FromJson β] :
            JsonExcept String (α × β)
            Equations
            • One or more equations did not get rendered due to their size.
            Instances For
              @[instance_reducible]
              instance Lean.instFromJsonProd {α : Type u} {β : Type v} [FromJson α] [FromJson β] :
              FromJson (α × β)
              Equations
              def Lean.Prod.toJson {α : Type u_1} {β : Type u_2} [ToJson α] [ToJson β] :
              α × βJson
              Equations
              Instances For
                @[instance_reducible]
                instance Lean.instToJsonProd {α : Type u_1} {β : Type u_2} [ToJson α] [ToJson β] :
                ToJson (α × β)
                Equations
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For
                  @[instance_reducible]
                  Equations
                  @[instance_reducible]
                  Equations
                  Equations
                  Instances For
                    @[instance_reducible]
                    Equations
                    def Lean.NameMap.toJson {α : Type} [ToJson α] (m : NameMap α) :
                    Equations
                    Instances For
                      @[instance_reducible]
                      instance Lean.instToJsonNameMap {α : Type} [ToJson α] :
                      Equations

                      Note that USizes and UInt64s are stored as strings because JavaScript cannot represent 64-bit numbers.

                      Equations
                      • One or more equations did not get rendered due to their size.
                      Instances For
                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[instance_reducible]
                          Equations
                          @[instance_reducible]
                          Equations
                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            @[instance_reducible]
                            Equations
                            Equations
                            Instances For
                              @[instance_reducible]
                              Equations
                              def Lean.Json.getObjValAs? (j : Json) (α : Type u) [FromJson α] (k : String) :
                              Equations
                              Instances For
                                def Lean.Json.setObjValAs! (j : Json) {α : Type u} [ToJson α] (k : String) (v : α) :
                                Equations
                                Instances For
                                  def Lean.Json.opt {α : Type u_1} [ToJson α] (k : String) :
                                  Equations
                                  Instances For

                                    Returns the string value or single key name, if any.

                                    Equations
                                    Instances For
                                      def Lean.Json.parseTagged (json : Json) (tag : String) (nFields : Nat) (fieldNames? : Option (Array Name)) :
                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      Instances For
                                        def Lean.Json.parseCtorFields (json : Json) (tag : String) (nFields : Nat) (fieldNames? : Option (Array Name)) :

                                        Parses a JSON-encoded structure or inductive constructor, assuming the tag has already been checked and nFields is nonzero. Used mostly by deriving FromJson.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For