Drathier
Drathier

Reputation: 14539

Stringer equivalent for json marshal

I can implement Stringer to specify how fmt.Print() will write my custom type as a string. Is there an equivalent for json marshal?

I want to store unique values in go and represent it as a slice in json. Using a slice in go isn't optimal for this. This O(n) insert seems unnecessary - a binary tree or map would be faster, specially since I'm keeping the list sorted at all times.

Upvotes: 3

Views: 1451

Answers (1)

Mr_Pink
Mr_Pink

Reputation: 109406

Yes, json.Marshaler and json.Unmarshaler.

Implementing MarshalJSON and UnmarshalJSON will give you what you want.

Upvotes: 7

Related Questions