Skip to content

Commit 91035fb

Browse files
committed
Adding PersistentJSON functionality
1 parent 9613873 commit 91035fb

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Programmer: Hunter Goodin
3+
* Date Created: 09/19/2022 @ 10:00 PM
4+
* Description: This class is for JSON objects that will persist on disk to the persistentDataPath even if the app is deleted.
5+
*/
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using UnityEngine;
11+
12+
namespace JSON
13+
{
14+
public class PersistentJSON : JSON
15+
{
16+
public PersistentJSON() : base() { }
17+
18+
public PersistentJSON(TextAsset jsonFile) : base(jsonFile) { }
19+
20+
/// <summary> Test </summary>
21+
public override void WriteToFile()
22+
{
23+
if (this.jsonFile == null)
24+
{
25+
Debug.LogError("ERROR: JSON TextAsset file never set!");
26+
return;
27+
}
28+
29+
File.WriteAllText(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json"), ToString());
30+
Debug.Log(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json"));
31+
}
32+
33+
/// <summary> If the TextAsset was given, them also write to the TextAsset </summary>
34+
public override void WriteToFile(TextAsset jsonFile)
35+
{
36+
if (this.jsonFile != null && this.jsonFile != jsonFile)
37+
{
38+
Debug.LogError($"ERROR: Object already has a JSON TextAsset file: '{this.jsonFile.name}.json'");
39+
return;
40+
}
41+
42+
base.WriteToFile(this.jsonFile);
43+
44+
File.WriteAllText(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json"), ToString());
45+
Debug.Log(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json"));
46+
}
47+
48+
protected override void ParseJSON(TextAsset jsonFile)
49+
{
50+
// If there is a persistent data path, read from that instead
51+
if (File.Exists(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json")))
52+
{
53+
File.ReadAllText(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json"));
54+
55+
boolList = new List<JSONBoolean>();
56+
intList = new List<JSONInteger>();
57+
floatList = new List<JSONFloat>();
58+
stringList = new List<JSONString>();
59+
this.jsonFile = jsonFile;
60+
61+
string[] lines = File.ReadAllText(Path.Combine(Application.persistentDataPath, $"{jsonFile.name}.json")).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
62+
lines[lines.Length - 2] = (lines[lines.Length - 2].EndsWith(",")) ? lines[lines.Length - 2] : lines[lines.Length - 2] + ",";
63+
64+
for (int i = 1; i < lines.Length - 1; i++)
65+
{
66+
string[] lineSplit = lines[i].Split(new string[] { ":" }, StringSplitOptions.None);
67+
lineSplit[0] = lineSplit[0].Replace(" \"", "");
68+
lineSplit[0] = lineSplit[0].Replace("\t", "");
69+
lineSplit[0] = lineSplit[0].Replace("\"", "");
70+
lineSplit[1] = lineSplit[1].Substring(1, lineSplit[1].Length - 2);
71+
72+
// If it's a boolean
73+
if (lineSplit[1] == "true" || lineSplit[1] == "false")
74+
{
75+
AddBool(lineSplit[0], Convert.ToBoolean(lineSplit[1]));
76+
}
77+
78+
// If it's an int
79+
if (int.TryParse(lineSplit[1], out int num))
80+
{
81+
AddInt(lineSplit[0], num);
82+
}
83+
84+
// If it's a float
85+
if (lineSplit[1].Contains(".") && float.TryParse(lineSplit[1], out float fl))
86+
{
87+
AddFloat(lineSplit[0], fl);
88+
}
89+
90+
// If it's a string
91+
if (lineSplit[1].StartsWith("\""))
92+
{
93+
lineSplit[1] = lineSplit[1].Replace("\"", "");
94+
AddString(lineSplit[0], lineSplit[1]);
95+
}
96+
}
97+
}
98+
else
99+
{
100+
base.ParseJSON(jsonFile);
101+
}
102+
}
103+
}
104+
}

JSONSerializerPackage/Assets/Code/PersistentJSON.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)