duanhao5038 2018-11-29 20:21
浏览 94
已采纳

从和转换成消息包

So I'm trying to get the string representation of a JSON message in Golang. I just want to receive the messagepack encoded JSON, modify some values and send it back.

I haven't found an easy way to do it. Most of the times, I can't know in advance what is the structure of the JSON (apart from having JSON structure), so want I want to do is to receive the binary message. Decode it as a JSON, print it as a string to the standard output, modify the content, convert it to MessagePack again and send it back.

I've been looking at these two packages, but I don't know how to properly use them for a simple task like that:

So I will receive something like this:

DF 00 00 00 01 A7 6D 65 73 73 61 67 65 A3 48 69 21

I want to print this:

{"message": "Hi!"}

Modify the "Hi!":

{"message": "Hello Sir!"}

Send it as messagepack:

DF 00 00 00 01 A7 6D 65 73 73 61 67 65 AA 48 65 6C 6C 6F 20 53 69 72 21

Current Python code I'm trying to port to Golang:

def decode_msgpack(jsonData):
    packedStuff = 0
    for key in jsonData.keys():
        if type(jsonData[key]) is bytes:
            packedStuff += 1
            try:
                jsonData[key] = umsgpack.unpackb(jsonData[key])
            except umsgpack.InvalidStringException:
                try:
                    jsonData[key] = umsgpack.unpackb(jsonData[key], allow_invalid_utf8=True)
                except umsgpack.InsufficientDataException:
                    print("[!] InsufficientDataException")
                    jsonData[key] = base64.b64encode(jsonData[key]).decode('utf-8')
                else:
                    jsonData[key] = base64.b64encode(jsonData[key]).decode('utf-8')

    if packedStuff > 0:
        return decode_msgpack(jsonData)
    else:
        return jsonData
  • 写回答

2条回答 默认 最新

  • dongyoufo5672 2018-11-29 20:39
    关注

    Using the codec library and assuming that {"message": "Hi"} is a map, the code would look something like this.

    package main
    
    import (
            "fmt"
    
            "github.com/ugorji/go/codec"
    )
    
    func main() {
            var data []byte
            original := map[string]string{"message": "Hi!"}
            enc := codec.NewEncoderBytes(&data, new(codec.MsgpackHandle))
            if err := enc.Encode(&original); err != nil {
                    panic(err)
            }
            fmt.Printf("Encoded: ")
            for _, b := range data {
                    fmt.Printf("%X ", b)
            }
            fmt.Printf("
    ")
            decoded := make(map[string]string)
            dec := codec.NewDecoderBytes(data, new(codec.MsgpackHandle))
            if err := dec.Decode(&decoded); err != nil {
                    panic(err)
            }
            fmt.Printf("Decoded: %v
    ", decoded)
            decoded["message"] = "Hello Sir!"
            /* reinitialize the encoder */
            enc = codec.NewEncoderBytes(&data, new(codec.MsgpackHandle))
            if err := enc.Encode(&decoded); err != nil {
                    panic(err)
            }
            fmt.Printf("Encoded: ")
            for _, b := range data {
                    fmt.Printf("%X ", b)
            }
            fmt.Printf("
    ")
    }
    

    That said, if you get {"message": "Hi"} as a JSON string, you can use codec to decode the JSON into a map, update the map and then re-encode it as msgpack.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)