Skip to content

Commit

Permalink
Merge pull request #256 from PerformanceCoder/fixed_serialization
Browse files Browse the repository at this point in the history
Fixed serialization issue
  • Loading branch information
isuruf committed Jun 16, 2022
2 parents f1c9186 + 71252c5 commit 1bce8dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/types.jl
Expand Up @@ -286,20 +286,23 @@ coeff(b::Basic, x::Basic) = coeff(b, x, one(Basic))

function Serialization.serialize(s::Serialization.AbstractSerializer, m::Basic)
Serialization.serialize_type(s, typeof(m))

size = Ref{UInt64}(0)
serialized = ccall((:basic_dumps, libsymengine),
serialized = ccall((:basic_dumps, libsymengine),
Ptr{Int8}, (Ref{Basic}, Ptr{UInt64}), m, size)
julia_serialized_str = unsafe_string(serialized, size[])
write(s.io, julia_serialized_str)

write(s.io, size[])
unsafe_write(s.io, serialized, size[])
end

function Serialization.deserialize(s::Serialization.AbstractSerializer, ::Type{Basic})
ser_str = read(s.io, String)

size = read(s.io, UInt64)
serialized_data = read(s.io, size)

a = Basic()
res = ccall((:basic_loads, libsymengine),
Cuint, (Ref{Basic}, Ptr{Int8}, UInt64), a, ser_str, length(ser_str))
res = ccall((:basic_loads, libsymengine),
Cuint, (Ref{Basic}, Ptr{Int8}, UInt64), a, serialized_data, size)
throw_if_error(res)
return a
end

11 changes: 10 additions & 1 deletion test/runtests.jl
Expand Up @@ -290,4 +290,13 @@ end
@test is_serialization_correct(Basic(pi))
@test is_serialization_correct(Basic(x + y))
@test is_serialization_correct(Basic(sin(cos(pi*x + y)) + y^2))
end

# Complex type test
iobuf = IOBuffer()
data = [x+y, x+y]
serialize(iobuf, data)
seek(iobuf, 0)
deserialized = deserialize(iobuf)
close(iobuf)
@test deserialized == data
end

0 comments on commit 1bce8dd

Please sign in to comment.