Repository navigation
Expand file tree
/
Copy pathJsonHelper.cs
More file actions
107 lines (100 loc) · 3.99 KB
/
Copy pathJsonHelper.cs
File metadata and controls
107 lines (100 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma warning disable 0649 // ignores warning: array "is never assigned to, and will always have its default value 'null'"
using UnityEngine;
using System;
using System.Text.RegularExpressions;
#if NETFX_CORE
using Windows.Data.Json;
using System.Collections.Generic;
using System.Linq;
#endif
namespace Unity3dAzure.AppServices
{
/// <summary>
/// Wrapper work-around for json array described on https://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/
/// </summary>
[Serializable]
internal class Wrapper<T>
{
public T[] array;
}
public class JsonHelper
{
/// <summary>
/// Work-around to parse json array
/// </summary>
public static T[] FromJsonArray<T>(string json)
{
// Work-around for JsonUtility array serialization issues in Windows Store Apps.
#if NETFX_CORE
JsonArray jsonArray = new JsonArray();
if (JsonArray.TryParse(json, out jsonArray))
{
return GetArray<T>(jsonArray);
}
Debug.LogWarning("Failed to parse json array of type:" + typeof(T).ToString() );
return default(T[]);
#endif
string newJson = "{\"array\":" + json + "}";
Wrapper<T> wrapper = new Wrapper<T>();
try
{
wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
}
catch (Exception e)
{
Debug.LogWarning("Failed to parse json array of type:" + typeof(T).ToString() + " Exception message: " + e.Message);
return default(T[]);
}
return wrapper.array;
}
public static N FromJsonNestedArray<T,N>(string json, string namedArray) where N : INestedResults<T>, new()
{
#if NETFX_CORE
JsonObject jsonObject = new JsonObject();
if (JsonObject.TryParse(json, out jsonObject))
{
JsonArray jsonArray = jsonObject.GetNamedArray(namedArray);
T[] array = GetArray<T>(jsonArray);
N nestedResults = new N(); //NestedResults<T> nestedResults = new NestedResults<T>(array);
nestedResults.SetArray(array);
string namedCount = nestedResults.GetCountField();
uint count = Convert.ToUInt32( jsonObject.GetNamedNumber(namedCount) );
nestedResults.SetCount(count);
return nestedResults;
} else {
Debug.LogWarning("Failed to parse json nested array of type:" + typeof(T).ToString());
return default(N);
}
#endif
N results = JsonUtility.FromJson<N>(json); // NestedResults<T> nestedResults = JsonUtility.FromJson<NestedResults<T>(json);
return results;
}
#if NETFX_CORE
private static T[] GetArray<T>(JsonArray array)
{
List<T> list = new List<T>();
foreach (var x in array)
{
try
{
T item = JsonUtility.FromJson<T>(x.ToString());
list.Add(item);
}
catch (Exception e)
{
Debug.LogWarning("Failed to parse json of type:" + typeof(T).ToString() + " Exception message: " + e.Message + " json:'" + x.ToString() + "'");
}
}
return list.ToArray();
}
#endif
/// <summary>
/// Workaround to only exclude Data Model's read only system properties being returned as json object. Unfortunately there is no JsonUtil attribute to do this as [NonSerialized] will just ignore the properties completely (both in and out).
/// </summary>
public static string ToJsonExcludingSystemProperties(object obj)
{
string jsonString = JsonUtility.ToJson(obj);
return Regex.Replace(jsonString, "(?i)(\\\"id\\\":\\\"\\\",)?(\\\"createdAt\\\":\\\"[0-9TZ:.-]*\\\",)?(\\\"updatedAt\\\":\\\"[0-9TZ:.-]*\\\",)?(\\\"version\\\":\\\"[A-Z0-9=]*\\\",)?(\\\"deleted\\\":(true|false),)?(\\\"ROW_NUMBER\\\":\\\"[0-9]*\\\",)?", "");
}
}
}