Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 Usage Examples for JSON - Part 2 #332

Open
wants to merge 2 commits into
base: usage-examples
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using SplashKitSDK;

namespace JsonFromFile
{
public class Program
{
public static void Main()
{
// Create a JSON object
Json json_obj = SplashKit.CreateJson();
SplashKit.JsonSetString(json_obj, "name", "Breezy");
SplashKit.JsonSetNumber(json_obj, "age", 25);
SplashKit.JsonSetBool(json_obj, "is_active", true);

// Save the JSON object to the file
SplashKit.WriteLine("Saving JSON to file...");
SplashKit.JsonToFile(json_obj, "example.json");

// Free the original JSON object
SplashKit.FreeJson(json_obj);
SplashKit.WriteLine("Original JSON object freed.");

// Load the JSON object back from the file
SplashKit.WriteLine("Reading JSON from file...");
Json json_from_file_obj = SplashKit.JsonFromFile("example.json");

// Display the JSON object read from the file
SplashKit.WriteLine("JSON read from file:");
SplashKit.WriteLine(SplashKit.JsonToString(json_from_file_obj));

// Free the loaded JSON object
SplashKit.FreeJson(json_from_file_obj);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create a JSON object
Json json_obj = CreateJson();
JsonSetString(json_obj, "name", "Breezy");
JsonSetNumber(json_obj, "age", 25);
JsonSetBool(json_obj, "is_active", true);

// Save the JSON object to the file
WriteLine("Saving JSON to file...");
JsonToFile(json_obj, "example.json");

// Free the original JSON object
FreeJson(json_obj);
WriteLine("Original JSON object freed.");

// Load the JSON object back from the file
WriteLine("Reading JSON from file...");
Json json_from_file_obj = JsonFromFile("example.json");

// Display the JSON object read from the file
WriteLine("JSON read from file:");
WriteLine(JsonToString(json_from_file_obj));

// Free the loaded JSON object
FreeJson(json_from_file_obj);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "splashkit.h"

int main()
{
// Create a JSON object
json json_obj = create_json();
json_set_string(json_obj, "name", "Breezy");
json_set_number(json_obj, "age", 25);
json_set_bool(json_obj, "is_active", true);

// Save the JSON object to the file
write_line("Saving JSON to file...");
json_to_file(json_obj, "example.json");

// Free the original JSON object
free_json(json_obj);
write_line("Original JSON object freed.");

// Load the JSON object back from the file
write_line("Reading JSON from file...");
json json_from_file_obj = json_from_file("example.json");

// Display the JSON object read from the file
write_line("JSON read from file:");
write_line(json_to_string(json_from_file_obj));

// Free the loaded JSON object
free_json(json_from_file_obj);

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from splashkit import *

# Create a JSON object
json_obj = create_json()
json_set_string(json_obj, "name", "Breezy")
json_set_number_integer(json_obj, "age", 25)
json_set_bool(json_obj, "is_active", True)

# Save the JSON object to the file
write_line("Saving JSON to file...")
json_to_file(json_obj, "example.json")

# Free the original JSON object
free_json(json_obj)
write_line("Original JSON object freed.")

# Load the JSON object back from the file
write_line("Reading JSON from file...")
json_from_file_obj = json_from_file("example.json")

# Display the JSON object read from the file
write_line("JSON read from file:")
write_line(json_to_string(json_from_file_obj))

# Free the loaded JSON object
free_json(json_from_file_obj)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Making and loading JSON from a file

The following code shows examples of using [json from file](/api/json/#json-from-file) to load JSON from a file after creating it. You will need to run `skm resources` to create the location the file will be saved to.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using SplashKitSDK;

namespace JsonFromString
{
public class Program
{
public static void Main()
{
// JSON string to be converted
string json_string = "{\"name\": \"Breezy\", \"age\": 25}";

// Create a JSON object from the string
Json json_obj = SplashKit.JsonFromString(json_string);

// Display the JSON object as a string
SplashKit.WriteLine("JSON from String: " + SplashKit.JsonToString(json_obj));

// Free the JSON object
SplashKit.FreeJson(json_obj);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// JSON string to be converted
string json_string = "{\"name\": \"Breezy\", \"age\": 25}";

// Create a JSON object from the string
Json json_obj = JsonFromString(json_string);

// Display the JSON object as a string
WriteLine("JSON from String: " + JsonToString(json_obj));

// Free the JSON object
FreeJson(json_obj);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "splashkit.h"

int main()
{
// JSON string to be converted
string json_string = "{\"name\": \"Breezy\", \"age\": 25}";

// Create a JSON object from the string
json json_obj = json_from_string(json_string);

// Display the JSON object as a string
write_line("JSON from String: " + json_to_string(json_obj));

// Free the JSON object
free_json(json_obj);

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from splashkit import *

# JSON string to be converted
json_string = "{\"name\": \"Breezy\", \"age\": 25}"

# Create a JSON object from the string
json_obj = json_from_string(json_string)

# Display the JSON object as a string
write_line("JSON from String: " + json_to_string(json_obj))

# Free the JSON object
free_json(json_obj)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Get string from JSON object

The following code shows an example of using [json from string](/api/json/#json-from-string) to create a JSON object with a string, and then call `json_to_string` to get the string JSON.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "splashkit.h"

int main()
{
// Create a JSON object
json json_obj = create_json();
json_set_string(json_obj, "name", "Breezy");

// Check if the JSON object contains specific keys
string key1 = "name";
string key2 = "age";

if (json_has_key(json_obj, key1))
{
write_line("Key '" + key1 + "' exists in the JSON object.");
}
else
{
write_line("Key '" + key1 + "' does not exist in the JSON object.");
}

if (json_has_key(json_obj, key2))
{
write_line("Key '" + key2 + "' exists in the JSON object.");
}
else
{
write_line("Key '" + key2 + "' does not exist in the JSON object.");
}

// Free the JSON object
free_json(json_obj);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create a JSON object
Json json_obj = CreateJson();
JsonSetString(json_obj, "name", "Breezy");

// Check if the JSON object contains specific keys
string key1 = "name";
string key2 = "age";

if (JsonHasKey(json_obj, key1))
{
WriteLine("Key '" + key1 + "' exists in the JSON object.");
}
else
{
WriteLine("Key '" + key1 + "' does not exist in the JSON object.");
}

if (JsonHasKey(json_obj, key2))
{
WriteLine("Key '" + key2 + "' exists in the JSON object.");
}
else
{
WriteLine("Key '" + key2 + "' does not exist in the JSON object.");
}

// Free the JSON object
FreeJson(json_obj);
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "splashkit.h"

int main()
{
// Create a JSON object
json json_obj = create_json();
json_set_string(json_obj, "name", "Breezy");

// Check if the JSON object contains specific keys
string key1 = "name";
string key2 = "age";

if (json_has_key(json_obj, key1))
{
write_line("Key '" + key1 + "' exists in the JSON object.");
}
else
{
write_line("Key '" + key1 + "' does not exist in the JSON object.");
}

if (json_has_key(json_obj, key2))
{
write_line("Key '" + key2 + "' exists in the JSON object.");
}
else
{
write_line("Key '" + key2 + "' does not exist in the JSON object.");
}

// Free the JSON object
free_json(json_obj);

return 0;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions public/usage-examples/json/json_has_key/json_has_key-1-find-key.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from splashkit import *

# Create a JSON object
json_obj = create_json()
json_set_string(json_obj, "name", "Breezy")

# Check if the JSON object contains specific keys
key1 = "name"
key2 = "age"

if json_has_key(json_obj, key1):
write_line(f"Key '{key1}' exists in the JSON object.")
else:
write_line(f"Key '{key1}' does not exist in the JSON object.")

if json_has_key(json_obj, key2):
write_line(f"Key '{key2}' exists in the JSON object.")
else:
write_line(f"Key '{key2}' does not exist in the JSON object.")

# Free the JSON object
free_json(json_obj)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Check is a key exists in a JSON object

The following code shows an example of using [json has key](/api/json/#json-has-key) to create a JSON object with a key, and then call `json_has_key` to check if the key exists in the JSON.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using SplashKitSDK;

namespace JsonReadBool
{
public class Program
{
public static void Main()
{
// Create a JSON object with a boolean value
Json json_obj = SplashKit.CreateJson();
SplashKit.JsonSetBool(json_obj, "is_active", true);

// Read the boolean value from the JSON object
bool is_active = SplashKit.JsonReadBool(json_obj, "is_active");

// Display the boolean value
SplashKit.WriteLine("Is Active: " + is_active.ToString());

// Free the JSON object
SplashKit.FreeJson(json_obj);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create a JSON object with a boolean value
Json json_obj = CreateJson();
JsonSetBool(json_obj, "is_active", true);

// Read the boolean value from the JSON object
bool is_active = JsonReadBool(json_obj, "is_active");

// Display the boolean value
WriteLine("Is Active: " + is_active.ToString());

// Free the JSON object
FreeJson(json_obj);
Loading