json.lua icon indicating copy to clipboard operation
json.lua copied to clipboard

Improved table encoding code to handle sparse arrays.

Open aryajur opened this issue 5 years ago • 5 comments

By going through the table before to get the maximum index we can handle sparse arrays. Also tostring seems to work better by maintaining float and integer number conversion.

aryajur avatar Jul 19 '20 22:07 aryajur

Hello! This would be nice change, but table.pack() creates an array with property n. Would it be possible to add check to line 71, that if k == "n" and type(v) == "number" then length = v else treat as object? Lua documentation: https://www.lua.org/manual/5.2/manual.html#pdf-table.pack Current behaviour:

> json.encode({1, nil, 3})
"[1,null,3]"
> json.encode(table.pack(1, nil, 3))
"{1:1,3:3,\"n\":3}"

Why is this beneficial? If lua array ends with nil(s), they are trimmed. If we however use table.pack, the original count of objects is stored in property n allowing the array to end with nil/null.

Mysak0CZ avatar Aug 18 '20 08:08 Mysak0CZ

Fixed that. Now arrays can be specified through table.pack.

aryajur avatar Aug 19 '20 04:08 aryajur

@appgurueu Is there a reason for your abrupt and occasionally hostile behavior?

You're coming off as unnecessarily rude.

ninjamonkey198206 avatar Nov 25 '23 06:11 ninjamonkey198206

@appgurueu Is there a reason for your abrupt and occasionally hostile behavior?

You're coming off as unnecessarily rude.

I'm not sure what you consider to be rude about my review. I made three comments:

  • Pointing out a possible worst-case scenario.
  • Commenting on the bad formatting.
  • Labeling a clear code smell (outcommented code) as "dirty". If it isn't dirty, is it clean? I believe(d) this to be self-evident; it does not need elaboration. Maybe I should have phrased this differently, as "oops, looks like you forgot some outcommented code here"?
  • Pointing out that using tostring on numbers is unreliable (if a certain format is expected). I should have elaborated on this; I didn't because it is something I am used to.

This was intended just as very "direct" feedback from a quick look. In hindsight, some of it would have needed some elaboration.

If I came off as rude to @aryajur, that was not my intention, and I apologize for that.

appgurueu avatar Nov 25 '23 12:11 appgurueu

@appgurueu Is there a reason for your abrupt and occasionally hostile behavior? You're coming off as unnecessarily rude.

I'm not sure what you consider to be rude about my review. I made three comments:

  • Pointing out a possible worst-case scenario.
  • Commenting on the bad formatting.
  • Labeling a clear code smell (outcommented code) as "dirty". If it isn't dirty, is it clean? I believe(d) this to be self-evident; it does not need elaboration. Maybe I should have phrased this differently, as "oops, looks like you forgot some outcommented code here"?
  • Pointing out that using tostring on numbers is unreliable (if a certain format is expected). I should have elaborated on this; I didn't because it is something I am used to.

This was intended just as very "direct" feedback from a quick look. In hindsight, some of it would have needed some elaboration.

If I came off as rude to @aryajur, that was not my intention, and I apologize for that.

I appreciate your feedback. Would always want to improve my work.

aryajur avatar Nov 28 '23 04:11 aryajur