clugg / sm-json

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

[Feature Req] Dump to file function #20

Closed alexevladgabriel closed 2 years ago

alexevladgabriel commented 3 years ago

Is your feature request related to a problem? Please describe. I'm frustrated, to know I can't dump to a file the JSON, to check if it's complete or valid.

Describe the solution you'd like Make a function what dump the json content to file (in SM-Jansson json_dump_file function). I know this library is made with StringMap, and SM-Jansson is a extension.

Describe alternatives you've considered I don't think there are any alternatives.

Additional context

/**
 * Write the JSON representation of hObject to the file sFilePath.
 * If sFilePath already exists, it is overwritten.
 *
 * @param hObject           String containing valid JSON
 * @param sFilePath         Buffer to store the created JSON string.
 * @param iIndentWidth      Indenting with iIndentWidth spaces.
 *                          The valid range for this is between 0 and 31 (inclusive),
 *                          other values result in an undefined output. If this is set
 *                          to 0, no newlines are inserted between array and object items.
 * @param bEnsureAscii      If this is set, the output is guaranteed
 *                          to consist only of ASCII characters. This is achieved
 *                          by escaping all Unicode characters outside the ASCII range.
 * @param bSortKeys         If this flag is used, all the objects in output are sorted
 *                          by key. This is useful e.g. if two JSON texts are diffed
 *                          or visually compared.
 * @param bPreserveOrder    If this flag is used, object keys in the output are sorted
 *                          into the same order in which they were first inserted to
 *                          the object. For example, decoding a JSON text and then
 *                          encoding with this flag preserves the order of object keys.
 * @return                  Length of the returned string or -1 on error.
 */

native bool:json_dump_file(Handle:hObject, const String:sFilePath[], iIndentWidth = 4, bool:bEnsureAscii = false, bool:bSortKeys = false, bool:bPreserveOrder = false); json_dump_file(hJson, "json.txt", 0, true); That is the function from Jansson.

MAGNAT2645 commented 3 years ago

You can try to use JSON_Object.Encode with (or without) JSON_ENCODE_PRETTY flag and then File.WriteLine.

JSON_Object hObj = new JSON_Object();
hObj.SetInt( "key1", 50 );
hObj.SetString( "key2", "value" );
hObj.SetBool( "key3", true );

char szBuffer[PLATFORM_MAX_PATH];
BuildPath( Path_SM, szBuffer, sizeof szBuffer, "data/json.txt" );
File hFile = OpenFile( szBuffer, "w" );

hObj.Encode( szBuffer, sizeof szBuffer, JSON_ENCODE_PRETTY );
hFile.WriteLine( "%s", szBuffer );

hFile.Close();
hObj.Close();
alexevladgabriel commented 3 years ago

One question, in methodmaps, I had created a function AddField, but that function fails, I had tried to use Length and PushObject but didn't worked, causing handle errors.

property JSON_Array Fields
{
    public get() 
    {
        return view_as<JSON_Array>(this.GetObject("fields"));
    }

    public set(JSON_Array value) 
    {
        this.SetObject("fields", value);
    }
}

public void AddField(const char[] name, const char[] value, bool inline) 
{
    JSON_Object hObj = new JSON_Object();
    hObj.SetString("name", name);
    hObj.SetString("value", value);
    hObj.SetBool("inline", inline);

    JSON_Array hArray = this.Fields;
    PrintToServer("Bool: %i; Fields: %i", hArray.IsArray, this.Fields.IsArray);
    if(hArray == null) {
        hArray = new JSON_Array();
    }

    //json_array_append_new(hArray, hObj);
    hArray.PushObject(hObj);

    this.Fields = hArray;
    delete hArray;
}

Yeah, I realized that and tried. It had worked, but still is somethign what would make a little easy the life of developers. And I had created a new function json_rename() to be able to rename the key values from JSON.

MAGNAT2645 commented 3 years ago

Are you making something Discord-related? If yes then you should use Discord-API, there are methodmaps for embeds and fields (https://github.com/KillStr3aK/discord-api/blob/main/include/discord/DiscordEmbedField.inc).

alexevladgabriel commented 3 years ago

@MAGNAT2645 Yes, I'm rewriting the Discord API code base. The Discord API was using Jansson (extension), and I started yesterday on rewriting all the base to sm-json.

MAGNAT2645 commented 3 years ago

No, Discord-API (which i provided above) uses sm-json.

alexevladgabriel commented 3 years ago

Yes, I had saw. I will continue to work on my rewrite with source inspiration from the other vendor. This is the one, I rewrite Discord-API Do you have discord? (Scai#8477)

MAGNAT2645 commented 3 years ago

I have, but if you want to talk about your rewrite there then don't. Sorry.

clugg commented 3 years ago

Thanks for the suggestion - will also be focusing on this for the upcoming release.

clugg commented 2 years ago

I've just released v4.0.0. You can find the relevant documentation for this feature below:

Thanks again for the suggestion!