Optional string param

60 views
Skip to first unread message

Hüseyin Uslu

unread,
Mar 4, 2014, 6:49:37 PM3/4/14
to jay...@googlegroups.com
 I want to use a single optional parameter.

  [JsonRpcMethod("getwork")]
       
public Wallet.Responses.Getwork Getwork(string data = null)

Will this work with jayrock?

I want the data parameter to be optional.

Atif Aziz

unread,
Mar 5, 2014, 3:52:12 AM3/5/14
to jay...@googlegroups.com
Yes and no. It will work depending on how the JSON-RPC call was expressed. If arguments are sent as an array then the call will fail due to argument and parameter count mismatch. If arguments are sent as an object on the other hand, then it will work but results could depend on your expectations. Both cases are demonstrated by the program below:

using System;
using Jayrock.JsonRpc;

static class Program
{
    static void Main()
    {
        var dispatcher = JsonRpcDispatcherFactory.CreateDispatcher(new MyService());
        
        // This won't work due parameter count mismatch
        Console.WriteLine(dispatcher.Process(@"{ id: 1, method: getwork, params: [] }"));
        
        // This will work but data in method will be null, not "foo"
        Console.WriteLine(dispatcher.Process(@"{ id: 1, method: getwork, params: {} }"));
    }
}

class MyService : JsonRpcService
{
    [JsonRpcMethod("getwork", Idempotent = true)]
    public object Getwork(string data = "foo") { return data; }
}

Notice that I intentionally defined the default of the data parameter of Getwork as "foo" and you'll see that while the second call will go through, the argument received by the method will still be null

I wouldn't encourage relying too much on optional parameters. You should setup the defaults within the method body, like this:

    public object Getwork(string data) { return data ?? "foo"; }

That said, Jayrock has enough hooks available for you to modify the behavior and make optional parameters work how you'd like. If you're interested, we can dig deeper.

- Atif

Hüseyin Uslu

unread,
Mar 5, 2014, 4:27:36 AM3/5/14
to jay...@googlegroups.com
Hi there again,

Basically I'm not designing the underlying protocol and I should be suiting it's specifications.

So basically here's two samples of the json-rpc calls;

Call without any parameter
POST / HTTP/1.1
Authorization: Basic Y2RlY2tlcjphYmMxMjM=
Host: localhost:18332
Accept: */*
Accept-Encoding: deflate, gzip
Content-type: application/json
X-Mining-Extensions: longpoll midstate rollntime submitold
X-Mining-Hashrate: 583000000
Content-Length: 45
User-Agent: cgminer 2.8.1

{"method": "getwork", "params": [], "id":0}
Call with a parameter;

POST / HTTP/1.1
Authorization: Basic Y2RlY2tlcjphYmMxMjM=
Host: localhost:18332
Accept: */*
Accept-Encoding: deflate, gzip
Content-type: application/json
X-Mining-Extensions: longpoll midstate rollntime submitold
X-Mining-Hashrate: 661000000
Content-Length: 305
User-Agent: cgminer 2.8.1

{"method": "getwork", "params": [ "00000002b15704f4ecae05d077e54f6ec36da7f20189ef73b77603225ae56d2b00000000b052cbbdeed2489ccb13a526b77fadceef4caf7d3bb82a9eb0b69ebb90f9f5a7510c27fd1c0e8a37fa531338000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000" ], "id":1}
So basically it's sent using arrays and I guess I should be somehow digging deep then to get the stuff working.

Your help is much appreciated.

Hüseyin Uslu

unread,
Mar 5, 2014, 10:33:15 AM3/5/14
to jay...@googlegroups.com


That said, Jayrock has enough hooks available for you to modify the behavior and make optional parameters work how you'd like. If you're interested, we can dig deeper.



I checked the sources but couldn't find the hooks (and any samples) and I'm quite interested in them to make it work for me. 

Atif Aziz

unread,
Mar 5, 2014, 1:34:59 PM3/5/14
to jay...@googlegroups.com
I've posted a sample console app as a gist and also attaching it to this post.

Here's a brief explanation of how it's done…

A custom JsonRpcDispatcher implementation is registered. In an ASP.NET app, you could do this in Global.asax The custom implementation is to make the request available downstream by associating it with the service instance.

A custom attribute called JsonRpcOptionalParametersSupport, and which is decorated on Getwork, acts as a method description modifier (implementing IMethodModifier) and hijacks the invocation handler. During invocation, it checks if enough arguments are supplied before the target method is truly invoked. In case too few arguments are supplied, default values from optional parameters are added. If the JSON-RPC request used named arguments by supplying a JSON object for params then the implementation goes on to fill default values from target method's optional parameters where the parameter is absent in the source request.

Hopefully the rest of the sample is clear and self-explanatory.

- Atif



On Wed, Mar 5, 2014 at 4:33 PM, Hüseyin Uslu <shalafi...@gmail.com> wrote:


That said, Jayrock has enough hooks available for you to modify the behavior and make optional parameters work how you'd like. If you're interested, we can dig deeper.



I checked the sources but couldn't find the hooks (and any samples) and I'm quite interested in them to make it work for me. 

--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jayrock+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Program.cs

Hüseyin Uslu

unread,
Mar 5, 2014, 5:58:51 PM3/5/14
to jay...@googlegroups.com
thanks for your great support!

I'll be trying the sample code and integrating to my code.
Reply all
Reply to author
Forward
0 new messages