# \[RFC\] JSObjectLiteral.jl

**URL:** <https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584>\
**Category:** Community\
**Created:** [February 8, 2019, 2:01pm UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584 "2019-02-08T14:01:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [February 8, 2019, 2:01pm UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584/1 "2019-02-08T14:01:32Z")

</div>

Hello,

For my own understanding of Julia macros and evaluation of expressions, I’ve made a [tiny package](https://github.com/davidavdav/JSObjectLiteral.jl) that can parse “javascript-like” object literals in Julia, like `{a: b, c: [d, { e, f, g: "gee!" }], h: "ouch!" }` by parsing and replacing the abstract syntax tree.

Any comments (like “why would you want to do this?” or “This is not what we usually call an object literal”) are welcome!

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [February 10, 2019, 4:27am UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584/2 "2019-02-10T04:27:16Z")

</div>

Cool job.  
However, I’d say sorry for I cannot stop myself from selling my package.  
 ![-3312329fe9891403](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a007af29c66cf066370e34b3fba7f058aee1f10.jpeg)

```julia
using MLStyle
json(node) =
@match node begin
      :({ $(kvs...) }) => 
             let f = @λ :($k : $v) -> Expr(:call, =>, string(k), json(v))
                 Expr(:call, Dict, (f(kv) for kv in kvs if !(kv isa LineNumberNode))...)
            end
       :[$(elts...)] => Expr(:vect, map(json, elts)...)
       a :: Symbol || a :: T where T <: Number || a :: T where T <: AbstractString => a
end

macro json(expr)
     json(expr) |> esc
end
```

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [February 10, 2019, 10:31am UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584/3 "2019-02-10T10:31:21Z")

</div>

Wow, that looks impressive. As far as I can tell you don’t have the shortcut `{ a } === { a: a }` but I suppose that is just one more line in your code.

I couldn’t stop myself from developing somewhat further, and I can now also do things like evaluating `a.b.c` and `a.b.c = 4` but this led quite a refactoring of everything.

---

<div class="post-metadata">

**Author:** ![thautwarm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thautwarm/32/37760_2.png) [@thautwarm](https://discourse.julialang.org/u/thautwarm)\
**Post date:** [February 10, 2019, 11:33am UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584/4 "2019-02-10T11:33:42Z")

</div>

Hey, your idea is cool. The reason why for my code is short is that I use better tools, but which is not the core of the making a package.  
If you want to make `JSObjectLiteral` a solid solution for JSON operations, it’s still far to go, which is not MLStyle.jl could help with much, e.g., you can make following plans:

- Chaining getter or setters, just as what you said, `a.b.c, a.b.c = 4`.
- support (de)serialization for JSONs.
- support queries/GraphQL on JSONs.

I sell my package, which is aimed at making developers or normal users able to focus on the core logics in their work instead of something boring and verbose.

---

<div class="post-metadata">

**Author:** ![davidavdav](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidavdav/32/1065_2.png) [@davidavdav](https://discourse.julialang.org/u/davidavdav)\
**Post date:** [February 14, 2019, 10:01am UTC](https://discourse.julialang.org/t/rfc-jsobjectliteral-jl/20584/5 "2019-02-14T10:01:16Z")

</div>

For the interested, with the help of @thautwarm, [`JSObjectLiteral.jl`](https://github.com/davidavdav/JSObjectLiteral.jl) can now generate objects from more esoteric javascript expressions, such as

```julia
@js d = { e: exp(1) } ## Dict("e" => exp(1))
deep = @js { a.b.c: d.e, f: d } ## short for { a: { b: { c: d.e } }, f: d }
@js { a, f } = deep ## assignment-by-key-correspondence
@js a.b.c == exp(1) ## true
f == d ## true

```
