[erlang-questions] No JSON/MAPS interoperability in 17.0?

Fred Hebert mononcqc@REDACTED
Sun Mar 16 19:33:46 CET 2014


Answers inline.

On 03/16, cri wrote:
> 
> To me, "Bait and switch" seems an unfair and inaccurate characterization. 
> Unfair due to the negative connotation and inaccurate, unless I am missing 
> something, because there is no implied "switch". 
> 
> 

the negative connotation is intended. As mention in my earlier emails, I
*hate* having to learn a solution, only to discover it's an incomplete
one, and then having to learn a new one again, while having the
incomplete one everywhere in my code base.

The 'switch' here is having me think you have this super elegant bit of
code only to find out it's all a lie and that I have to use something
else entirely to get the job done.

> Isn't there a contradiction here? Either any JSON library is somehow bound 
> to hang me out to dry or an acceptable solution with trade-offs  and and 
> workarounds is possible.
> 

No single JSON library will do *everything*. Most of them have strings
decoded as binaries, which eliminates the atoms case, but makes matching
uglier and cannot have dialyzer analyze types the same way for example.

Jsx can support both a streaming and a non-streaming API while being in
regular Erlang, but will be slower than a library like Jiffy.

Not all JSON libraries accept the same input format.

The difference is that most workarounds and options aren't afforded
through a single juggernaut library that does everything, but through
many of them so that you can choose what to use.

> It seems to me that the real question is whether JSON, even with its 
> imperfections, is so prevalent that a std library would be significantly 
> advantageous for Erlang developers.
> 

My point is that a standard library version as the one Joe described
would be extremely bad. Other libraries (say jsx) wouldn't be nearly
as bad, but they would be favoring some use cases heavily over others.

> I, being the Erlang novice that started this thread, wanted to use the once 
> imagined maps-to-json functions to convert my maps to json to store them in 
> mongo and vice versa. If this has been worked out in the wide, can I get a 
> reference?

See https://github.com/talentdeficit/jsx/issues/49 for one example

Again, be aware of the caveats mentioned in this here thread already.

The object <--> JSON conversion tends to go without an issue, because
it's relatively rare to have object attributes be other objects as a
name (they'll tend to be values, which JSON can handle), for example.

On the other hand, Erlang maps can have any term as a key, including
other maps. It can also have any value, some that are not representable
easily as strings and aren't covered by the JSON spec(s) (what's a
pid?). One has to be aware that even if you have a maps:to_json/1 or
maps:from_json/1 calls, it doesn't mean you could even convert most maps
with it, even less get the same exact map back that you encoded first.

The same goes for third party libraries, but at least, they don't have
the surprising attribute of being part of a module (or library) they
can't reliably convert.

Just keep in mind that you're *translating* to JSON, and *translating*
from JSON. Much like putting stuff through google translate back and
forth, the results might be surprising.

Regards,
Fred.



More information about the erlang-questions mailing list