Created
April 20, 2017 02:49
-
-
Save halzate93/77e2011123b6af2541074e2a9edd5fc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
namespace UnityRest | |
{ | |
public static class JsonHelper | |
{ | |
public static T[] FromJson<T>(string jsonArray) | |
{ | |
jsonArray = WrapArray (jsonArray); | |
return FromJsonWrapped<T> (jsonArray); | |
} | |
public static T[] FromJsonWrapped<T> (string jsonObject) | |
{ | |
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(jsonObject); | |
return wrapper.items; | |
} | |
private static string WrapArray (string jsonArray) | |
{ | |
return "{ \"items\": " + jsonArray + "}"; | |
} | |
public static string ToJson<T>(T[] array) | |
{ | |
Wrapper<T> wrapper = new Wrapper<T>(); | |
wrapper.items = array; | |
return JsonUtility.ToJson(wrapper); | |
} | |
public static string ToJson<T>(T[] array, bool prettyPrint) | |
{ | |
Wrapper<T> wrapper = new Wrapper<T>(); | |
wrapper.items = array; | |
return JsonUtility.ToJson(wrapper, prettyPrint); | |
} | |
[Serializable] | |
private class Wrapper<T> | |
{ | |
public T[] items; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, can you please share the c# serializable classes' code also?