b %s {"Name":"World","Id":2} b2 %s {"Name":"Hello","Id":1,"Json":"eyJOYW1lIjoiV29ybGQiLCJJZCI6Mn0="} d.Json %s "eyJOYW1lIjoiV29ybGQiLCJJZCI6Mn0="
package main
import (
"encoding/json"
"fmt"
)
type Data struct {
Name string
Id int
Json json.RawMessage
}
func main() {
tmp := struct {
Name string
Id int
}{"World", 2}
b, err := json.Marshal(tmp)
if err != nil {
fmt.Println("Error %s", err.Error())
}
fmt.Println("b %s", string(b))
test := Data{"Hello", 1, b}
b2, err := json.Marshal(test)
if err != nil {
fmt.Println("Error %s", err.Error())
}
fmt.Println("b2 %s", string(b2))
var d Data
err = json.Unmarshal(b2, &d)
if err != nil {
fmt.Println("Error %s", err.Error())
}
fmt.Println("d.Json %s", string(d.Json))
}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
We can only use it in cases where no sane program would rely on the broken behaviour.
--
Unfortunately, there are many, many other ways in which this could cause a program to break or change behavior. You're effectively changing the method sets of any type that embeds a json.RawMessage, for instance. It's concievable that someone would embed a RawMessage and use a pointer for unmarshaling, so that the raw JSON is stuck in the RawMessage field, and then they parse it into other fields from there while allowing the JSON encoder to not realize that it is a RawMessage when marshaling a value of it.
I think you're also making the code `x := (json.RawMessage).MarshalJSON` fail to compile, and making `y := foo.MarshalJSON` potentially cause foo to escape.
On Monday, October 7, 2013, Kyle Lemons wrote:
Unfortunately, there are many, many other ways in which this could cause a program to break or change behavior. You're effectively changing the method sets of any type that embeds a json.RawMessage, for instance. It's concievable that someone would embed a RawMessage and use a pointer for unmarshaling, so that the raw JSON is stuck in the RawMessage field, and then they parse it into other fields from there while allowing the JSON encoder to not realize that it is a RawMessage when marshaling a value of it.They'd have to be marshalling it into a base 64 representation of the json encoding of the data, which would be nonsense. There's no reason you'd base 64 encode something that was already text. Unless I'm not catching your meaning...
I think you're also making the code `x := (json.RawMessage).MarshalJSON` fail to compile, and making `y := foo.MarshalJSON` potentially cause foo to escape.The opposite. The declaration of x wouldn't compile currently, but would with the change, and that of y could cause foo to escape now but wouldn't with the change.
On Fri, Oct 11, 2013 at 10:28 AM, Steven Blenkinsop <stev...@gmail.com> wrote:They'd have to be marshalling it into a base 64 representation of the json encoding of the data, which would be nonsense. There's no reason you'd base 64 encode something that was already text. Unless I'm not catching your meaning...It is admittedly contrived, but it works and is a perfectly legal use of method sets:
The opposite. The declaration of x wouldn't compile currently, but would with the change, and that of y could cause foo to escape now but wouldn't with the change.Right, I got myself switched around. `x := (*json.RawMessage).MarshalJSON` will compile now, but fail in the future.
Right, I got myself switched around. `x := (*json.RawMessage).MarshalJSON` will compile now, but fail in the future.