FAQ
Hello Gophers

I use RawMessage to precompute part of a json message.

RawMessage is a raw encoded JSON object. It implements Marshaler and
Unmarshaler and can be used to delay JSON decoding or precompute a JSON
encoding.


from http://golang.org/pkg/encoding/json/#RawMessage


Following code shows the problem:

package main

import "encoding/json"

type S struct{ R json.RawMessage }

func main() {
s := S{json.RawMessage([]byte(`42`))}
b, _ := json.Marshal(s)
println(string(b)) // {"R":"NDI="} Expected: {"R":42}
}


Is this behaviour expected?
I have made a patch to go tip to fix this, should i send it for review?

Thank you for this great language!

Ron





--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Kyle Lemons at Sep 17, 2013 at 11:23 pm
    You need a pointer:

    http://play.golang.org/p/b8fzGMxrMC

    On Tue, Sep 17, 2013 at 6:51 AM, ron wrote:

    Hello Gophers

    I use RawMessage to precompute part of a json message.

    RawMessage is a raw encoded JSON object. It implements Marshaler and
    Unmarshaler and can be used to delay JSON decoding or precompute a JSON
    encoding.


    from http://golang.org/pkg/encoding/json/#RawMessage


    Following code shows the problem:

    package main

    import "encoding/json"

    type S struct{ R json.RawMessage }

    func main() {
    s := S{json.RawMessage([]byte(`42`))}
    b, _ := json.Marshal(s)
    println(string(b)) // {"R":"NDI="} Expected: {"R":42}
    }


    Is this behaviour expected?
    I have made a patch to go tip to fix this, should i send it for review?

    Thank you for this great language!

    Ron





    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ron at Sep 19, 2013 at 11:35 am
    Thank you Kyle completely missed the pointer in the receiver.
    On Wednesday, September 18, 2013 1:23:25 AM UTC+2, Kyle Lemons wrote:

    You need a pointer:

    http://play.golang.org/p/b8fzGMxrMC


    On Tue, Sep 17, 2013 at 6:51 AM, ron <burch...@gmail.com <javascript:>>wrote:
    Hello Gophers

    I use RawMessage to precompute part of a json message.

    RawMessage is a raw encoded JSON object. It implements Marshaler and
    Unmarshaler and can be used to delay JSON decoding or precompute a JSON
    encoding.


    from http://golang.org/pkg/encoding/json/#RawMessage


    Following code shows the problem:

    package main

    import "encoding/json"

    type S struct{ R json.RawMessage }

    func main() {
    s := S{json.RawMessage([]byte(`42`))}
    b, _ := json.Marshal(s)
    println(string(b)) // {"R":"NDI="} Expected: {"R":42}
    }


    Is this behaviour expected?
    I have made a patch to go tip to fix this, should i send it for review?

    Thank you for this great language!

    Ron





    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Johann Höchtl at Feb 26, 2014 at 4:52 pm

    Am Mittwoch, 18. September 2013 01:23:25 UTC+2 schrieb Kyle Lemons:
    You need a pointer:
    I have a struct like that

    struct {
       Data interface{}
    }

    and Data should either be copied verbatim oder base64 encoded. data always
    ends up in base64. Where is the problem?

    http://play.golang.org/p/tApLjVyrqN

       if ext == "json" {
        // if the data is already json, include it verbatim;
        // no sanity check is happening here; if the input is corrupt,
        // the resulting datapackage file will be corrupt too
        res.Data = json.RawMessage(*rawdata)
       } else {
        // otherwise base64 encode it. The JSON-serializer will base64 encode our []byte stream
        // so no explicit encoding neccessary here
        res.Data = *rawdata
       }

       if ext == "json" {
    // if the data is already json, include it verbatim;
    // no sanity check is happening here; if the input is corrupt,
    // the resulting datapackage file will be corrupt too
    res.Data = json.RawMessage(*rawdata)
    } else {
    // otherwise base64 encode it. The JSON-serializer will base64 encode our []byte stream
    // so no explicit encoding neccessary here
    res.Data = *rawdata
    }
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Johann Höchtl at Feb 26, 2014 at 4:54 pm

    Am Mittwoch, 18. September 2013 01:23:25 UTC+2 schrieb Kyle Lemons:
    You need a pointer:

    I have a struct like that

    struct {
    Data interface{}
    }

    and Data should either be copied verbatim oder base64 encoded. data always
    ends up in base64. Where is the problem?

    http://play.golang.org/p/tApLjVyrqN
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kevin Gillette at Feb 26, 2014 at 10:23 pm
    So it was agreed that this doesn't constitute a bug, and thus cannot be
    fixed because of the Go 1 compatibility guarantee?
    On Tuesday, September 17, 2013 5:23:25 PM UTC-6, Kyle Lemons wrote:

    You need a pointer:

    http://play.golang.org/p/b8fzGMxrMC


    On Tue, Sep 17, 2013 at 6:51 AM, ron <burch...@gmail.com <javascript:>>wrote:
    Hello Gophers

    I use RawMessage to precompute part of a json message.

    RawMessage is a raw encoded JSON object. It implements Marshaler and
    Unmarshaler and can be used to delay JSON decoding or precompute a JSON
    encoding.


    from http://golang.org/pkg/encoding/json/#RawMessage


    Following code shows the problem:

    package main

    import "encoding/json"

    type S struct{ R json.RawMessage }

    func main() {
    s := S{json.RawMessage([]byte(`42`))}
    b, _ := json.Marshal(s)
    println(string(b)) // {"R":"NDI="} Expected: {"R":42}
    }


    Is this behaviour expected?
    I have made a patch to go tip to fix this, should i send it for review?

    Thank you for this great language!

    Ron





    --
    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Johann Höchtl at Feb 27, 2014 at 6:57 am

    Am Mittwoch, 26. Februar 2014 23:23:22 UTC+1 schrieb Kevin Gillette:
    So it was agreed that this doesn't constitute a bug, and thus cannot be
    fixed because of the Go 1 compatibility guarantee?
    Actually my previous post reports a new problem, not that of the OP but the
    formating turned out strange. I will post a new question.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 17, '13 at 3:17p
activeFeb 27, '14 at 6:57a
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase