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 1 #331

Open
wants to merge 2 commits into
base: usage-examples
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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,20 @@
using SplashKitSDK;

namespace CreateJson
{
public class program
{
public static void Main()
{
// Create an empty JSON object
Json jsonObj = SplashKit.CreateJson();

// Add some data to the JSON object
SplashKit.JsonSetString(jsonObj, "name", "Breezy");
SplashKit.JsonSetNumber(jsonObj, "age", 25);

// Display the JSON object as a string
SplashKit.WriteLine("Created JSON: " + SplashKit.JsonToString(jsonObj));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create an empty JSON object
Json jsonObj = CreateJson();

// Add some data to the JSON object
JsonSetString(jsonObj, "name", "Breezy");
JsonSetNumber(jsonObj, "age", 25);

// Display the JSON object as a string
WriteLine("Created JSON: " + JsonToString(jsonObj));
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "splashkit.h"

int main()
{
// Create an empty JSON object
json json_obj = create_json();

// Add some data to the JSON object
json_set_string(json_obj, "name", "Breezy");
json_set_number(json_obj, "age", 25);

// Display the JSON object as a string
write_line("Created JSON: " + json_to_string(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,11 @@
from splashkit import *

# Create an empty JSON object
json_obj = create_json()

# Add some data to the JSON object
json_set_string(json_obj, "name", "Breezy")
json_set_number_integer(json_obj, "age", 25)

# Display the JSON object as a string
write_line("Created JSON: " + json_to_string(json_obj))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Create a JSON

The following code shows an example of using [Create Json](/api/json/#create-json-1) to create a simple json object.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using SplashKitSDK;

namespace CreateJsonString
{
public class Program
{
public static void Main()
{
// JSON string to convert to a JSON object
string jsonString = "{\"name\": \"Breezy\", \"age\": 25}";

// Create a JSON object from the string
Json jsonObj = SplashKit.CreateJson(jsonString);

// Read and display values from the JSON object
SplashKit.WriteLine("Name: " + SplashKit.JsonReadString(jsonObj, "name"));
SplashKit.WriteLine("Age: " + SplashKit.JsonReadNumber(jsonObj, "age").ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// JSON string to convert to a JSON object
string jsonString = "{\"name\": \"Breezy\", \"age\": 25}";

// Create a JSON object from the string
Json jsonObj = CreateJson(jsonString);

// Read and display values from the JSON object
WriteLine("Name: " + JsonReadString(jsonObj, "name"));
WriteLine("Age: " + JsonReadNumber(jsonObj, "age").ToString());
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "splashkit.h"

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

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

// Read and display values from the JSON object
write_line("Name: " + json_read_string(json_obj, "name"));
write_line("Age: " + std::to_string(json_read_number(json_obj, "age")));

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,11 @@
from splashkit import *

# JSON string to convert to a JSON object
json_string = "{\"name\": \"Breezy\", \"age\": 25}"

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

# Read and display values from the JSON object
write_line("Name: " + json_read_string(json_obj, "name"))
write_line("Age: " + str(json_read_number(json_obj, "age")))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Create a JSON from a string

The following code shows an example of using [Create Json From String](/api/json/#create-json-2) to create a simple json object from a string.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using SplashKitSDK;

namespace FreeAllJson
{
public class Program
{
public static void Main()
{
// Create multiple JSON objects
Json json1 = SplashKit.CreateJson();
Json json2 = SplashKit.CreateJson();

// Add some data to the JSON objects
SplashKit.JsonSetString(json1, "name", "Breezy");
SplashKit.JsonSetNumber(json2, "age", 25);

SplashKit.WriteLine("Json1: " + SplashKit.JsonToString(json1));
SplashKit.WriteLine("Json2: " + SplashKit.JsonToString(json2));

// Free all JSON objects
SplashKit.WriteLine("Freeing all JSON objects...");
SplashKit.FreeAllJson();

// These should now display a warning of an invalid JSON object
// Attempting to use json1 or json2 after this would be invalid
SplashKit.WriteLine("Json1: " + SplashKit.JsonToString(json1));
SplashKit.WriteLine("Json2: " + SplashKit.JsonToString(json2));

SplashKit.WriteLine("All JSON objects have been freed.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create multiple JSON objects
Json json1 = CreateJson();
Json json2 = CreateJson();

// Add some data to the JSON objects
JsonSetString(json1, "name", "Breezy");
JsonSetNumber(json2, "age", 25);

WriteLine("Json1: " + JsonToString(json1));
WriteLine("Json2: " + JsonToString(json2));

// Free all JSON objects
WriteLine("Freeing all JSON objects...");
FreeAllJson();

// These should now display a warning of an invalid JSON object
// Attempting to use json1 or json2 after this would be invalid
WriteLine("Json1: " + JsonToString(json1));
WriteLine("Json2: " + JsonToString(json2));

WriteLine("All JSON objects have been freed.");
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "splashkit.h"

int main()
{
// Create multiple JSON objects
json json1 = create_json();
json json2 = create_json();

// Add some data to the JSON objects
json_set_string(json1, "name", "Breezy");
json_set_number(json2, "age", 25);

write_line("Json1: " + json_to_string(json1));
write_line("Json2: " + json_to_string(json2));

// Free all JSON objects
write_line("Freeing all JSON objects...");
free_all_json();

// These should now display a warning of an invalid JSON object
// Attempting to use json1 or json2 after this would be invalid
write_line("Json1: " + json_to_string(json1));
write_line("Json2: " + json_to_string(json2));

write_line("All JSON objects have been freed.");

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,23 @@
from splashkit import *

# Create multiple JSON objects
json1 = create_json()
json2 = create_json()

# Add some data to the JSON objects
json_set_string(json1, "name", "Breezy")
json_set_number_integer(json2, "age", 25)

write_line("Json1: " + json_to_string(json1))
write_line("Json2: " + json_to_string(json2))

# Free all JSON objects
write_line("Freeing all JSON objects...")
free_all_json()

# These should now display a warning of an invalid JSON object
# Attempting to use json1 or json2 after this would be invalid
write_line("Json1: " + json_to_string(json1))
write_line("Json2: " + json_to_string(json2))

write_line("All JSON objects have been freed.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Create and free all JSON objects

The following code shows an example of using [Free All Json](/api/json/#free-all-json) to create 2 JSON objects and then free them. It also shows how when you try call a JSON you freed you get a log warning message.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SplashKitSDK;

namespace FreeJson
{
public class Program
{
public static void Main()
{
// Create a single JSON object
Json json = SplashKit.CreateJson();

// Add some data to the JSON object
SplashKit.JsonSetString(json, "name", "Breezy");
SplashKit.WriteLine("Json: " + SplashKit.JsonToString(json));

// Free the JSON object
SplashKit.WriteLine("Freeing the JSON object...");
SplashKit.FreeJson(json);

// These should now display a warning of an invalid JSON object
// Attempting to use json after this would be invalid
SplashKit.WriteLine("Json: " + SplashKit.JsonToString(json));

SplashKit.WriteLine("The JSON object has been freed.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using SplashKitSDK;
using static SplashKitSDK.SplashKit;

// Create a single JSON object
Json json = CreateJson();

// Add some data to the JSON object
JsonSetString(json, "name", "Breezy");
WriteLine("Json: " + JsonToString(json));

// Free the JSON object
WriteLine("Freeing JSON object...");
FreeJson(json);

// This should now display a warning of an invalid JSON object
// Attempting to use json after this would be invalid
WriteLine("Json: " + JsonToString(json));

WriteLine("The JSON object has been freed.");
23 changes: 23 additions & 0 deletions public/usage-examples/json/free_json/free_json_1-free-single.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "splashkit.h"

int main()
{
// Create a single JSON object
json json = create_json();

// Add some data to the JSON object
json_set_string(json, "name", "Breezy");
write_line("Json1: " + json_to_string(json));

// Free the JSON object
write_line("Freeing the JSON object...");
free_json(json);

// These should now display a warning of an invalid JSON object
// Attempting to use json after this would be invalid
write_line("Json: " + json_to_string(json));

write_line("The JSON object has been freed.");

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

# Create a single JSON object
json = create_json()

# Add some data to the JSON object
json_set_string(json, "name", "Breezy")
write_line("Json: " + json_to_string(json))

# Free the JSON object
write_line("Freeing the JSON object...")
free_json(json)

# These should now display a warning of an invalid JSON object
# Attempting to use json after this would be invalid
write_line("Json: " + json_to_string(json))

write_line("The JSON object has been freed.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Create and free a specific JSON object

The following code shows an example of using [Free Json](/api/json/#free-json) to create a JSON object and then free it. It also shows how when you try to call a JSON you freed, you get a log warning message.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using SplashKitSDK;

namespace CountKeys
{
public class Program
{
public static void Main()
{
// Create a JSON object
Json json_obj = SplashKit.CreateJson();

// Add some data to the JSON object
SplashKit.JsonSetString(json_obj, "name", "Breezy");
SplashKit.JsonSetNumber(json_obj, "age", 25);
SplashKit.JsonSetString(json_obj, "hobby", "Coding");

// Count the keys in the JSON object
int key_count = SplashKit.JsonCountKeys(json_obj);

// Display the count of keys
SplashKit.WriteLine("The JSON object has " + key_count.ToString() + " keys.");

// Free the JSON object
SplashKit.FreeJson(json_obj);
}
}
}
Loading