Hacker News new | ask | show | jobs
by tynorf 2401 days ago
One technique I use is to create a struct that mirrors the layout of the target struct, then just cast between them. So if you have a

  type Person struct {
    FirstName string `json:"first_name"`
  }
And you get json like `{"name": "Alice"}`, you can decode it something like this:

  var doc struct {
    FirstName string `json:"name"`
  }
  json.Unmarshal(data, &doc)
  person := Person(doc)
I have not looked at the assembly so I can't say whether this incurs extra overhead or not.
1 comments

I thought of this, but I would have to do this for every single protobuf message. Also, anytime the protobuf changes, you would have to update the code.
Your protobuf messages are changing that much? My condolences.

ETA: Also, you already are needing to add code to every encode/decode point for the `map[string]interface{}` handling. :P