Skip to content

Add extension to make TimeArray JSON3 serializable #535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ValentinKaisermayer opened this issue Apr 9, 2025 · 0 comments
Open

Add extension to make TimeArray JSON3 serializable #535

ValentinKaisermayer opened this issue Apr 9, 2025 · 0 comments

Comments

@ValentinKaisermayer
Copy link
Contributor

The main problem is that a TimeArray is a parametric type, which is difficult to implement, see.

The meta::Any field is not serializable as a JSON in any case.
JSON.json even results in a stack overflow error.

This does work, but still makes the assumption that the timestamps are DateTime.
They can be anything: Date, DateTime, TimeZones.ZonedDateTime, TimesDates.TimeDate, TimesDates.TimeDateZone

StructTypes.StructType(::Type{<:TimeSeries.TimeArray}) = StructTypes.CustomStruct()
function StructTypes.lower(x::TimeSeries.TimeArray)
    return Dict(
        "timestamp" => TimeSeries.timestamp(x),
        "values" => [TimeSeries.values(x[c]) for c in TimeSeries.colnames(x)],
        "colnames" => String.(TimeSeries.colnames(x)),
    )
end
function StructTypes.construct(::Type{TimeSeries.TimeArray}, x)
    return TimeSeries.TimeArray(
        Dates.DateTime.(x["timestamp"]),
        reduce(hcat, x["values"]), 
        Symbol.(x["colnames"]),
    )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant