Standard library / std::json

std::json

std::json provides JSON parsing and stringifying (implementation + expanded subset).

Full reference: json.

Notes#

  • Supported forms + design: parsing and stringify are implemented; the DOM/query surface is still evolving.
  • Full reference: json

Importing#

import std::json;
import std::strings;

Exported API#

Examples#

Example: parse + query + stringify#

import std::json;
import std::strings;
import std::result;
import std::memory;

type StringAllocResult = std::result::Result(std::strings::String, std::memory::OutOfMemory);

fn main () -> int {
  let mut doc: Document = Document{};
  let input: string = `{"a":1,"b":true,"c":null,"d":["x","y"],"u":"\u0041"}`;

  match (doc.parse(input)) {
    Ok(root) => {
      if !doc.is_ok() {
        doc.drop();
        return 2;
      }

      let u_id_opt = doc.object_get(root, "u");
      if u_id_opt == None {
        doc.drop();
        return 3;
      }
      let u_id: i64 = u_id_opt ?? 0 as i64;
      let u_opt = doc.as_string(u_id);
      if u_opt == None {
        doc.drop();
        return 4;
      }
      if (u_opt ?? "") != "A" {
        doc.drop();
        return 5;
      }

      let a_id_opt = doc.object_get(root, "a");
      if a_id_opt == None {
        doc.drop();
        return 6;
      }
      let a_id: i64 = a_id_opt ?? 0 as i64;
      let a_num_opt = std::json::number_as_i64(doc, a_id);
      if a_num_opt == None {
        doc.drop();
        return 7;
      }
      if (a_num_opt ?? 0 as i64) != 1 {
        doc.drop();
        return 8;
      }

      match (std::json::stringify(doc, root)) {
        StringAllocResult::Ok(compact_value) => {
          let mut compact: std::strings::String = compact_value;
          let expected: string = `{"a":1,"b":true,"c":null,"d":["x","y"],"u":"A"}`;
          if compact.as_string() != expected {
            compact.drop();
            doc.drop();
            return 10;
          }

          compact.drop();
          doc.drop();
          return 0;
        },
        StringAllocResult::Err(_) => {
          doc.drop();
          return 9;
        },
      }
    },
    Err(_) => {
      doc.drop();
      return 1;
    },
  }
}

See also#

  • Full reference: json

Source repository · Edit this page · View Markdown