jsonrpc2

MoonBit jsonrpc2: provide simple JSON-RPC 2.0 abstractions.

json
json-rpc2
rpc2
moon add gmlewis/jsonrpc2@0.0.34
Download zip
Author
Version
0.0.34
License
Apache-2.0
Last updated
8 days ago
Downloads
90

Dependencies

README

#gmlewis/jsonrpc2

check

This is a simple abstraction for the JSON-RPC 2.0 specification: https://www.jsonrpc.org/specification based on the Go implementation: https://pkg.go.dev/golang.org/x/exp/jsonrpc2 which has copyright:
// Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.

#Status

The code has been updated to support compiler:

$ moon version --all moon 0.1.20260803 (c19f78e 2026-08-03) ~/.moon/bin/moon moonc v0.10.6+80dc50f24 (2026-08-04) ~/.moon/bin/moonc moonrun 0.1.20260803 (c19f78e 2026-08-03) ~/.moon/bin/moonrun

#
Framer

pub trait Framer {
fn reader(Self, &
ByteReader
) -> &Reader
fn writer(Self, &
Writer
) -> &Writer
}

Framer wraps low level byte readers and writers into jsonrpc2 message readers and writers. It is responsible for the framing and encoding of messages into wire form.

#
Reader

pub trait Reader {
fn read(Self) -> (Message?, Int64,
IOError
?)
}

Reader abstracts the transport mechanics from the JSON RPC protocol. A Conn reads messages from the reader it was provided on construction, and assumes that each call to Read fully transfers a single message, or returns an error. A reader is not safe for concurrent use, it is expected it will be used by a single Conn in a safe manner.

#
Writer

pub trait Writer {
fn write(Self, Message) -> (Int64,
IOError
?)
}

Writer abstracts the transport mechanics from the JSON RPC protocol. A Conn writes messages using the writer it was provided on construction, and assumes that each call to Write fully transfers a single message, or returns an error. A writer is not safe for concurrent use, it is expected it will be used by a single Conn in a safe manner.

#
HeaderFramer

pub struct HeaderFramer {
}

#
HeaderReader

pub struct HeaderReader {
in_ : &
ByteReader

}

#
HeaderWriter

pub struct HeaderWriter {
out : &
Writer

}

pub enum ID {
Number(Int64)
String(String)
} derive(Eq,
Debug
)

ID is used to identify the request and tie it to the response.
impl Show for ID
impl ToJson for ID

#
ID::number

fn ID::number(n : Int64) -> ID

#
ID::string

fn ID::string(s : String) -> ID

#
LineFramer

pub struct LineFramer {
}

#
LineReader

pub struct LineReader {
in_ : &
ByteReader

}

#
LineWriter

pub struct LineWriter {
out : &
Writer

}

#
Message

pub enum Message {
Request(Request)
Response(Response)
BatchRequest(Array[Request])
BatchResponse(Array[Response])
} derive(Eq,
Debug
)

Message covers all jsonrpc2 message types. They share no common functionality, but are a closed set of concrete types that are allowed to implement this interface. The message types are Request and Response.
impl Show for Message
impl ToJson for Message

#
Message::as_batch_request

fn Message::as_batch_request(self : Message) -> Array[Request]?

as_batch_request returns the message as a batch request if it is one.

#
Message::as_batch_response

fn Message::as_batch_response(self : Message) -> Array[Response]?

as_batch_response returns the message as a batch response if it is one.

#
Message::as_call

fn Message::as_call(self : Message) -> Request?

#
Message::as_notification

fn Message::as_notification(self : Message) -> Request?

#
Message::as_request

fn Message::as_request(self : Message) -> Request?

#
Message::as_response

fn Message::as_response(self : Message) -> Response?

#
RawFramer

pub struct RawFramer {
}

impl Framer for RawFramer

#
RawReader

type RawReader

impl Reader for RawReader

#
RawWriter

type RawWriter

impl Writer for RawWriter

#
Request

pub(all) struct Request {
id : ID?
method_ : String
params : Json
} derive(Eq,
Debug
)

A Request is a message sent to a peer to request behavior. If it has an ID then it is a call and therefore requests a response, otherwise it is a notification which receives no response.
impl Show for Request
impl ToJson for Request

#
Response

pub(all) struct Response {
id : ID
result : Result[Json, WireError]
} derive(Eq,
Debug
)

Response is a Message used as a reply to a call Request. It will have the same ID as the call it is a response to.
impl Show for Response
impl ToJson for Response

#
WireError

pub(all) struct WireError {
code : Int
message : String
data : Json?
} derive(Eq, ToJson,
Debug
,
FromJson
)

WireError is a jsonrpc2 error message.
impl Show for WireError

#
header_framer

fn header_framer() -> &Framer

header_framer returns a new Framer. The messages are sent with HTTP content length and MIME type headers. This is the format used by LSP and others.

#
line_framer

fn line_framer() -> &Framer

line_framer returns a new Framer. The messages are sent with a terminating cr+newline, and rely on json decode consistency to determine message boundaries.

#
new_batch_request

fn new_batch_request(messages : Array[Message]) -> Message

new_batch_request constructs a new batch request message for the supplied requests.

#
new_batch_response

fn new_batch_response(messages : Array[Message]) -> Message

new_batch_response constructs a new batch response message for the supplied responses.

#
new_call

fn new_call(id : ID, method_ : String, params : Json) -> Message

new_call constructs a new call message for the supplied ID, method and parameters.

#
new_notification

fn new_notification(method_ : String, params : Json) -> Message

new_notification constructs a new notification message for the supplied method and parameters.

#
new_response

fn new_response(id : ID, result : Result[Json, WireError]) -> Message

new_response constructs a new Response message.

#
raw_framer

fn raw_framer() -> &Framer

raw_framer returns a new Framer. The messages are sent with no wrapping, and rely on json decode consistency to determine message boundaries.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io