xjson

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

The xcover/json package encapsulates the basic JSON encoding/decoding operations for reading/writing a problem and writing out a solution, implementing the XReader and XWriter interfaces. Note that solutions are written out in the same form as problems. Henceforth, we shall refer to them simply as "problems".

Streaming decoders/encoders are used. This enables a sequence of problems of unknown number to be read/written. Also, it is not necessary to load the complete problem at one time.

The structure of a problem is a sequence of JSON objects: an array of integers defining the problem sizes; an array of items (strings); an array of secondary items (strings); a series of option arrays (strings). If there are no secondary items, the array should be empty.

The XReader and XWriter interfaces are shorthand wrappers around the JSON encoding/decoding. External logic must insure that the calls to them are in order (see xcover.SequenceReader) and recognize the end of the problem: e.g. EOF when reading options.

When writing out a sequence of solutions in JSON, there needs to be a way to delimit the different solutions. The assumed method is to emit an empty array after the last option in a solution. When reading a problem, an EOF or an empty array are used to check for end of the problem definition. Using the empty array delimiter means that one problem after another can be read from the same Decoder.

Here is Knuth's problem "(6)" in JSON following the above conventions:

[ 7, 6, 16 ]
[ "a", "b", "c", "d", "e", "f", "g" ]
[]
["c", "e"]
["a", "d", "g"]
["b", "c", "f"]
["a", "d", "f"]
["b", "g"]
["d", "e", "g"]

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonReader

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

func NewJsonReader

func NewJsonReader(r io.Reader) *JsonReader

NewJsonReader returns an XReader that reads JSON encoded problems/solutions from r.

func (*JsonReader) ReadItems

func (r *JsonReader) ReadItems(items []string) ([]string, error)

ReadItems expects a JSON array: "[ string... ]" The contents are not checked for correctness (e.g. each string unique).

func (*JsonReader) ReadOption

func (r *JsonReader) ReadOption(option []string) ([]string, error)

ReadItems expects a JSON array: "[ string... ]" The contents are not checked for correctness, especially with respect to other options. An zero length array--signaling the end of the options--is returned as such (not as io.EOF).

func (*JsonReader) ReadSecondaryItems

func (r *JsonReader) ReadSecondaryItems(sitems []string) ([]string, error)

ReadItems expects a JSON array: "[ string... ]" The contents are not checked for correctness (e.g. each string unique including wrt primary items).

func (*JsonReader) ReadSizes

func (r *JsonReader) ReadSizes(sizes []int) ([]int, error)

ReadSizes expects a JSON array: "[ int, int, int ]". However, any JSON array of ints will be returned without error.

type JsonWriter

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

func NewJsonWriter

func NewJsonWriter(w io.Writer) *JsonWriter

NewJsonWriter returns an XWriter that writes JSON encoded problems/solutions on w.

func (*JsonWriter) Close

func (w *JsonWriter) Close() error

Close the XWriter: subsequent write operations return an error. If the underlying io.Writer is a Closer, then it is closed. Subsequent calls to Close return nil.

func (*JsonWriter) WriteItems

func (w *JsonWriter) WriteItems(items []string) error

WriteItems encodes items as a JSON array: "[ string... ]"

func (*JsonWriter) WriteMarker

func (w *JsonWriter) WriteMarker() error

WriteMarker encodes an empty JSON array: "[ ]"

func (*JsonWriter) WriteOption

func (w *JsonWriter) WriteOption(option []string) error

WriteOption encodes option as a JSON array: "[ string... ]"

func (*JsonWriter) WriteSecondaryItems

func (w *JsonWriter) WriteSecondaryItems(sitems []string) error

WriteSecondaryItems encodes sitems as a JSON array: "[ string... ]"

func (*JsonWriter) WriteSizes

func (w *JsonWriter) WriteSizes(sizes []int) error

WriteSizes encodes items as a JSON array: "[ int... ]" up to a maximum length of 3. Other elements are ignored.

Jump to

Keyboard shortcuts

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