pson

package module
v0.0.0-...-c656477 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: MIT Imports: 7 Imported by: 0

README

pson

This is an experimental implementation of progressive JSON similar to what is described in https://youtu.be/MaMQLNBZz64. The idea is based on progressive images where instead of loading the entire image from the top to the bottom over the network, a lower quality version of the entire image is sent first and then the details are filled in afterwards. This works by sending special JSON tags that correspond to data that is sent after the entire original object is sent, and this process can be repeated recursively, allowing data to not only be partially handled before all of it has been loaded, but also allowing cheaper to calculate data to be sent by the server before more expensive data is even ready to send at all.

Documentation

Overview

Package pson implements experimental support for progressive JSON.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(ctx context.Context, out ChunkWriter, in any, opts ...json.Options) error

Marshal encodes a piece of data as JSON and writes it to out. This works almost exactly like json.Marshal except that it adds specialized support for AsyncFunc values. When one is encountered during the marshaling process, it is called concurrently. Once it returns and after the original object that it was encountered in has finished being marshaled, the value it returns is sent to out as well. This is performed recursively, allowing an AsyncFunc to return a value containing more AsyncFunc values which will in turn all be called in a similar way.

Every marshal, first for in and then for the results of the various AsyncFunc calls, will be written to out as a separate chunk. These will be obtained, written to, and then immediately closed. These calls do not happen concurrently with other chunks being written.

Every call to an AsyncFunc is passed a context dervived from the provided ctx but that is canceled when Marshal returns.

Marshal does not return until all AsyncFunc calls have fully exited or until one of them returns an error or an error is encountered during the encoding process.

Types

type AsyncFunc

type AsyncFunc func(ctx context.Context) (any, error)

An AsyncFunc represents a piece of data that should be sent later. See Marshal for more information.

type ChunkWriter

type ChunkWriter interface {
	Chunk() (io.WriteCloser, error)
}

ChunkWriter is something that can be written to in pieces. This is a way for Marshal to make sure that data is sent after each piece of JSON is ready. For simple use-cases, see Chunk.

func Chunk

func Chunk(w io.Writer) ChunkWriter

Chunk returns a ChunkWriter that returns chunks that write directly to w and have no-op Close methods.

Directories

Path Synopsis
cmd
server command