FAQ
Let's say I have an application that receives json data in two different
formats.

     f1 = `{"pointtype":"type1", "data":{"col1":"val1", "col2":"val2"}}`
     f2 = `{"pointtype":"type2", "data":{"col3":"val3", "col3":"val3"}}`

And I have a struct associated to each type:

     type F1 struct {
       col1 string
       col2 string
     }

     type F2 struct {
       col3 string
       col4 string
     }

Assuming that I use the `encoding/json` library to turn the raw json data
into the struct:
     type Point {
       pointtype string
       data json.RawMessage
     }

How can I decode the data into the appropiate struct just by knowing the
pointtype?

I was trying something along the lines of :

     func getType(pointType string) interface{} {
     switch pointType {
     case "f1":
     var p F1
     return &p
     case "f2":
     var p F2
     return &p
     }
     return nil
     }

Which is not working because the returned value is an interface, not the
proper struct type.
How can I make this kind of switch struct selection work?

[here is a non working example](http://play.golang.org/p/mjNeWlAw6j)

--
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/d/optout.

Search Discussions

  • James Wendel at Oct 23, 2014 at 4:48 am
    Assuming the code in your post is what you are using, make sure vars you want populated in your struct are public (start with a capital letter). I believe the json lib needs that. See the docs for the json package for details.

    For the type check, use type assertion I believe.
    http://golang.org/ref/spec#Type_assertions

    --
    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/d/optout.
  • peterGo at Oct 23, 2014 at 6:49 am
    http://play.golang.org/p/I6xeWTlPst

    Peter

    On Wednesday, October 22, 2014 11:07:24 PM UTC-4, manuel.ga...@gmail.com
    wrote:
    Let's say I have an application that receives json data in two different
    formats.

    f1 = `{"pointtype":"type1", "data":{"col1":"val1", "col2":"val2"}}`
    f2 = `{"pointtype":"type2", "data":{"col3":"val3", "col3":"val3"}}`

    And I have a struct associated to each type:

    type F1 struct {
    col1 string
    col2 string
    }

    type F2 struct {
    col3 string
    col4 string
    }

    Assuming that I use the `encoding/json` library to turn the raw json data
    into the struct:
    type Point {
    pointtype string
    data json.RawMessage
    }

    How can I decode the data into the appropiate struct just by knowing the
    pointtype?

    I was trying something along the lines of :

    func getType(pointType string) interface{} {
    switch pointType {
    case "f1":
    var p F1
    return &p
    case "f2":
    var p F2
    return &p
    }
    return nil
    }

    Which is not working because the returned value is an interface, not the
    proper struct type.
    How can I make this kind of switch struct selection work?

    [here is a non working example](http://play.golang.org/p/mjNeWlAw6j)
    --
    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/d/optout.
  • Mateusz Czapliński at Oct 23, 2014 at 9:55 am

    On Thursday, October 23, 2014 8:49:48 AM UTC+2, peterGo wrote:
    http://play.golang.org/p/I6xeWTlPst
    I believe it should be also doable by introducing an intermediary type for
    Data, which would implement UnmarshalJSON([]byte) (see
    http://golang.org/pkg/encoding/json/#Unmarshaler) and doing the type switch
    inside the method. Sorry for not providing a code sample.

    /M.

    --
    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/d/optout.
  • Manuel Garrido Pena at Oct 23, 2014 at 12:54 pm
    Peter, thanks! I see that this works. However, in this case, you still need
    to assert the type after the switch. Is there any way the last line of your
    example

         log.Println(parsedStruct.(*F1).Col1)

    could be written on a generic, type agnostic line? Maybe usng
      reflect.TypeOf?



    El jueves, 23 de octubre de 2014 02:49:48 UTC-4, peterGo escribió:

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

    Peter

    On Wednesday, October 22, 2014 11:07:24 PM UTC-4, manuel.ga...@gmail.com
    wrote:
    Let's say I have an application that receives json data in two different
    formats.

    f1 = `{"pointtype":"type1", "data":{"col1":"val1", "col2":"val2"}}`
    f2 = `{"pointtype":"type2", "data":{"col3":"val3", "col3":"val3"}}`

    And I have a struct associated to each type:

    type F1 struct {
    col1 string
    col2 string
    }

    type F2 struct {
    col3 string
    col4 string
    }

    Assuming that I use the `encoding/json` library to turn the raw json data
    into the struct:
    type Point {
    pointtype string
    data json.RawMessage
    }

    How can I decode the data into the appropiate struct just by knowing the
    pointtype?

    I was trying something along the lines of :

    func getType(pointType string) interface{} {
    switch pointType {
    case "f1":
    var p F1
    return &p
    case "f2":
    var p F2
    return &p
    }
    return nil
    }

    Which is not working because the returned value is an interface, not the
    proper struct type.
    How can I make this kind of switch struct selection work?

    [here is a non working example](http://play.golang.org/p/mjNeWlAw6j)
    --
    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/d/optout.
  • Tomwilde at Oct 23, 2014 at 1:26 pm

    On Thursday, October 23, 2014 2:54:03 PM UTC+2, manuel.ga...@gmail.com wrote:

    could be written on a generic, type agnostic line? Maybe usng
    reflect.TypeOf?
    Go is statically typed and as such you cannot have a single variable whose
    value will be of any of two different types depending on the situation. You
    could use an interface but then you'd be misusing it.

    Consider this concrete, assertion-less, reflection-less
    implementation: http://play.golang.org/p/Z3hRpX6BWs

    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 23, '14 at 3:38a
activeOct 23, '14 at 1:26p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase