|
1 | 1 | using Accessors
|
2 | 2 | using Accessors: ComposedOptic, PropertyLens, IndexLens, DynamicIndexLens
|
3 |
| -using StructTypes: StructTypes |
4 |
| -using JSON3: JSON3 |
| 3 | +using JSON: JSON |
5 | 4 |
|
6 | 5 | const ALLOWED_OPTICS = Union{typeof(identity),PropertyLens,IndexLens,ComposedOptic}
|
7 | 6 |
|
@@ -867,79 +866,58 @@ end
|
867 | 866 | # Alternate implementation with StructTypes
|
868 | 867 | # -----------------------------------------
|
869 | 868 |
|
870 |
| -index_to_dict(i::Integer) = Dict(:type => "integer", :value => i) |
871 |
| -index_to_dict(v::AbstractVector{Int}) = Dict(:type => "vector", :values => v) |
872 |
| -index_to_dict(r::UnitRange) = Dict(:type => "unitrange", :start => r.start, :stop => r.stop) |
873 |
| -index_to_dict(r::StepRange) = Dict(:type => "steprange", :start => r.start, :stop => r.stop, :step => r.step) |
874 |
| -index_to_dict(::Colon) = Dict(:type => "colon") |
875 |
| -index_to_dict(s::ConcretizedSlice{T,Base.OneTo{I}}) where {T,I} = Dict(:type => "concretized_slice", :oneto => s.range.stop) |
| 869 | +index_to_dict(i::Integer) = Dict("type" => "integer", "value" => i) |
| 870 | +index_to_dict(v::AbstractVector{Int}) = Dict("type" => "vector", "values" => v) |
| 871 | +index_to_dict(r::UnitRange) = Dict("type" => "unitrange", "start" => r.start, "stop" => r.stop) |
| 872 | +index_to_dict(r::StepRange) = Dict("type" => "steprange", "start" => r.start, "stop" => r.stop, "step" => r.step) |
| 873 | +index_to_dict(::Colon) = Dict("type" => "colon") |
| 874 | +index_to_dict(s::ConcretizedSlice{T,Base.OneTo{I}}) where {T,I} = Dict("type" => "concretized_slice", "oneto" => s.range.stop) |
876 | 875 | index_to_dict(::ConcretizedSlice{T,R}) where {T,R} = error("ConcretizedSlice with range type $(R) not supported")
|
877 |
| -index_to_dict(t::Tuple) = Dict(:type => "tuple", :values => [index_to_dict(x) for x in t]) |
| 876 | +index_to_dict(t::Tuple) = Dict("type" => "tuple", "values" => map(index_to_dict, t)) |
878 | 877 |
|
879 | 878 | function dict_to_index(dict)
|
880 |
| - # conversion needed because of the same reason as in dict_to_optic |
881 |
| - dict = Dict(Symbol(k) => v for (k, v) in dict) |
882 |
| - if dict[:type] == "integer" |
883 |
| - return dict[:value] |
884 |
| - elseif dict[:type] == "vector" |
885 |
| - return collect(Int, dict[:values]) |
886 |
| - elseif dict[:type] == "unitrange" |
887 |
| - return dict[:start]:dict[:stop] |
888 |
| - elseif dict[:type] == "steprange" |
889 |
| - return dict[:start]:dict[:step]:dict[:stop] |
890 |
| - elseif dict[:type] == "colon" |
| 879 | + if dict["type"] == "integer" |
| 880 | + return dict["value"] |
| 881 | + elseif dict["type"] == "vector" |
| 882 | + return collect(Int, dict["values"]) |
| 883 | + elseif dict["type"] == "unitrange" |
| 884 | + return dict["start"]:dict["stop"] |
| 885 | + elseif dict["type"] == "steprange" |
| 886 | + return dict["start"]:dict["step"]:dict["stop"] |
| 887 | + elseif dict["type"] == "colon" |
891 | 888 | return Colon()
|
892 |
| - elseif dict[:type] == "concretized_slice" |
893 |
| - return ConcretizedSlice(Base.Slice(Base.OneTo(dict[:oneto]))) |
894 |
| - elseif dict[:type] == "tuple" |
895 |
| - return tuple(map(dict_to_index, dict[:values])...) |
| 889 | + elseif dict["type"] == "concretized_slice" |
| 890 | + return ConcretizedSlice(Base.Slice(Base.OneTo(dict["oneto"]))) |
| 891 | + elseif dict["type"] == "tuple" |
| 892 | + return tuple(map(dict_to_index, dict["values"])...) |
896 | 893 | else
|
897 |
| - error("Unknown index type: $(dict[:type])") |
| 894 | + error("Unknown index type: $(dict["type"])") |
898 | 895 | end
|
899 | 896 | end
|
900 | 897 |
|
901 |
| -optic_to_dict(::typeof(identity)) = Dict(:type => "identity") |
902 |
| -optic_to_dict(::PropertyLens{sym}) where {sym} = Dict(:type => "property", :field => String(sym)) |
903 |
| -optic_to_dict(i::IndexLens) = Dict(:type => "index", :indices => index_to_dict(i.indices)) |
904 |
| -optic_to_dict(c::ComposedOptic) = Dict(:type => "composed", :outer => optic_to_dict(c.outer), :inner => optic_to_dict(c.inner)) |
| 898 | +optic_to_dict(::typeof(identity)) = Dict("type" => "identity") |
| 899 | +optic_to_dict(::PropertyLens{sym}) where {sym} = Dict("type" => "property", "field" => String(sym)) |
| 900 | +optic_to_dict(i::IndexLens) = Dict("type" => "index", "indices" => index_to_dict(i.indices)) |
| 901 | +optic_to_dict(c::ComposedOptic) = Dict("type" => "composed", "outer" => optic_to_dict(c.outer), "inner" => optic_to_dict(c.inner)) |
905 | 902 |
|
906 | 903 | function dict_to_optic(dict)
|
907 |
| - # Nested dicts are deserialised to Dict{String, Any} |
908 |
| - # but the top level dict is deserialised to Dict{Symbol, Any} |
909 |
| - # so for this recursive function to work we need to first |
910 |
| - # convert String keys to Symbols |
911 |
| - dict = Dict(Symbol(k) => v for (k, v) in dict) |
912 |
| - if dict[:type] == "identity" |
| 904 | + if dict["type"] == "identity" |
913 | 905 | return identity
|
914 |
| - elseif dict[:type] == "index" |
915 |
| - return IndexLens(dict_to_index(dict[:indices])) |
916 |
| - elseif dict[:type] == "property" |
917 |
| - return PropertyLens{Symbol(dict[:field])}() |
918 |
| - elseif dict[:type] == "composed" |
919 |
| - return dict_to_optic(dict[:outer]) ∘ dict_to_optic(dict[:inner]) |
| 906 | + elseif dict["type"] == "index" |
| 907 | + return IndexLens(dict_to_index(dict["indices"])) |
| 908 | + elseif dict["type"] == "property" |
| 909 | + return PropertyLens{Symbol(dict["field"])}() |
| 910 | + elseif dict["type"] == "composed" |
| 911 | + return dict_to_optic(dict["outer"]) ∘ dict_to_optic(dict["inner"]) |
920 | 912 | else
|
921 |
| - error("Unknown optic type: $(dict[:type])") |
| 913 | + error("Unknown optic type: $(dict["type"])") |
922 | 914 | end
|
923 | 915 | end
|
924 | 916 |
|
925 |
| -struct VarNameWithDictOptic |
926 |
| - sym::Symbol |
927 |
| - optic::Dict{Symbol, Any} |
928 |
| -end |
929 |
| - |
930 |
| -function VarNameWithDictOptic(dict::Dict{Symbol, Any}) |
931 |
| - return VarNameWithDictOptic{dict[:sym]}(dict[:optic]) |
932 |
| -end |
| 917 | +vn_to_dict(vn::VarName) = Dict("sym" => getsym(vn), "optic" => optic_to_dict(getoptic(vn))) |
933 | 918 |
|
934 |
| -# Serialisation |
935 |
| -StructTypes.StructType(::Type{VarNameWithDictOptic}) = StructTypes.UnorderedStruct() |
| 919 | +dict_to_vn(dict::Dict{<:AbstractString, Any}) = VarName{Symbol(dict["sym"])}(dict_to_optic(dict["optic"])) |
936 | 920 |
|
937 |
| -vn_to_string2(vn::VarName) = JSON3.write(VarNameWithDictOptic(getsym(vn), optic_to_dict(getoptic(vn)))) |
| 921 | +vn_to_string2(vn::VarName) = JSON.json(vn_to_dict(vn)) |
938 | 922 |
|
939 |
| -# Deserialisation |
940 |
| -Base.pairs(vn::VarNameWithDictOptic) = Dict(:sym => vn.sym, :optic => vn.optic) |
941 |
| - |
942 |
| -function vn_from_string2(str) |
943 |
| - vn_nt = JSON3.read(str, VarNameWithDictOptic) |
944 |
| - return VarName{vn_nt.sym}(dict_to_optic(vn_nt.optic)) |
945 |
| -end |
| 923 | +vn_from_string2(str) = dict_to_vn(JSON.parse(str)) |
0 commit comments