beancount
    Preparing search index...

    Function deserializeNodes

    • Deserializes an array of nodes from their JSON representations.

      This function takes an array of plain JavaScript objects (typically from JSON.parse) and reconstructs each as the appropriate Node subclass instance. It validates the input and provides helpful error messages, including the index of any node that fails to deserialize.

      Parameters

      • nodesData: Record<string, unknown>[]

        Array of plain objects containing node data

      Returns Node[]

      Array of Node instances

      If the input is invalid or nodes cannot be deserialized:

      • Input is not an array
      • Any node fails validation (see deserializeNode)

      Deserializing an array of nodes:

      const nodesData = [
      { type: 'open', date: '2024-01-01', account: 'Assets:Checking' },
      { type: 'balance', date: '2024-01-02', account: 'Assets:Checking', amount: '100', currency: 'USD' }
      ]
      const nodes = deserializeNodes(nodesData)
      console.log(nodes.length) // 2

      Roundtrip with JSON.parse:

      const original = [Open.fromString('2024-01-01 open Assets:Checking')]
      const json = JSON.stringify(original.map(e => e.toJSON()))
      const parsed = JSON.parse(json)
      const deserialized = deserializeNodes(parsed)
      // deserialized equals original