# Serializing Julia Composite Type with Inline JavaScript to JSON

**URL:** <https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178>\
**Category:** General Usage\
**Tags:** json\
**Created:** [April 12, 2017, 1:26am UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178 "2017-04-12T01:26:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [April 12, 2017, 1:26am UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/1 "2017-04-12T01:26:29Z")

</div>

This is a follow-up to this issue, because I still can’t quite see how to do this.

I’d love to be able to create a Julia composite type, and when I serialize to JSON, be able to emit valid JavaScript. The JavaScript library I’m wrapping uses inline function definitions, which aren’t in the JSON specification but are valid JavaScript.

```julia
using JSON

type JSFunction
    symbolSize
end

a = JSFunction("function (data) {return Math.sqrt(data[2]) / 5e2;}")

print(JSON.json(a))

returns: {"symbolSize":"function (data) {return Math.sqrt(data[2]) / 5e2;}"}
need: {"symbolSize": function (data) {return Math.sqrt(data[2]) / 5e2;}}

```

[https://github.com/JuliaIO/JSON.jl/issues/170](https://github.com/JuliaIO/JSON.jl/issues/170)

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 12, 2017, 7:10am UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/2 "2017-04-12T07:10:45Z")

</div>

The trick is to define a new kind of serialization context that inherits from the default.

```julia
import JSON
using JSON.StructuralContext
using JSON.Serializations.CommonSerialization

immutable JSSerialization <: CommonSerialization end
immutable JSFunction
    data::Compat.UTF8String
end

function JSON.show_json(io::StructuralContext,
                          ::JSSerialization, f::JSFunction)
    first = true
    for line in split(f.data, '\n')
        if !first
            JSON.indent(io)
        end
        first = false
        Base.print(io, line)
    end
end

json(x) = sprint(JSON.show_json, JSSerialization(), x)

```

You’re right that this isn’t super clear right now; we’ll need to document this better.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [April 12, 2017, 12:32pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/3 "2017-04-12T12:32:08Z")

</div>

Unfortunately, this doesn’t seem to provide the correct answer, I’m still seeing the double-quotes around the function.

```julia
a = ECharts.JSFunction("function (data) {return Math.sqrt(data[2]) / 5e2;}")
ECharts.JSFunction("function (data) {return Math.sqrt(data[2]) / 5e2;}")

JSON.json(a)
"{\"data\":\"function (data) {return Math.sqrt(data[2]) / 5e2;}\"}"

print(JSON.json(a))
{"data":"function (data) {return Math.sqrt(data[2]) / 5e2;}"}

```

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [April 12, 2017, 12:36pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/4 "2017-04-12T12:36:52Z")

</div>

I pushed a commit to show what I have implemented:

[https://github.com/randyzwitch/ECharts.jl/commit/529ae3536ac7e9613268287fb9f4971c4b507a43](https://github.com/randyzwitch/ECharts.jl/commit/529ae3536ac7e9613268287fb9f4971c4b507a43)

---

<div class="post-metadata">

**Author:** ![tshort](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tshort/32/43_2.png) [@tshort](https://discourse.julialang.org/u/tshort)\
**Post date:** [April 12, 2017, 3:32pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/5 "2017-04-12T15:32:11Z")

</div>

Try using the `json` method that @fengyang.wang defined, not the one in JSON.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [April 12, 2017, 4:32pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/6 "2017-04-12T16:32:10Z")

</div>

Thanks, that’s really subtle. I still have an error in my package somewhere, but at the very least, I can get my test example to work.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [April 12, 2017, 7:00pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/7 "2017-04-12T19:00:20Z")

</div>

Yes, that’s right. The idea behind it is that different packages will have different needs, and it’s important for custom serializations not to interfere with the standard serialization. The intention is that packages will define and use their own `json` function.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [April 13, 2017, 1:26pm UTC](https://discourse.julialang.org/t/serializing-julia-composite-type-with-inline-javascript-to-json/3178/8 "2017-04-13T13:26:32Z")

</div>

FWIW, here’s what I was trying to do and why it’s important, and most importantly, THANK YOU!!! This will open up all the goodies in this visualization library.

 ![](https://global.discourse-cdn.com/julialang/original/3X/6/9/69871fe325bb21023b53028a8db29e7fa4cd44d5.png)
