在golang中使用json-rpc,将id作为字符串。

huangapple go评论90阅读模式
英文:

json-rpc in golang, id as string

问题

我对Go语言还比较新。

我使用这个包https://github.com/kdar/httprpc来进行我的json-rpc v 1.0请求(因为golang只实现了2.0)。

我有一个问题,我调用的这个服务器将"id"作为字符串返回,例如

"id":"345"

而不是

"id":345

我找到的唯一方法是重新定义clientResponse,使用字符串而不是uint64

type clientResponse struct {
    Result *json.RawMessage `json:"result"`
    Error  interface{}      `json:"error"`
    Id     string           `json:"id"`
}

并重新定义相同的DecodeClientResponse函数来使用我的clientResponse

而不是调用CallJson,我调用(使用DecodeClientResponse而不是gjson.DecodeClientResponse):

httprpc.CallRaw(address, method, &params, &reply, "application/json",
            gjson.EncodeClientRequest, DecodeClientResponse)

我觉得这样做很丑陋,有没有更好的方法?

谢谢

英文:

I'm fairly new to go.

I use this package https://github.com/kdar/httprpc to do my json-rpc v 1.0 requests (as golang only implements 2.0)

I have a problem, this server i'm calling return the "id" as a string, like

"id":"345"

instead of

"id":345

The only way I found was to redefine clientResponse using string instead of uint64

type clientResponse struct {
	Result *json.RawMessage `json:"result"`
    Error  interface{}      `json:"error"`
    Id     string           `json:"id"`
}

and to redefine the exacte same DecodeClientResponse function to use my clientResponse

and instead of CallJson, I call (DecodeClientResponse instead of gjson.DecodeClientResponse):

httprpc.CallRaw(address, method, &params, &reply, "application/json",
            gjson.EncodeClientRequest, DecodeClientResponse)

I find this quite ugly, is there any way to do better ?

Thanks

答案1

得分: 2

The json-rpc v 1.0 specifies that:

>id - The request id. This can be of any type. It is used to match the response with the request that it is replying to.

That is, id can be anything (even an array), and the server response should contain the same value and type for id, which in your case it doesn't do. So, the server you communicate with is not doing its job correctly, and is not following the json-rpc v 1.0 specs.

So, yes, you need to do the "ugly" solution and create a new decoder function for this 'broken' server. Jeremy Wall's suggestion works (but int should be changed to uint64) and should at least make you avoid using string as type.

Edit

I don't know the httprpc package enough to know how it handles the Id value. But if you want either string or int, you should be able to set Id in clientResponse to:

Id interface{} `json:"id"`

When checking the value in Id you use a type switch:

var id int
// response is of type clientResponse
switch t := response.Id.(type) {
default:
    // Error. Bad type
case string:
    var err error
    id, err = strconv.Atoi(t)
    if err != nil {
        // Error. Not possible to convert string to int
    }
case int:
    id = t
}
// id now contains your value
英文:

The json-rpc v 1.0 specifies that:

>id - The request id. This can be of any type. It is used to match the response with the request that it is replying to.

That is, id can be anything (even an array), and the server response should contain the same value and type for id, which in your case it doesn't do. So, the server you communicate with is not doing its job correctly, and is not following the json-rpc v 1.0 specs.

So, yes, you need to do the "ugly" solution and create a new decoder function for this 'broken' server. Jeremy Wall's suggestion works (but int should be changed to uint64) and should at least make you avoid using string as type.

Edit

I don't know the httprpc package enough to know how it handles the Id value. But if you want either string or int, you should be able to set Id in clientResponse to:

Id interface{} `json:"id"`

When checking the value in Id you use a type switch:

var id int
// response is of type clientResponse
switch t := response.Id.(type) {
default:
    // Error. Bad type
case string:
    var err error
    id, err = strconv.Atoi(t)
    if err != nil {
        // Error. Not possible to convert string to int
    }
case int:
    id = t
}
// id now contains your value

答案2

得分: 1

尝试

type clientResponse struct {
    Result *json.RawMessage `json:"result"`
    Error  interface{}      `json:"error"`

    // 告诉 encoding/json 这个字段是以 json 字符串的形式编码的,尽管类型是 int。
    Id     int           `json:"id,string"`
}

只要库在底层使用 encoding/json,这段代码应该可以工作。

英文:

Try

type clientResponse struct {
    Result *json.RawMessage `json:"result"`
    Error  interface{}      `json:"error"`

    # Tell encoding/json that the field is
    # encoded as a json string even though the type is int.
    Id     int           `json:"id,string"`
}

As long as the libraries are using encoding/json under the hood this should work.

huangapple
  • 本文由 发表于 2013年3月30日 09:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/15713983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定