Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
},
{
"type": "null"
},
{
"type": "number"
}
]
}
Expand Down
40 changes: 10 additions & 30 deletions src/Serialization/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,38 @@
@register_serialization_type fpField "FiniteField"

function save_object(s::SerializerState, F::fpField)
save_object(s, string(characteristic(F)))
save_object(s, characteristic(F))
end

function load_object(s::DeserializerState, ::Type{fpField})
load_node(s) do str
return fpField(parse(UInt64, str))
end
fpField(load_object(s, UInt64))
end

# elements
@register_serialization_type fpFieldElem

function save_object(s::SerializerState, elem::fpFieldElem)
save_data_basic(s, string(elem))
save_data_basic(s, lift(ZZ, elem))
end

function load_object(s::DeserializerState, ::Type{fpFieldElem}, F::fpField)
load_node(s) do str
return F(parse(UInt64, str))
end
end
load_object(s::DeserializerState, ::Type{fpFieldElem}, F::fpField) = F(load_object(s, UInt64))

################################################################################
# ZZRingElem variant
@register_serialization_type FpField "FiniteField"

function save_object(s::SerializerState, F::FpField)
save_object(s, string(characteristic(F)))
save_object(s, characteristic(F))
end

function load_object(s::DeserializerState, ::Type{FpField})
load_node(s) do str
FpField(parse(ZZRingElem, str))
end
end
load_object(s::DeserializerState, ::Type{FpField}) = FpField(load_object(s, ZZRingElem))

# elements
@register_serialization_type FpFieldElem

function save_object(s::SerializerState, elem::FpFieldElem)
save_data_basic(s, string(elem))
end
save_object(s::SerializerState, elem::FpFieldElem) = save_data_basic(s, lift(ZZ, elem))

function load_object(s::DeserializerState, ::Type{FpFieldElem}, F::FpField)
load_node(s) do str
F(parse(ZZRingElem, str))
end
end
load_object(s::DeserializerState, ::Type{FpFieldElem}, F::FpField) = F(load_object(s, ZZRingElem))

################################################################################
# SimpleNumField
Expand Down Expand Up @@ -548,12 +532,8 @@ function save_object(s::SerializerState, P::PadicField)
end

function load_object(s::DeserializerState, ::Type{PadicField})
prime_num = load_node(s, :prime) do node
return parse(ZZRingElem, node)
end
precision = load_node(s, :precision) do node
return parse(Int64, node)
end
prime_num = load_object(s, ZZRingElem, :prime)
precision = load_object(s, Int64, :precision)
return PadicField(prime_num, precision)
end

Expand Down
4 changes: 2 additions & 2 deletions src/Serialization/Rings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function save_object(s::SerializerState, p::Union{UniversalPolyRingElem, MPolyRi
save_data_array(s) do
for i in 1:length(p)
save_data_array(s) do
save_object(s, map(string, exponent_vector(p, i)))
save_object(s, exponent_vector(p, i))
save_object(s, coeff(p, i))
end
end
Expand All @@ -161,7 +161,7 @@ function save_object(s::SerializerState, p::AbstractAlgebra.Generic.LaurentMPoly
for c in coefficients(p)
exponent_vector, index = iterate(exponent_vectors_gen, index)
save_data_array(s) do
save_object(s, map(string, exponent_vector))
save_object(s, exponent_vector)
save_object(s, c)
end
end
Expand Down
20 changes: 16 additions & 4 deletions src/Serialization/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ end
# ZZRingElem
@register_serialization_type ZZRingElem

function save_object(s::SerializerState, x::ZZRingElem)
if (1 - 2^53 <= x <= 2^53 -1 )
save_data_basic(s, Int(x))
else
save_data_basic(s, x)
Comment thread
antonydellavecchia marked this conversation as resolved.
end
end

load_object(s::DeserializerState, T::Type{ZZRingElem}, ::ZZRing) = load_object(s, T)

function load_object(s::DeserializerState, ::Type{ZZRingElem})
load_node(s) do str
return ZZRingElem(str)
load_node(s) do val
return ZZRingElem(val)
end
end

Expand Down Expand Up @@ -77,8 +85,12 @@ end
@register_serialization_type Float64

function load_object(s::DeserializerState, ::Type{T}) where {T<:Number}
load_node(s) do str
parse(T, str)
load_node(s) do x
if x isa Integer
return T(x)
else
parse(T, x)
end
end
end

Expand Down
15 changes: 14 additions & 1 deletion src/Serialization/serializers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,23 @@ function save_data_array(f::Function, s::SerializerState,
_save_data_container(f, s, key, "[", "]")
end

function save_data_basic(s::SerializerState{IPCSerializer}, x::T,
key::Union{Symbol, Nothing} = nothing) where T <: Union{Bool, Int64, Int32, Int16, Int8, UInt32, UInt16, UInt8}
begin_node(s, key)
JSON.json(s.io, x)
end

function save_data_basic(s::SerializerState, x::Any,
key::Union{Symbol, Nothing} = nothing)
begin_node(s, key)
data = x isa Bool ? x : string(x)
if x isa Bool
data = x
elseif x isa Integer && (1 - 2^53 <= x <= 2^53 -1 )
data = x
else
data = string(x)
end

if s.pretty_print
print(s.io, "")
JSON.json(s.io, data)
Expand Down
7 changes: 7 additions & 0 deletions test/Serialization/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
end
end

@testset "Large Integer" begin
original = ZZRingElem(2^62)
test_save_load_roundtrip(path, original) do loaded
@test loaded == original
end
end

@testset "String" begin
original = "original \n \" "
test_save_load_roundtrip(path, original) do loaded
Expand Down
2 changes: 1 addition & 1 deletion test/Serialization/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ end
save(filename, [[1, 2], [3, 4], [5, 6]]; pretty_print=true)
str = read(filename, String)
version_info = Oscar.Serialization.get_oscar_serialization_version()[:Oscar][2]
cmp_str = "{\n \"_ns\":{\n \"Oscar\":[\n \"https://github.com/oscar-system/Oscar.jl\",\n \"" * version_info * "\"\n ]\n },\n \"_type\":{\n \"name\":\"Vector\",\n \"params\":{\n \"name\":\"Vector\",\n \"params\":\"Base.Int\"\n }\n },\n \"data\":[\n [\n \"1\",\n \"2\"\n ],\n [\n \"3\",\n \"4\"\n ],\n [\n \"5\",\n \"6\"\n ]\n ]\n}"
cmp_str = "{\n \"_ns\":{\n \"Oscar\":[\n \"https://github.com/oscar-system/Oscar.jl\",\n \"" * version_info * "\"\n ]\n },\n \"_type\":{\n \"name\":\"Vector\",\n \"params\":{\n \"name\":\"Vector\",\n \"params\":\"Base.Int\"\n }\n },\n \"data\":[\n [\n 1,\n 2\n ],\n [\n 3,\n 4\n ],\n [\n 5,\n 6\n ]\n ]\n}"

@test str == cmp_str
end
Expand Down
Loading