Serialize a Instance of mutable type into Json using JSON3?

Hi,
Lets say i have defined a struct and StructType method for unmarshaling from json string as follows:

mutable struct Request
    from::String
    message::String
    requesttime::String
end

# Outer Constructor function for creating a request object
Request(from, message) = Request(from, message, string(now()))
# Empty Constructor function for parsing json string
Request() = Request("", "", string(now()))
StructTypes.StructType(:: Type{Request}) = StructTypes.Mutable()

Now I have a json string and I can unmarshall that in to instance of type Response as follows:

test_json = """{"from": "Saru", "message": "Saru to Discovery, Over!"}"""
JSON3.read(test_json, Request)

To do opposite lets say I have a instance of type Request:

test_instance = Request("Saru", "Saru to Discovery, Over!")

The documentation says that I need to create my own implementations such as:

StructTypes.StructType(::Type{Request}) = JSON3.RawType()
JSON3.rawbytes(x::Request)  = ...

Can some one help how should I define the marshaling part.

JSON3.write(test_instance) works for me

julia> test_instance = Request("Saru", "Saru to Discovery, Over!")
Request("Saru", "Saru to Discovery, Over!", "2021-01-30T16:44:55.273")

julia> JSON3.write(test_instance)
"{\"from\":\"Saru\",\"message\":\"Saru to Discovery, Over!\",\"requesttime\":\"2021-01-30T16:44:55.273\"}"
2 Likes

Oh, did not know that it would be that easy. Not sure why the document shows other way. May be I am missing something. But that works. Thanks