Implement json.Marshaller interface #46

Closed
opened 2017-02-16 03:50:09 +00:00 by saheljalal · 8 comments
saheljalal commented 2017-02-16 03:50:09 +00:00 (Migrated from github.com)

Not sure if this is possible, but it would be nice if there were a way to implement a default json marshaling implementation so that the collection data can be exported as json.

Not sure if this is possible, but it would be nice if there were a way to implement a default json marshaling implementation so that the collection data can be exported as json.
emirpasic commented 2017-02-16 04:35:39 +00:00 (Migrated from github.com)

@saheljalal

I think I had somewhere the idea of making serialization and deserialization (marshalling/unmarshalling) of these structures. My idea was to have that done in a binary form at first, but JSON could also be another option due to readability. Let me meditate on this and find some time, last few months have been crazy for me and I hardly found any free time.

What data structures are you using and would you like this feature for? What is the use case for JSON marshalling?

@saheljalal I think I had somewhere the idea of making serialization and deserialization (marshalling/unmarshalling) of these structures. My idea was to have that done in a binary form at first, but JSON could also be another option due to readability. Let me meditate on this and find some time, last few months have been crazy for me and I hardly found any free time. What data structures are you using and would you like this feature for? What is the use case for JSON marshalling?
saheljalal commented 2017-02-16 04:39:47 +00:00 (Migrated from github.com)

I just discovered this library recently and am using the hashset to store identifiers to start. That list is just a []string but working with the set API is so much easier and faster at call sites and from a performance perspective I'm sure. On the outgoing interface as json, however, I am just marshaling my own struct to handle this for now but was wondering if it was something that could be done in a generic fashion in the data structure itself.

I just discovered this library recently and am using the hashset to store identifiers to start. That list is just a `[]string` but working with the set API is so much easier and faster at call sites and from a performance perspective I'm sure. On the outgoing interface as json, however, I am just marshaling my own struct to handle this for now but was wondering if it was something that could be done in a generic fashion in the data structure itself.
emirpasic commented 2017-02-16 04:56:29 +00:00 (Migrated from github.com)

@saheljalal

In most cases, the marshalling would have to be handled individually for most structures. They would share a common interface, but the logic would be custom for each. I think that would be a cool feature and will try to include it in this lib, once I clean up the other pull requests.

From the performance perspective, there is a penalty in this lib, because it uses interface{} types everywhere, which gets hit hard type assertion implementation in Go. I am not criticizing this lib, but one should be aware of that fact when using this lib and it's mainly due to the lack of generics (won't go there, I'll let others be opinionated on this) and, thus, having to type assert everywhere. My suggestion is to use native Go's maps with concrete types if the problem you are working on is simple. The HashSet is really a simple implementation that uses Go's native map, where key is the value as to be unique in the set and the value is empty struct{}{}.

@saheljalal In most cases, the marshalling would have to be handled individually for most structures. They would share a common interface, but the logic would be custom for each. I think that would be a cool feature and will try to include it in this lib, once I clean up the other pull requests. From the performance perspective, there is a penalty in this lib, because it uses `interface{}` types everywhere, which gets [hit hard type assertion implementation in Go](http://stackoverflow.com/a/28027945). I am not criticizing this lib, but one should be aware of that fact when using this lib and it's mainly due to the lack of generics (won't go there, I'll let others be opinionated on this) and, thus, having to type assert everywhere. My suggestion is to use native Go's maps with concrete types if the problem you are working on is simple. The HashSet is really a simple implementation that uses Go's native map, where key is the value as to be unique in the set and the value is empty `struct{}{}`.
saheljalal commented 2017-02-16 06:53:02 +00:00 (Migrated from github.com)

@emirpasic will keep that in mind going forward and hope to see this materialize in the future. I'd like to make more use of this library given its simple and intuitive interfaces compared to go's limited type offerings. If performance becomes an issue, will evaluate my options at that time. Thanks.

@emirpasic will keep that in mind going forward and hope to see this materialize in the future. I'd like to make more use of this library given its simple and intuitive interfaces compared to go's limited type offerings. If performance becomes an issue, will evaluate my options at that time. Thanks.
lattice0 commented 2017-02-17 04:02:04 +00:00 (Migrated from github.com)

I was about to ask for something like unmarshaling, it'd be very very useful

I was about to ask for something like unmarshaling, it'd be very very useful
emirpasic commented 2017-03-05 21:43:47 +00:00 (Migrated from github.com)

@lucaszanella sure, marshaling (serialization) and unmarshaling (deserialization) will be implemented together

@lucaszanella sure, marshaling (serialization) and unmarshaling (deserialization) will be implemented together
emirpasic commented 2017-03-06 04:30:38 +00:00 (Migrated from github.com)

@saheljalal @lucaszanella JSON marshalling and unmarshalling has been implemented in #53 for all data structures.

Please make note of the ToJSON and FromJSON functions from the examples and let me know if this works out for you.

@saheljalal @lucaszanella JSON [marshalling ](https://github.com/emirpasic/gods#jsonserializer) and [unmarshalling](https://github.com/emirpasic/gods#jsondeserializer) has been implemented in #53 for all data structures. Please make note of the ```ToJSON``` and ```FromJSON``` functions from the [examples ](https://github.com/emirpasic/gods/blob/master/examples/serialization.go) and let me know if this works out for you.
emirpasic commented 2017-03-07 15:07:49 +00:00 (Migrated from github.com)

closed by #53

closed by #53
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
golibs/gms-gods#46
No description provided.