|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using UnityEngine; |
| 5 | +using System; |
| 6 | +using System.Text.RegularExpressions; |
| 7 | +#if NETFX_CORE |
| 8 | +using Windows.Data.Json; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +#endif |
| 12 | + |
| 13 | +namespace RESTClient { |
| 14 | + /// <summary> |
| 15 | + /// Wrapper work-around for json array described on https://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/ |
| 16 | + /// </summary> |
| 17 | +#pragma warning disable 0649 // suppresses warning: array "is never assigned to, and will always have its default value 'null'" |
| 18 | + [Serializable] |
| 19 | + internal class Wrapper<T> { |
| 20 | + |
| 21 | + public T[] array; |
| 22 | + } |
| 23 | + |
| 24 | + public static class JsonHelper { |
| 25 | + /// <summary> |
| 26 | + /// Work-around to parse json array |
| 27 | + /// </summary> |
| 28 | + public static T[] FromJsonArray<T>(string json) { |
| 29 | + // Work-around for JsonUtility array serialization issues in Windows Store Apps. |
| 30 | +#if NETFX_CORE |
| 31 | + JsonArray jsonArray = new JsonArray(); |
| 32 | + if (JsonArray.TryParse(json, out jsonArray)) { |
| 33 | + return GetArray<T>(jsonArray); |
| 34 | + } |
| 35 | + Debug.LogWarning("Failed to parse json array of type:" + typeof(T).ToString() ); |
| 36 | + return default(T[]); |
| 37 | +#endif |
| 38 | + string newJson = "{\"array\":" + json + "}"; |
| 39 | + Wrapper<T> wrapper = new Wrapper<T>(); |
| 40 | + try { |
| 41 | + wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson); |
| 42 | + } catch (Exception e) { |
| 43 | + Debug.LogWarning("Failed to parse json array of type:" + typeof(T).ToString() + " Exception message: " + e.Message); |
| 44 | + return default(T[]); |
| 45 | + } |
| 46 | + return wrapper.array; |
| 47 | + } |
| 48 | + |
| 49 | + public static N FromJsonNestedArray<T, N>(string json, string namedArray) where N : INestedResults<T>, new() { |
| 50 | +#if NETFX_CORE |
| 51 | + JsonObject jsonObject = new JsonObject(); |
| 52 | + if (JsonObject.TryParse(json, out jsonObject)) { |
| 53 | + JsonArray jsonArray = jsonObject.GetNamedArray(namedArray); |
| 54 | + T[] array = GetArray<T>(jsonArray); |
| 55 | + N nestedResults = new N(); //NestedResults<T> nestedResults = new NestedResults<T>(array); |
| 56 | + nestedResults.SetArray(array); |
| 57 | + |
| 58 | + string namedCount = nestedResults.GetCountField(); |
| 59 | + uint count = Convert.ToUInt32( jsonObject.GetNamedNumber(namedCount) ); |
| 60 | + nestedResults.SetCount(count); |
| 61 | + |
| 62 | + return nestedResults; |
| 63 | + } else { |
| 64 | + Debug.LogWarning("Failed to parse json nested array of type:" + typeof(T).ToString()); |
| 65 | + return default(N); |
| 66 | + } |
| 67 | +#endif |
| 68 | + N results = JsonUtility.FromJson<N>(json); // NestedResults<T> nestedResults = JsonUtility.FromJson<NestedResults<T>(json); |
| 69 | + return results; |
| 70 | + } |
| 71 | + |
| 72 | +#if NETFX_CORE |
| 73 | + private static T[] GetArray<T>(JsonArray array) |
| 74 | + { |
| 75 | + List<T> list = new List<T>(); |
| 76 | + foreach (var x in array) { |
| 77 | + try { |
| 78 | + T item = JsonUtility.FromJson<T>(x.ToString()); |
| 79 | + list.Add(item); |
| 80 | + } catch (Exception e) { |
| 81 | + Debug.LogWarning("Failed to parse json of type:" + typeof(T).ToString() + " Exception message: " + e.Message + " json:'" + x.ToString() + "'"); |
| 82 | + } |
| 83 | + } |
| 84 | + return list.ToArray(); |
| 85 | + } |
| 86 | +#endif |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// 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). |
| 90 | + /// </summary> |
| 91 | + public static string ToJsonExcludingSystemProperties(object obj) { |
| 92 | + string jsonString = JsonUtility.ToJson(obj); |
| 93 | + return Regex.Replace(jsonString, "(?i)(\\\"id\\\":\\\"\\\",)?(\\\"createdAt\\\":\\\"[0-9TZ:.-]*\\\",)?(\\\"updatedAt\\\":\\\"[0-9TZ:.-]*\\\",)?(\\\"version\\\":\\\"[A-Z0-9=]*\\\",)?(\\\"deleted\\\":(true|false),)?(\\\"ROW_NUMBER\\\":\\\"[0-9]*\\\",)?", ""); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments