Closed alexevladgabriel closed 2 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();
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.
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).
@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.
No, Discord-API (which i provided above) uses sm-json.
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)
I have, but if you want to talk about your rewrite there then don't. Sorry.
Thanks for the suggestion - will also be focusing on this for the upcoming release.
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
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.