Skip to content

Commit

Permalink
changed json serializer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dducode committed Oct 15, 2024
1 parent 765360c commit 9e742be
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 100 deletions.
14 changes: 1 addition & 13 deletions Editor/SaveSystemSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ internal class SaveSystemSettingsProvider {
private SerializedProperty m_compressionSettingsProperty;
private SerializedProperty m_encryptProperty;
private SerializedProperty m_encryptionSettingsProperty;
private SerializedProperty m_jsonSerializerSettings;
private SerializedProperty m_cacheSizeProperty;


Expand Down Expand Up @@ -134,9 +133,6 @@ private void InitializeCommonSettings () {
m_encryptionSettingsProperty = m_serializedSettings.FindProperty(
nameof(SaveSystemSettings.encryptionSettings)
);
m_jsonSerializerSettings = m_serializedSettings.FindProperty(
nameof(SaveSystemSettings.jsonSerializerSettings)
);
m_cacheSizeProperty = m_serializedSettings.FindProperty(nameof(SaveSystemSettings.cacheSize));
}

Expand Down Expand Up @@ -173,7 +169,7 @@ private void DrawCommonSettings () {
EditorGUILayout.PropertyField(m_savePeriodProperty);
GUI.enabled = true;

DrawSerializerTypeProperty();
EditorGUILayout.PropertyField(m_serializerType);
EditorGUILayout.PropertyField(m_cacheSizeProperty);
DrawCompressionSettings();
DrawEncryptionSettings();
Expand All @@ -182,14 +178,6 @@ private void DrawCommonSettings () {
}


private void DrawSerializerTypeProperty () {
EditorGUILayout.PropertyField(m_serializerType);
var serializerType = (SerializerType)m_serializerType.enumValueIndex;
if (serializerType == SerializerType.JsonSerializer)
EditorGUILayout.PropertyField(m_jsonSerializerSettings);
}


private void DrawUserActionsProperties () {
EditorGUILayout.LabelField("User Actions", EditorStyles.boldLabel);

Expand Down
24 changes: 21 additions & 3 deletions Runtime/Map.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using UnityEngine;

namespace SaveSystemPackage {

[XmlRoot("map")]
[Serializable]
public class Map<Tkey, TValue> : Dictionary<Tkey, TValue>, IXmlSerializable {
public class Map<Tkey, TValue> : Dictionary<Tkey, TValue>, IXmlSerializable, ISerializationCallbackReceiver {

[SerializeField]
private SerializableKeyValuePair<Tkey, TValue>[] map;

public Map () { }
public Map (Dictionary<Tkey, TValue> dictionary) : base(dictionary) { }
Expand All @@ -21,7 +26,7 @@ public virtual XmlSchema GetSchema () {
}


public virtual void ReadXml (XmlReader reader) {
public void ReadXml (XmlReader reader) {
if (reader.IsEmptyElement)
return;

Expand Down Expand Up @@ -49,7 +54,7 @@ public virtual void ReadXml (XmlReader reader) {
}


public virtual void WriteXml (XmlWriter writer) {
public void WriteXml (XmlWriter writer) {
var keySerializer = new XmlSerializer(typeof(Tkey));
var valueSerializer = new XmlSerializer(typeof(TValue));

Expand All @@ -68,6 +73,19 @@ public virtual void WriteXml (XmlWriter writer) {
}
}


public void OnBeforeSerialize () {
map = this
.Select(item => (SerializableKeyValuePair<Tkey, TValue>)item)
.ToArray();
}


public void OnAfterDeserialize () {
foreach ((Tkey key, TValue value) in map)
Add(key, value);
}

}

}
2 changes: 1 addition & 1 deletion Runtime/SaveSystem.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private ISerializer SelectSerializer (SaveSystemSettings settings) {
serializer = new BinarySerializer();
break;
case SerializerType.JsonSerializer:
serializer = new JsonSerializer(settings.jsonSerializerSettings);
serializer = new JsonSerializer();
break;
case SerializerType.XmlSerializer:
serializer = new XmlSerializer();
Expand Down
18 changes: 1 addition & 17 deletions Runtime/SerializableData/DataBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,6 @@
namespace SaveSystemPackage.SerializableData {

[Serializable]
public class DataBatch<TData> : ISaveData where TData : ISaveData {

public Map<string, TData> batch = new();

public TData this [string key] => batch[key];


public void Add (string key, TData item) {
batch.Add(key, item);
}


public bool ContainsKey (string key) {
return batch.ContainsKey(key);
}

}
public class DataBatch<TData> : Map<string, TData>, ISaveData where TData : ISaveData { }

}
40 changes: 40 additions & 0 deletions Runtime/SerializableKeyValuePair.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace SaveSystemPackage {

[Serializable]
public struct SerializableKeyValuePair<TKey, TValue> {

[SerializeField]
private TKey key;

[SerializeField]
private TValue value;


public SerializableKeyValuePair (TKey key, TValue value) {
this.key = key;
this.value = value;
}


public static implicit operator KeyValuePair<TKey, TValue> (SerializableKeyValuePair<TKey, TValue> item) {
return new KeyValuePair<TKey, TValue>(item.key, item.value);
}


public static implicit operator SerializableKeyValuePair<TKey, TValue> (KeyValuePair<TKey, TValue> item) {
return new SerializableKeyValuePair<TKey, TValue>(item.Key, item.Value);
}


public void Deconstruct (out TKey key, out TValue value) {
key = this.key;
value = this.value;
}

}

}
3 changes: 3 additions & 0 deletions Runtime/SerializableKeyValuePair.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 4 additions & 40 deletions Runtime/Serialization/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,17 @@
using System.IO;
using System.Text;
using Unity.Plastic.Newtonsoft.Json;
using JsonSerializerSettings = SaveSystemPackage.Settings.JsonSerializerSettings;
using System.Text;
using UnityEngine;

namespace SaveSystemPackage.Serialization {

public class JsonSerializer : ISerializer {

private readonly Unity.Plastic.Newtonsoft.Json.JsonSerializer m_baseSerializer;


public JsonSerializer (Unity.Plastic.Newtonsoft.Json.JsonSerializerSettings settings) {
m_baseSerializer = new Unity.Plastic.Newtonsoft.Json.JsonSerializer {
Formatting = settings.Formatting,
DateFormatHandling = settings.DateFormatHandling,
DateTimeZoneHandling = settings.DateTimeZoneHandling,
DateParseHandling = settings.DateParseHandling,
FloatFormatHandling = settings.FloatFormatHandling,
FloatParseHandling = settings.FloatParseHandling,
StringEscapeHandling = settings.StringEscapeHandling,
ReferenceLoopHandling = settings.ReferenceLoopHandling
};
}


public JsonSerializer (JsonSerializerSettings settings) {
m_baseSerializer = new Unity.Plastic.Newtonsoft.Json.JsonSerializer {
Formatting = settings.formatting,
DateFormatHandling = settings.dateFormatHandling,
DateTimeZoneHandling = settings.dateTimeZoneHandling,
DateParseHandling = settings.dateParseHandling,
FloatFormatHandling = settings.floatFormatHandling,
FloatParseHandling = settings.floatParseHandling,
StringEscapeHandling = settings.stringEscapeHandling,
ReferenceLoopHandling = settings.referenceLoopHandling
};
}


public byte[] Serialize<TData> (TData data) where TData : ISaveData {
using var writer = new StringWriter();
m_baseSerializer.Serialize(writer, data);
return Encoding.UTF8.GetBytes(writer.ToString());
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(data, true));
}


public TData Deserialize<TData> (byte[] data) where TData : ISaveData {
using var reader = new StringReader(Encoding.UTF8.GetString(data));
return m_baseSerializer.Deserialize<TData>(new JsonTextReader(reader));
return JsonUtility.FromJson<TData>(Encoding.UTF8.GetString(data));
}


Expand Down
20 changes: 0 additions & 20 deletions Runtime/Settings/JsonSerializerSettings.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Runtime/Settings/JsonSerializerSettings.cs.meta

This file was deleted.

2 changes: 0 additions & 2 deletions Runtime/Settings/SaveSystemSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public class SaveSystemSettings : ScriptableObject, IDisposable {
public bool encrypt;
public EncryptionSettings encryptionSettings;

public JsonSerializerSettings jsonSerializerSettings;

[Min(0), Tooltip(Tooltips.CacheSize)]
public int cacheSize = 64;

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.dducode.save-system",
"version": "3.1.0",
"version": "3.1.1",
"displayName": "Save System",
"unity": "2021.3",
"description": "Use this save system to save and load your game objects",
Expand Down

0 comments on commit 9e742be

Please sign in to comment.