Fix JSON Marshaling for Millis and Nanos Types #368
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR addresses two bugs #362 & #360 related to the JSON marshaling of
Millis
andNanos
types. Both users experienced issues where these types were not correctly marshaled into JSON, resulting in empty field in the JSON output.Here's the incorrectly marshaled JSON response with the missing
t
value.The core issue was rooted in the use of pointer receivers (
*Millis
and*Nanos
) for the MarshalJSON methods in these types. In our response structs, forAgg
,Trades
,Quotes
,Snapshot
, etc,Millis
andNanos
were utilized as non-pointer fields (e.g.Timestamp Millis
in the response struct). Consequently, whenjson.Marshal
was called, the custom MarshalJSON methods were not being triggered because of the pointer receiver vs. value receiver miss-match, leading to the observed marshaling problems. I was able to debug this by addingfmt.Println
statements toMarshalJSON
for bothMillis
andNanos
and discovered these were never being triggered.To resolve this, the MarshalJSON methods for both
Millis
andNanos
have been modified to use value receivers instead of pointer receivers. This change ensures that these methods are appropriately invoked during JSON marshaling, even whenMillis
andNanos
are used as non-pointer fields. This update aligns the method definitions with the actual usage of these types in our response structs and ensures correct JSON serialization behavior.Here's the correctly marshaled JSON response with
t
having the correct value after re-running the script above.This update specifically targets the JSON marshaling process and should not affect other areas of the application where
Millis
andNanos
are used. I ran though all the examples for stocks, options, indices, forex, and crypto without issue.