UnityEssentials
Small but useful tools and features for Unity
SD_JsonSerializer.cs
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020 Bronson Zgeb
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 
24 // Significant parts of the code are extracted from this GitHub repository: https://github.com/BayatGames/SaveGameFree and https://github.com/UnityTechnologies/UniteNow20-Persistent-Data
25 
26 
27 using System;
28 using System.IO;
29 using FullSerializer;
30 using System.Text;
31 using UnityEngine;
32 
33 namespace Essentials.SaveData
34 {
38  public class SD_JsonSerializer
39  {
40 
48  public void Serialize<T>(T obj, Stream stream, Encoding encoding)
49  {
50  #if !UNITY_WSA || !UNITY_WINRT
51  try
52  {
53  StreamWriter writer = new StreamWriter(stream, encoding);
54  fsSerializer serializer = new fsSerializer();
55  fsData data = new fsData();
56  serializer.TrySerialize(obj, out data);
57  writer.Write(fsJsonPrinter.CompressedJson(data));
58  writer.Dispose();
59  }
60  catch (Exception ex)
61  {
62  Debug.LogException(ex);
63  }
64  #else
65  StreamWriter writer = new StreamWriter ( stream, encoding );
66  writer.Write ( JsonUtility.ToJson ( obj ) );
67  writer.Dispose ();
68  #endif
69  }
70 
77  public T Deserialize<T>(Stream stream, Encoding encoding)
78  {
79  T result = default(T);
80  #if !UNITY_WSA || !UNITY_WINRT
81  try
82  {
83  StreamReader reader = new StreamReader(stream, encoding);
84  fsSerializer serializer = new fsSerializer();
85  fsData data = fsJsonParser.Parse(reader.ReadToEnd());
86  serializer.TryDeserialize(data, ref result);
87  if (result == null)
88  {
89  result = default(T);
90  }
91  reader.Dispose();
92  }
93  catch (Exception ex)
94  {
95  Debug.LogException(ex);
96  }
97  #else
98  StreamReader reader = new StreamReader ( stream, encoding );
99  result = JsonUtility.FromJson<T> ( reader.ReadToEnd () );
100  reader.Dispose ();
101  #endif
102  return result;
103  }
104 
105  }
106 
107 }
Json Serializer used by the SaveData class.
void Serialize< T >(T obj, Stream stream, Encoding encoding)
Serialize the specified object to stream with encoding.
T Deserialize< T >(Stream stream, Encoding encoding)
Deserialize the specified object from stream using the encoding.