JSON-RPC and LSP: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= This is probably the quickest thing to put my nose into. So here goes with rust =Rust Example= To get going we need <syntaxhighlight lang="toml"> [dependencies] jsonrpc-core = "18.0.0" jsonrpc-http-server = "18.0.0" serde_json = "1.0.140" </syntaxhighlight> And here is the code. Not must to discuss. The ServerBuilder I thought was pretty cool. <syntaxhighlight lang="rs"> fn main() { let mut io = IoHandler::default(); io.add_method("say_hello", |_p..."
 
Line 52: Line 52:
     "result": 22,
     "result": 22,
     "id": 1
     "id": 1
}
</syntaxhighlight>
And in error (provided it is valid json)
<syntaxhighlight lang="json">
{
    "jsonrpc": "2.0",
    "error": {
        "code": -32602,
        "message": "Invalid parameters"
    },
    "id": 1
}
</syntaxhighlight>
With invalid json there is no id
<syntaxhighlight lang="json">
{
    "jsonrpc": "2.0",
    "error": {
        "code": -32700,
        "message": "Parse error"
    },
    "id": null
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 02:46, 29 March 2025

Introduction

This is probably the quickest thing to put my nose into. So here goes with rust

Rust Example

To get going we need

[dependencies]
jsonrpc-core = "18.0.0"
jsonrpc-http-server = "18.0.0"
serde_json = "1.0.140"

And here is the code. Not must to discuss. The ServerBuilder I thought was pretty cool.

fn main() {
    let mut io = IoHandler::default();

    io.add_method("say_hello", |_params: Params| async {
        Ok( Value::String("Hello, world!".to_string()))
    });

    io.add_method("add", |params: Params| async {
        let tuple = params.parse::<(i32, i32)>();
        match tuple {
            Ok((a, b)) => Ok(Value::Number((a + b).into())),
            Err(_) => Err(jsonrpc_core::Error::invalid_params("Invalid parameters")),
            
        }
    });
   
    let server = ServerBuilder::new(io)
    .threads(3)
    .start_http(&"0.0.0.0:8003".parse().unwrap())
    .unwrap();

    println!("Server is up");

    server.wait();
}

And finally the JSON to send

{
  "jsonrpc": "2.0",
  "method": "add",
  "params": [10,12],
  "id": 1
}

And the no surprises response.

{
    "jsonrpc": "2.0",
    "result": 22,
    "id": 1
}

And in error (provided it is valid json)

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32602,
        "message": "Invalid parameters"
    },
    "id": 1
}

With invalid json there is no id

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32700,
        "message": "Parse error"
    },
    "id": null
}