clugg / sm-json

A pure SourcePawn JSON encoder/decoder.
GNU General Public License v3.0
82 stars 8 forks source link

Some Additions and Improvements #12

Closed MAGNAT2645 closed 4 years ago

MAGNAT2645 commented 4 years ago

Would be good to have new helper methods for JSON_Array like JSON_Array.Swap, JSON_Array.ShiftUp and maybe JSON_Array.Sort (only by integers, floats etc.)

EDIT: Also could you add setters for JSON_Array so we'll be able to replace values in specific index? EDIT 2: Also i think JSON_Array.Push* should return a new index instead of bool (any positive (or null) value instead of true and -1 instead of false)

clugg commented 4 years ago

Hey there! First, thanks for being so active here - it is nice to know that someone is getting good use out of this library.

All of the functionality in the first point I will attempt to include in the next minor release. I would consider points 2 and 3 breaking changes so they will require a new major release.

Thanks again! I will keep this issue updated as I progress.

MAGNAT2645 commented 4 years ago

This library is important for me because i use it for my private Store/Shop system (with inventory and equipment features). I use JSON_Object a lot for Item Attributes (so i can change things like Item name, item description, item max stack etc. and attributes depend on item type and its actions used). For example: i have item that works like letter. There's actually two of this item: blank letter (that you can write and send to other players) and finished letter (its name displays like "Letter from [AUTHOR]" where [AUTHOR] - string value used by letterAuthor attribute)

Recently i started to use this library for players CustomData so every player has its individual data (i can use such data to detect any changes that player made on specific maps etc.) that saves to MySQL DB.

MAGNAT2645 commented 4 years ago

You can also replace some Format functions with FormatEx (only if input buffer and output are not same variable) to increase some speed.

EDIT: Also since we have JSON_Array.parent (idk why but would be good to have uppercase name Parent) we should also have JSON_Object.Child or JSON_Object.ToArray property (this would eliminate need to use view_as<JSON_Array>())

clugg commented 4 years ago

Thanks for the tip about FormatEx - I hadn't seen that before, will also be a part of the next update. As for the Child/ToArray property, unfortunately that is not possible due to circular reference limitations in SourceMod.

clugg commented 4 years ago

I have replaced Format with FormatEx in v2.4.0. I have been thinking a bit about how I want to accomplish the rest of the things you've suggested and am thinking some of it might be out of scope in terms of my intentions for the library. That being said, Sourcemod already provides some of these methods for native arrays, so I am thinking I might add a to/from native array method with which you can perform whatever native functions you want on before converting back to a JSON_Array. The next release will likely be a major - there are some other things I've been wanting to change that I might look into as well.

clugg commented 4 years ago

Apologies for the delay. I have just released v3.0.0. Pushing values now returns their index as opposed to true or false, as well as many other things.

I haven't yet added some of your other suggestions but I think I may have done one better - you can now export JSON_Array values to a native array (e.g. int values[]). This is explained in the README and in the release notes. Once it is exported to a native array, you will be able to use all of Sourcemod's native functions on it (e.g. SortIntegers), which will hopefully provide all of the functionality you require. Once you're finished with it, you can import it back into a JSON_Array, which is also explained in the README and release notes.

Let me know what you think of this. Thanks!

MAGNAT2645 commented 4 years ago

Sounds cool! Thank you for this update.