beancount
    Preparing search index...

    Function deserializeNodesFromString

    • Deserializes an array of nodes from a JSON string.

      This function parses a JSON string containing an array of node objects and reconstructs each node 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

      • jsonString: string

        JSON string containing an array of node objects

      Returns Node[]

      Array of Node instances

      If the JSON is invalid or nodes cannot be deserialized:

      • Invalid JSON syntax
      • JSON does not contain an array
      • Any node fails validation (see deserializeNode)

      Deserializing multiple nodes:

      const json = '[
      {"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(json)
      console.log(nodes.length) // 2

      Roundtrip serialization:

      import { parse } from 'beancount'

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