None coming out as null in the json string

331 views
Skip to first unread message

simbo1905

unread,
Sep 7, 2013, 4:52:10 PM9/7/13
to argona...@googlegroups.com
Hey Folks, 

As Mark Hibbard so kindly helped me out with an argonaut example on the issue tracker #64 I thought I would pay the help on. So I answered someone's stackoverflow on json serialization to spread the good news about argonaut over at stackoverflow.com/questions/12591457

This is something a touch gnarly about the solution I gave. It turns the None members into member:"null" entries. So if we have a list: 

  val customers = List(
    Customer(1,"one",Some("one street"),Some("one city"),Some("one state"),1)
    , Customer(2,"two",None,Some("two city"),Some("two state"),2)
    , Customer(3,"three",Some("three address"),None,Some("three state"),3)
    , Customer(4,"four",Some("four address"),Some("four city"),None,4)
  )

Customers converted into json string with a null for each None:

 [{"city":"one city","name":"one","state":"one state","user_id":1,"id":1,"address":"one street"}
,{"city":"two city","name":"two","state":"two state","user_id":2,"id":2,"address":null}
,{"city":null,"name":"three","state":"three state","user_id":3,"id":3,"address":"three address"}
,{"city":"four city","name":"four","state":null,"user_id":4,"id":4,"address":"four address"}]

Someone doing some browerside programming with that json may be expecting undefined not null of members which are none. 

Is there a way to suppress the null entries such that the None values simply don't turn up in the string? 

Thanks, 

Simon



Mark Hibberd

unread,
Sep 8, 2013, 7:09:01 PM9/8/13
to simbo1905, argona...@googlegroups.com
On Sun, Sep 8, 2013 at 6:52 AM, simbo1905 <masse...@gmail.com> wrote:
> Someone doing some browerside programming with that json may be expecting
> undefined not null of members which are none.

This isn't as clear cut as you think, the difference between the field
not included and it included but it is set to none. The default
behaviour maintains as much info as possible, and if we receive json
we handle both null and field not existing as None so you get the
behaviour you expect when sending data to the server. Having said all
that, this is just the default behaviour because it is the safest and
most complete representation.

> Is there a way to suppress the null entries such that the None values simply
> don't turn up in the string?

I wish there was a simple way I could point you at, but you end up
losing some of the benefit of the convenience functions when you don't
follow the default behaviour. Approaches to dealing with it are:
- Define a custom EncodeJson that uses the `:=?` and `->?:`
operators (an example can be seen at http://argonaut.io/doc/json/).
These let you build a json structure that skips the field when it is
none.
- Define a post-processing step. Although this may have some
non-trivial performance impact if your json is large. Something like.
fred.asJson.withObject(o => /* filter out nulls on certain fields */)

There are possibly, better ways we could support this by using type
tags to give you the different behaviour. I have raised
https://github.com/argonaut-io/argonaut/issues/66 to look at it
further because this need has come up a few times recently.

Hopefully this will give you some ideas, if not I will try to do
something to make it easier, but it probably won't be until next week.

Cheers
Mark
Reply all
Reply to author
Forward
0 new messages