marcjson

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package marcjson reads and writes MARC 21 records in the de-facto "MARC-in-JSON" structure (the pymarc/ruby-marc/marc4j layout), implementing codex.RecordReader and codex.RecordWriter with a hand-rolled tokenizer (see scan.go) rather than encoding/json, so both directions avoid reflection and most per-token allocation.

A record is a JSON object with a "leader" string and an ordered "fields" array. Each field is a single-key object whose key is the 3-character tag: a control field maps the tag to a string value, while a data field maps it to an object with "ind1"/"ind2" strings and an ordered "subfields" array of single-key {code: value} objects:

{"leader":"…","fields":[
  {"001":"ocm12345"},
  {"245":{"ind1":"1","ind2":"0","subfields":[{"a":"Title"},{"c":"Author"}]}}
]}

Decoding walks JSON tokens (no reflection), preserving field and subfield order, and accepts a single object, a whitespace-separated stream of objects, or a top-level array. A Writer emits a JSON array and must be closed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(b []byte) (*codex.Record, error)

Decode parses a single record from a MARC-in-JSON byte slice.

func Encode

func Encode(r *codex.Record) ([]byte, error)

Encode serializes a record as a single compact MARC-in-JSON object. It returns an error if a value is not valid UTF-8 (which JSON cannot represent).

func ReadFile

func ReadFile(path string) ([]*codex.Record, error)

ReadFile reads every record from the named MARC-in-JSON file. On the first malformed record it returns the records parsed so far together with the error.

func WriteFile

func WriteFile(path string, records []*codex.Record) error

WriteFile writes every record to the named file as a JSON array, creating it or truncating an existing file.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader reads MARC records from a MARC-in-JSON stream one record at a time. It accepts a single object, a whitespace-separated stream of objects, or a top-level array of objects.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a Reader that reads records from r.

func (*Reader) All

func (rd *Reader) All() iter.Seq2[*codex.Record, error]

All returns an iterator over the remaining records, for use as "for rec, err := range r.All()". It stops at the first error.

func (*Reader) Read

func (rd *Reader) Read() (*codex.Record, error)

Read returns the next record, or io.EOF when the stream is exhausted.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer streams records into a JSON array. Close must be called to emit the closing "]" and complete the document.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a Writer that writes a JSON array of records to w.

func (*Writer) Close

func (wr *Writer) Close() error

Close writes the closing "]". It must be called to produce a complete document; an empty array is emitted if no records were written.

func (*Writer) Write

func (wr *Writer) Write(r *codex.Record) error

Write serializes one record as an element of the JSON array, emitting the opening "[" before the first record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL