@cnri/json-streaming
    Preparing search index...

    Class JsonReader

    A streaming JSON reader that allows streaming parsing of JSON data.

    JsonReader provides methods to navigate through a JSON structure without loading the entire content into memory. This is particularly useful for processing large JSON files or streams efficiently.

    Index

    Constructors

    • Creates a new JsonReader that reads from the specified ReadableStream.

      Parameters

      • readable: ReadableStream<Uint8Array<ArrayBuffer>>

        A ReadableStream containing JSON data as Uint8Array chunks

      Returns JsonReader

    Methods

    • Closes the reader and releases any resources associated with it.

      Returns Promise<void>

      A Promise that resolves when the reader has been closed

    • Looks at the next token in the JSON stream without consuming it.

      This method allows you to check what type of token is coming next without advancing the reader position.

      Returns Promise<JsonToken>

      A Promise that resolves to the type of the next token

      Error if the reader is closed or the JSON is incomplete

    • Consumes the beginning of a JSON array.

      Returns Promise<void>

      Error if the next token is not the beginning of an array ('[')

    • Consumes the beginning of a JSON object.

      Returns Promise<void>

      Error if the next token is not the beginning of an object ('{')

    • Consumes the end of a JSON array.

      Returns Promise<void>

      Error if the next token is not the end of an array (']')

    • Consumes the end of a JSON object.

      Returns Promise<void>

      Error if the next token is not the end of an object ('}')

    • Checks if there are more elements in the current array or object.

      This method is typically used in a while loop to iterate through all elements in an array or all properties in an object.

      Returns Promise<boolean>

      A Promise that resolves to true if there are more elements, or false if the end of the current array or object has been reached

      Error if the reader is closed or the JSON is incomplete

      // Iterate through an array
      await reader.beginArray();
      while (await reader.hasNext()) {
      const value = await reader.nextString();
      console.log(value);
      }
      await reader.endArray();
    • Reads the name of the next property in a JSON object.

      Returns Promise<string>

      A Promise that resolves to the name of the property

      Error if the next token is not a property name

      await reader.beginObject();
      while (await reader.hasNext()) {
      const propertyName = await reader.nextName();
      // Process the property based on its name
      }
      await reader.endObject();
    • Reads the next JSON primitive value.

      Returns Promise<null | string | number | boolean>

      A Promise that resolves to the primitive value

      Error if the next token is not a primitive value

    • Reads the next value as a string.

      If the next value is already a string, it is returned as is. If the next value is another primitive type, it is converted to a string using JSON.stringify.

      Returns Promise<string>

      A Promise that resolves to the string value

      Error if the next token is not a primitive value

    • Reads the next value as a number.

      Returns Promise<number>

      A Promise that resolves to the number value

      Error if the next token is not a number

    • Reads the next value as a boolean.

      Returns Promise<boolean>

      A Promise that resolves to the boolean value

      Error if the next token is not a boolean

    • Reads the next value as null.

      Returns Promise<null>

      A Promise that resolves to null

      Error if the next token is not null

    • Skips the next value in the JSON stream.

      Returns Promise<void>

      Error if there is no more JSON to read, or if the next token is a property name

      // Skip properties you don't care about
      await reader.beginObject();
      while (await reader.hasNext()) {
      const name = await reader.nextName();
      if (name === "importantProperty") {
      const value = await reader.nextString();
      console.log(value);
      } else {
      await reader.skipValue(); // Skip this property
      }
      }
      await reader.endObject();
    • Reads the next complete JSON value (object, array, or primitive).

      This method parses and returns the entire next value in the JSON stream, regardless of its complexity. It's useful when you want to read a complete JSON structure without manually navigating through it.

      Returns Promise<unknown>

      A Promise that resolves to the parsed JSON value

      Error if the JSON is invalid or incomplete

      // Read a complete JSON object
      const result = await reader.nextJson();
      console.log(result.content.name);