Skip to content

Commit d29fc5f

Browse files
Syncing changes from private repository
1 parent eecb605 commit d29fc5f

File tree

9 files changed

+72
-15
lines changed

9 files changed

+72
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file using the standards as defined at [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0).
33

4+
### Version 1.0.2 *(2024-09-19)*
5+
Added:
6+
- `toObjectFromJson` in `ChartboostUnityUtilities.h` for reusable JSON serialization.
7+
48
### Version 1.0.1 *(2024-08-01)*
59
Added:
610

Chartboost.CSharp.Utilities.Unity.nuspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>Chartboost.CSharp.Utilities.Unity</id>
5-
<version>1.0.1</version>
5+
<version>1.0.2</version>
66
<title>Chartboost Utilities for Unity</title>
77
<description>Reusable Utilities for Chartboost's Unity Projects</description>
88
<authors>Chartboost</authors>
@@ -13,7 +13,7 @@
1313
<tags>Chartboost, Ads, Mediation, Unity, cs</tags>
1414
<repository type="git" url="https://github.com/ChartBoost/chartboost-unity-utilities"/>
1515
<dependencies>
16-
<dependency id="Chartboost.CSharp.Logging.Unity" version="1.0.0" />
16+
<dependency id="Chartboost.CSharp.Logging.Unity" version="1.1.0" />
1717
</dependencies>
1818
</metadata>
1919
</package>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Chartboost Canary Utilities
1+
# Chartboost Unity Utilities
22

33
Simple utilities package for the Chartboost Unity development environment.
44

Runtime/Android/AndroidExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using Chartboost.Constants;
43
using Chartboost.Logging;
54
using UnityEngine;
65

76
namespace Chartboost
87
{
9-
public static partial class AndroidExtensions
8+
public static class AndroidExtensions
109
{
1110
public static AndroidJavaObject DictionaryToMap(this IDictionary<string, string> source)
1211
=> DictionaryToMap(source, SharedAndroidConstants.ClassString);
1312

1413
public static AndroidJavaObject DictionaryToMap(this IReadOnlyDictionary<string, string> source)
1514
=> DictionaryToMap(source, SharedAndroidConstants.ClassString);
16-
1715
public static AndroidJavaObject DictionaryToMap(this IReadOnlyDictionary<string, bool> source)
1816
=> DictionaryToMap(source, SharedAndroidConstants.ClassBoolean);
1917

Runtime/Android/Constants/SharedAndroidConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public static AndroidJavaObject UnityPlayerCurrentActivity()
4848
public const string FunctionSet = "set";
4949
public const string FunctionPut = "put";
5050
public const string FunctionAdd = "add";
51+
public const string FunctionCompleted = "completed";
5152

5253
public const string FunctionGetLogLevel = "getLogLevel";
5354
public const string FunctionSetLogLevel = "setLogLevel";

Runtime/Utilities/Json/JsonTools.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ public static class JsonTools
1010
private const string JsonToolsTag = "[JsonTools]";
1111

1212
public static string SerializeObject<T>(T target, Formatting formatting)
13-
=> JsonConvert.SerializeObject(target, formatting);
14-
15-
public static string SerializeObject<T>(T target)
16-
=> SerializeObject(target, Formatting.None);
13+
{
14+
if (target != null) return JsonConvert.SerializeObject(target, formatting);
15+
LogController.Log($"{JsonToolsTag}/SerializeObject string value cannot be null or empty.", LogLevel.Warning);
16+
return string.Empty;
17+
}
18+
19+
public static string SerializeObject<T>(T target)
20+
{
21+
return SerializeObject(target, Formatting.None);
22+
}
1723

1824
public static T DeserializeObject<T>(string objectJson)
1925
{

Runtime/iOS/Plugins/iOS/ChartboostUnityUtilities.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ typedef void (^block)(void);
88
/// - Parameter block: block of code to be executed on the main thread.
99
void toMain(block block);
1010

11+
/// Creates an id from a C String
12+
///
13+
/// - Parameter jsonString: Json string to create id from.
14+
id toObjectFromJson(const char * jsonString);
15+
1116
/// Creates a null terminated C string on the heap so that our string's memory isn't wiped out right after method's return
1217
///
1318
/// - Parameter target: Target C string to allocate
@@ -60,6 +65,23 @@ NSMutableArray* toNSMutableArray(const char** cArray, int cArrayCount);
6065
///
6166
/// Use to allocate a C String reference when converting from id serializable.
6267
///
63-
/// - Parameter cString: C String to use as a base for the NSString.
68+
/// - Parameter data: C String to use as a base for the NSString.
6469
/// - Returns: Allocated JSON C String or Null.
6570
const char * toJSON(id data);
71+
72+
73+
/// Allocates a JSON NSString from an id
74+
///
75+
/// Use to allocate a NSString reference when converting from id serializable.
76+
///
77+
/// - Parameter data: NString to use as a base for the NSString.
78+
/// - Returns: Allocated JSON NSString or Null.
79+
NSString * toJSONNSString(id data);
80+
81+
/// Generates a int hashCode from a NSString
82+
///
83+
/// Use to obtain a Java like hashCode from a NSString
84+
///
85+
/// - Parameter value: Target NSString.
86+
/// - Returns: int Java like hashCode
87+
int hashCode(NSString* value);

Runtime/iOS/Plugins/iOS/ChartboostUnityUtilities.mm

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ void toMain(block block) {
44
dispatch_async(dispatch_get_main_queue(), block);
55
}
66

7+
id toObjectFromJson(const char * jsonString) {
8+
NSData* jsonData = [[NSString stringWithUTF8String:jsonString] dataUsingEncoding:NSUTF8StringEncoding];
9+
NSError* error = nil;
10+
id arr = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
11+
12+
if (error != nil)
13+
return nil;
14+
15+
return arr;
16+
}
17+
718
const char * allocateCString(const char* target)
819
{
920
if (target == NULL)
@@ -62,6 +73,12 @@ void toMain(block block) {
6273
}
6374

6475
const char * toJSON(id data)
76+
{
77+
return toCStringOrNull(toJSONNSString(data));
78+
}
79+
80+
81+
NSString * toJSONNSString(id data)
6582
{
6683
if (data == nil)
6784
return NULL;
@@ -72,6 +89,15 @@ void toMain(block block) {
7289
NSLog(@"%s: error: %@", __func__, error.localizedDescription);
7390
return NULL;
7491
}
75-
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
76-
return toCStringOrNull(json);
92+
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
93+
}
94+
95+
int hashCode(NSString* value) {
96+
int h = 0;
97+
98+
for (int i = 0; i < (int)value.length; i++) {
99+
h = (31 * h) + [value characterAtIndex:i];
100+
}
101+
102+
return h;
77103
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.chartboost.unity.utilities",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"displayName": "Chartboost Utilities",
55
"description": "Reusable Utilities for Chartboost's Unity Projects",
66
"unity": "2022.3",
@@ -14,6 +14,6 @@
1414
"hideInEditor": false,
1515
"dependencies": {
1616
"com.unity.nuget.newtonsoft-json": "3.2.1",
17-
"com.chartboost.unity.logging": "1.0.0"
17+
"com.chartboost.unity.logging": "1.1.0"
1818
}
1919
}

0 commit comments

Comments
 (0)