FAQ
A lot of times I end up casting Maps/Interfaces to static types after
decoding them from JSON/BSON. I initially decode them to interface as I
cannot know before hand what the type could be. So is there a library
which can decode/cast objects from the interface to the defined type.

I am looking for something like this => http://play.golang.org/p/QWEnds0cdR

Does anything like this exist?

-Abhishek

--
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

  • Egon at Feb 22, 2014 at 9:52 am
    Pleas show your problem, what you do now, and not the proposed solution. Obviously construct a better, more realistic example... or better yet, show the actual code where you are having problems. That way people can suggest more meaningful comments.

    +egon

    --
    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.
  • Abhishek Kona at Feb 22, 2014 at 9:55 am
    I don't think I have a real life example to present right now.

    https://github.com/mitchellh/mapstructure => This is the kind of library I
    am looking for, the downside is this library does not seem to have
    tag/variable matching via a tag name.


    On Saturday, February 22, 2014 1:52:03 AM UTC-8, egon wrote:

    Pleas show your problem, what you do now, and not the proposed solution.
    Obviously construct a better, more realistic example... or better yet, show
    the actual code where you are having problems. That way people can suggest
    more meaningful comments.

    +egon
    --
    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.
  • Egon at Feb 22, 2014 at 2:24 pm
    Well the problem is that I can't understand your problem then. your example is fairly limited and it seems that json package is already able to do that using field tags. If you have json type that can change the structure then it might be possible to use pointers in the struct to recieve a concrete version. If that doesn't help then json.RawMessage could be the answer. Or it might require some custom marshaler. Or it might be that the json protocol itself is poorly designed.

    So, it's very hard to answer a question, if it's not precise.

    Show all the possible input json-s with some imagined problem, whatever is clearest. You can use some other language if needed. Try to avoid x,y,z,aa,bb things in examples, they just make things confusing.... use the phone test, if you can't communicate the code via phone unambiguously then choose better names. You could imagine some game protocol that had such problem and try to use that as an example.

    + egon

    --
    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.
  • Nick Craig-Wood at Feb 22, 2014 at 10:02 am

    On 22/02/14 09:23, Abhishek Kona wrote:
    A lot of times I end up casting Maps/Interfaces to static types after
    decoding them from JSON/BSON. I initially decode them to interface as I
    cannot know before hand what the type could be. So is there a library
    which can decode/cast objects from the interface to the defined type.

    I am looking for something like this => http://play.golang.org/p/QWEnds0cdR
    You could always json Encode the map, then decode it back into the
    struct which would work but not be wildly efficient.

    Alternatively you could use json.RawMessage to delay the decoding until
    you've worked out what you've got (instead of decoding it into a map),
    then decode it into a struct (this assumes you can work out what you've
    got from the context).

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


    --
    Nick Craig-Wood <nick@craig-wood.com> -- http://www.craig-wood.com/nick

    --
    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
postedFeb 22, '14 at 9:23a
activeFeb 22, '14 at 2:24p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase