# Serialize a Instance of mutable type into Json using JSON3?

**URL:** <https://discourse.julialang.org/t/serialize-a-instance-of-mutable-type-into-json-using-json3/54300>\
**Category:** General Usage\
**Tags:** package, json\
**Created:** [January 30, 2021, 11:18pm UTC](https://discourse.julialang.org/t/serialize-a-instance-of-mutable-type-into-json-using-json3/54300 "2021-01-30T23:18:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [January 30, 2021, 11:18pm UTC](https://discourse.julialang.org/t/serialize-a-instance-of-mutable-type-into-json-using-json3/54300/1 "2021-01-30T23:18:16Z")

</div>

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

```julia
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:

```julia
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:

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

```

[The documentation](https://quinnj.github.io/JSON3.jl/stable/#JSON3.RawType) says that I need to create my own implementations such as:

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

```

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

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [January 30, 2021, 11:46pm UTC](https://discourse.julialang.org/t/serialize-a-instance-of-mutable-type-into-json-using-json3/54300/2 "2021-01-30T23:46:25Z")

</div>

JSON3.write(test\_instance) works for me

```julia
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\"}"

```

---

<div class="post-metadata">

**Author:** ![DataPsycho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datapsycho/32/16891_2.png) [@DataPsycho](https://discourse.julialang.org/u/DataPsycho)\
**Post date:** [January 31, 2021, 5:02am UTC](https://discourse.julialang.org/t/serialize-a-instance-of-mutable-type-into-json-using-json3/54300/3 "2021-01-31T05:02:34Z")

</div>

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
