Files
std/json/json.sakura
T
2026-11-10 09:44:01 -06:00

181 lines
8.3 KiB
Plaintext

#! @file json.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains JSON's errors, and the functions that write a value as JSON and read
#! one back by what `@serial` marks of its type, over `std::serial`.
#!
#! JSON is read strictly, as RFC 8259 writes it: any value may stand alone, text
#! must be UTF-8, and comments, trailing commas, `NaN`, `Infinity`, and single
#! quotes are errors. `json::parse` reads text into a `json::document`, which holds
#! every value of it, `json::reader` gives one token at a time, and `json::writer`
#! writes JSON as it is given it.
import std::io;
module std::json;
#! Why JSON could not be read or written.
pub enum error_kind: u8 {
unexpected_end, #! The text ends before the value does.
unexpected_byte, #! A byte stands where JSON allows none of its kind.
invalid_escape, #! A string holds an escape JSON has not, or half a surrogate pair.
invalid_utf8, #! Text is not UTF-8.
invalid_number, #! A number breaks JSON's grammar, as `01`, `1.`, or `.5` do.
too_deep, #! Values nest deeper than the limit the text was read with.
too_long, #! The text is longer than the 4 GiB a document holds.
trailing_text, #! Something follows the value.
not_finite, #! A number written is infinite or NaN, which JSON cannot hold.
not_written, #! The stream the text was passed on to failed, as its own error says.
# What `std::serial` finds reading and writing typed values, as its own kinds say.
wrong_kind, #! A value is of another kind than the type asked for, as text for a number.
missing_member, #! An object lacks a member for a field that is not a `maybe`.
unknown_member, #! An object has a member no field takes, which only strict reading refuses.
repeated_member, #! An object has a member twice, which only strict reading refuses.
out_of_range, #! A number does not fit the type asked for, or a name is no member's.
not_serial, #! A value holds an enum member or union field that is not `@serial`.
}
#! Why JSON could not be read or written, and where in the text.
#!
#! An error is plain data, so `else (e)` names it. One from writing has no
#! place, and is at line 0.
pub struct error {
pub var kind: error_kind; #! What went wrong.
pub var offset: u64; #! The byte of the text the error is at.
pub var line: u32; #! Counted from 1.
pub var column: u32; #! The byte within the line, counted from 1.
}
#! Says what the error is, in a sentence, after where it is.
#! @param this The error to describe.
#! @returns The description, as `1:5: The text ends before the value does.`
pub def error::describe(&this): string {
if this.line == 0 {
return string::from(describe(this.kind), nothing {});
}
return $"{this.line}:{this.column}: {describe(this.kind)}";
}
#! Says what a kind of error is, in a sentence.
#! @param kind The kind to describe.
#! @returns The sentence.
pub def describe(kind: error_kind): strview {
return match kind {
unexpected_end => "The text ends before the value does.",
unexpected_byte => "A byte stands where JSON allows none of its kind.",
invalid_escape => "A string holds an escape JSON does not have, or half of a surrogate pair.",
invalid_utf8 => "The text is not UTF-8.",
invalid_number => "A number does not follow JSON's grammar.",
too_deep => "Values nest deeper than the limit the text is read with.",
too_long => "The text is longer than the 4 GiB a document holds.",
trailing_text => "Something follows the value.",
not_finite => "A number is infinite or NaN, which JSON cannot hold.",
not_written => "The stream the text was written to failed, as its own error says.",
wrong_kind => serial::describe(serial::error_kind::wrong_kind),
missing_member => serial::describe(serial::error_kind::missing_member),
unknown_member => serial::describe(serial::error_kind::unknown_member),
repeated_member => serial::describe(serial::error_kind::repeated_member),
out_of_range => serial::describe(serial::error_kind::out_of_range),
not_serial => serial::describe(serial::error_kind::not_serial),
};
}
#! The error JSON gives for one `std::serial` gives, at the same place.
#! @param problem The error reading or writing a typed value gave.
#! @returns The error.
pub def error::from(problem: &serial::error): error {
return error {
kind = of_serial(problem.kind),
offset = problem.offset,
line = problem.line,
column = problem.column
};
}
# The kind of JSON's error for a kind of `std::serial`'s.
def of_serial(kind: serial::error_kind): error_kind {
return match kind {
wrong_kind => error_kind::wrong_kind,
missing_member => error_kind::missing_member,
unknown_member => error_kind::unknown_member,
repeated_member => error_kind::repeated_member,
out_of_range => error_kind::out_of_range,
not_serial => error_kind::not_serial,
};
}
# An error of a kind at a byte of the text.
def error_at(kind: error_kind, text: strview, offset: u64): error {
val at := serial::place_of(text, offset);
return error { kind = kind, offset = offset, line = at.line, column = at.column };
}
#! Writes a typed value through a writer, by what `@serial` marks of its type,
#! or the form of its own the type gives, among whatever else the writer is
#! given.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param out The writer to write it with.
pub def encode<T>(value: &T, out: &mut writer): void {
serial::encode(value, out);
}
#! Writes a typed value as JSON text, by what `@serial` marks of its type.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param indent Spaces for each level of nesting, or 0 to write it on one
#! line.
#! @param alloc An optional allocator for the text to take memory from.
#! @returns The text, or the first failure in the value, as a number JSON cannot
#! hold, or an enum member or union field that is not marked.
pub def to_text<T>(value: &T, indent: u32 = 0, alloc: maybe<&mut allocator> = nothing {}): result<string, error> {
var out := writer::indented(indent, alloc);
serial::encode(value, &mut out);
_ = out.finish() else (e) { return e; };
return move(out._text);
}
#! Writes a typed value as JSON text into a stream, by what `@serial` marks of
#! its type, passing the text on as it goes rather than holding it all.
#! @tparam T The type of the value.
#! @param value The value to write.
#! @param out The stream to write to, which is not flushed: whoever made it
#! does that.
#! @param indent Spaces for each level of nesting, or 0 to write it on one
#! line.
#! @returns Nothing, or the first failure in the value, or `not_written` where
#! the stream failed, as its own error then says.
pub def write_to<T>(value: &T, out: &mut writable, indent: u32 = 0): result<void, error> {
var w := writer::to(out, indent);
serial::encode(value, &mut w);
_ = w.finish() else (e) { return e; };
return;
}
#! Reads a typed value from a value of a document, by what `@serial` marks of
#! its type, or the form of its own the type gives.
#! @tparam T The type to read.
#! @param from The value to read.
#! @param given How strictly to read: strictly, a member no field takes, and a
#! member written more than once, are errors.
#! @returns The value, or why it could not be read, at the value that failed.
pub def decode<T>(from: value, given: serial::options = serial::options {}): result<T, error> where T: zeroable {
var made := serial::decode::<T>(&from._document[0], from._at, given) else (e) { return error::from(&e); };
return move(made);
}
#! Reads a typed value from JSON text, by what `@serial` marks of its type.
#! @tparam T The type to read.
#! @param text The text to read.
#! @param given How strictly to read.
#! @returns The value, or why the text is not JSON, or could not be read as the
#! type.
pub def from_text<T>(text: strview, given: serial::options = serial::options {}): result<T, error> where T: zeroable {
val read := parse(text) else (e) { return e; };
var made := decode::<T>(read.root, given) else (e) { return e; };
return move(made);
}