Skip to content

Instantly share code, notes, and snippets.

@halzate93
Created April 20, 2017 02:49
Show Gist options
  • Save halzate93/77e2011123b6af2541074e2a9edd5fc0 to your computer and use it in GitHub Desktop.
Save halzate93/77e2011123b6af2541074e2a9edd5fc0 to your computer and use it in GitHub Desktop.
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;
}
}
}
@GamerXWarrior
Copy link

Hey, can you please share the c# serializable classes' code also?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment