# \[JSON.jl\] About empty structs

**URL:** <https://discourse.julialang.org/t/json-jl-about-empty-structs/53571>\
**Category:** General Usage\
**Created:** [January 19, 2021, 12:17am UTC](https://discourse.julialang.org/t/json-jl-about-empty-structs/53571 "2021-01-19T00:17:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TommyXR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tommyxr/32/21112_2.png) [@TommyXR](https://discourse.julialang.org/u/TommyXR)\
**Post date:** [January 19, 2021, 12:17am UTC](https://discourse.julialang.org/t/json-jl-about-empty-structs/53571/1 "2021-01-19T00:17:59Z")

</div>

Hello.

The package `JSON.jl` made the interesting design decision to make singleton structs unserializable.  
This behavior is implemented in this function in the [source code](https://github.com/JuliaIO/JSON.jl/blob/master/src/Writer.jl#L43).

```julia
function lower(a)
    if nfields(a) > 0
        CompositeTypeWrapper(a)
    else
        error("Cannot serialize type $(typeof(a))")
    end
end

```

However, I find this behavior interesting and I was wondering why this was the case. Empty Dicts and named tuples,are handled and return a simple `{}` JSON object. Empty tuples and arrays return `[]`. Shouldn’t singletons return `{}` by default?

Thank you!

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 19, 2021, 6:50am UTC](https://discourse.julialang.org/t/json-jl-about-empty-structs/53571/2 "2021-01-19T06:50:53Z")

</div>

> [@TommyXR](#):
>
> The package `JSON.jl` made the interesting design decision to make singleton structs unserializable.

If you want to serialize a struct, you usually have to preserve the type information since

```julia
struct A
     x
end

struct B
    x
end

```

are different types and must be treated differently even if they have the same structure. In that case you have to provide `JSON.lower(s::A)`, `JSON.lower(s::B)` … methods to make them distinguishable, even more so for an empty `struct C end` where you have only type information.

I’m no JSON expert, but for me the cited design decision makes sense. It is described also in the [README](https://github.com/JuliaIO/JSON.jl/blob/master/README.md).
