WaterSweet / mochiweb

Automatically exported from code.google.com/p/mochiweb
Other
0 stars 0 forks source link

json {struct,[{key,val}]} is unnecesarily verbose #73

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What is the point of {struct,obj} tuple? If the mochijson2:json_encode
function is modified to:

json_encode([{_,_}|_] = Props, State) ->
    json_encode_proplist(Props, State);
json_encode(Array, State) when is_list(Array) ->
    json_encode_array(Array, State);

It works just fine without it.

Original issue reported on code.google.com by rap...@gmail.com on 7 Jun 2010 at 2:27

GoogleCodeExporter commented 8 years ago
It's so that you can distinguish between a 0-element array and an object with 
no properties.

Original comment by bob.ippo...@gmail.com on 7 Jun 2010 at 2:43

GoogleCodeExporter commented 8 years ago
Ahh of course. A shame that the two mongodb drivers use the opposite solution
([{_,_},..] = object and {array,[...]}). Would you guys be willing to have the
json_encode function be more permissible?

json_encode([{_,_}|_] = Props, State) ->
    json_encode_proplist(Props, State);
json_encode({struct, Props}, State) when is_list(Props) ->
    json_encode_proplist(Props, State);
json_encode(Array, State) when is_list(Array) ->
    json_encode_array(Array, State);
json_encode({array, Array}, State) when is_list(Array) ->
    json_encode_array({array,Array}, State);

Original comment by rap...@gmail.com on 7 Jun 2010 at 3:15

GoogleCodeExporter commented 8 years ago
If you submit a patch with passing tests then I'll take a look at adding it

Original comment by bob.ippo...@gmail.com on 7 Jun 2010 at 3:18

GoogleCodeExporter commented 8 years ago
Patch attached. I added encoding tests for objects without struct tuple.

Original comment by rap...@gmail.com on 7 Jun 2010 at 3:45

Attachments:

GoogleCodeExporter commented 8 years ago
http://github.com/mochi/mochiweb/commit/c1c9431

Original comment by bob.ippo...@gmail.com on 23 Jul 2010 at 9:54

GoogleCodeExporter commented 8 years ago
After using my patch in multiple projects and testing with many possible json 
document combinations, I since found one bug. Patch attached. It's a very 
change simple.
I'm sorry for posting it so late, I forgot and the issue email reminded me.

Original comment by rap...@gmail.com on 24 Jul 2010 at 7:33

Attachments:

GoogleCodeExporter commented 8 years ago
After using my patch in multiple projects and testing with many possible json 
document combinations, I since found one bug. Patch attached. It's a very 
change simple.
I'm sorry for posting it so late, I forgot and the issue email reminded me.

Original comment by rap...@gmail.com on 24 Jul 2010 at 7:34

Attachments:

GoogleCodeExporter commented 8 years ago
After using my patch in multiple projects and testing with many possible json 
document combinations, I since found one bug. Patch attached. It's a very 
change simple.
I'm sorry for posting it so late, I forgot and the issue email reminded me.

Original comment by rap...@gmail.com on 24 Jul 2010 at 7:35

Attachments: