Skip to content

models.Date doesn't marshal correctly #512

@nathanhack

Description

@nathanhack

Describe the bug
models.Date doesn't marshal correctly

Expected behavior
I would expect models.Date to marshal and unmarshal correctly.

Example:
https://go.dev/play/p/6t85zeW7AHr

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"github.com/polygon-io/client-go/rest/models"
)

func main() {

	s := `{
            "cfi": "OCASPS",
            "contract_type": "call",
            "exercise_style": "american",
            "expiration_date": "2025-02-24",
            "primary_exchange": "BATO",
            "shares_per_contract": 100,
            "strike_price": 400,
            "ticker": "O:SPY250224C00400000",
            "underlying_ticker": "SPY"
        }`

	var m1 models.OptionsContract

	err := json.Unmarshal([]byte(s), &m1)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
	fmt.Println("expiration_date:", m1.ExpirationDate)
	fmt.Println("expiration_date:", time.Time(m1.ExpirationDate))

	bs, err := json.Marshal(m1)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}

	fmt.Printf("bs: %s\n", bs)

	var m2 models.OptionsContract
	err = json.Unmarshal(bs, &m2)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}

	fmt.Println("expiration_date:", m2.ExpirationDate)
	fmt.Println("expiration_date:", time.Time(m2.ExpirationDate))

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions