-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjld2_to_json.jl
More file actions
executable file
·71 lines (57 loc) · 1.88 KB
/
jld2_to_json.jl
File metadata and controls
executable file
·71 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env julia
using ArgParse
using JLD2
using JSON
using DataFrames # Required to properly handle DataFrame
"""
Recursively convert JLD2 data to something JSON-serializable.
Unsupported types are replaced with string descriptions.
"""
function safe_serialize(obj)
if isa(obj, Number) || isa(obj, String) || isa(obj, Bool) || obj === nothing
return obj
elseif isa(obj, AbstractArray)
return [safe_serialize(x) for x in obj]
elseif isa(obj, Dict)
return Dict(string(k) => safe_serialize(v) for (k, v) in obj)
elseif isa(obj, DataFrame)
# Serialize the DataFrame to a Dict of column names and values (for JSON compatibility)
return Dict("columns" => names(obj), "data" => Matrix(obj))
else
return JSON.lower(obj) # Try JSON-compatible lowering for other types
end
end
function jld2_to_json(jld2_path::String, json_path::String)
println("Reading from $jld2_path ...")
jld_data = jldopen(jld2_path, "r")
result = Dict{String, Any}()
for k in keys(jld_data)
println("Processing key: $k")
# Ensure that we're processing the reconstructed DataFrame properly
value = jld_data[k]
# If it's a DataFrame, we need to serialize it differently
result[string(k)] = safe_serialize(value)
end
close(jld_data)
println("Writing to $json_path ...")
open(json_path, "w") do io
JSON.print(io, result)
end
println("Conversion complete.")
end
function main()
s = ArgParseSettings()
@add_arg_table s begin
"input"
help = "Input JLD2 file path"
required = true
"--output", "-o"
help = "Output JSON file path"
required = true
end
parsed_args = parse_args(s)
input_file = parsed_args["input"]
output_file = parsed_args["output"]
jld2_to_json(input_file, output_file)
end
main()