Skip to content

Instantly share code, notes, and snippets.

@ginxx009
Created February 27, 2018 01:40
Show Gist options
  • Save ginxx009/661258fa682f68eadd54547678b6fc48 to your computer and use it in GitHub Desktop.
Save ginxx009/661258fa682f68eadd54547678b6fc48 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.IO;
using LitJson;
public class StreetUtility
{
// Parsing 1 (Unity default parser).
public static string ToJson<T>(T requestObject)
{
return JsonUtility.ToJson(requestObject);
}
public static T FromJson<T>(string responseJson)
{
return JsonUtility.FromJson<T>(responseJson);
}
// Parsing 2 (LitJson).
public static string ToJson_mk2<T>(T requestObject)
{
return LitJson.JsonMapper.ToJson(requestObject);
}
public static T FromJson_mk2<T>(string responseJson)
{
return LitJson.JsonMapper.ToObject<T>(responseJson);
}
// SAVE 1.
public static void SaveJson(string json, string path)
{
using (FileStream fs = new FileStream(path, FileMode.Create))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(json);
}
}
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}
// SAVE 2.
public static bool SaveJson_mk2(string json, string path)
{
try
{
//#if UNITY_ANDROID
//PK Debug 12/12/2017 - 12/14/2017
//read for now
//WWW reader = new WWW(path);
//while (!reader.isDone) { }
//json = reader.text;
//Debug.Log("path : " + json);
//#elif UNITY_STANDALONE
StreamWriter writer = new StreamWriter(path, false);
writer.WriteLine(json);
writer.Close();
//#endif
}
catch (Exception e)
{
Debug.LogWarningFormat("(Failed to save) 저장 실패.\n{0}\n{1}\n{2}", e, path, json);
return false;
}
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
return true;
}
// Resources 1.
public static string LoadJsonFromResources(string path_without_extention_under_resources_folder)
{
TextAsset textAsset = Resources.Load<TextAsset>(path_without_extention_under_resources_folder);
return textAsset.text;
}
// Load 2 (StreamingAssets).
public static string LoadJsonFromStreamingAssets(string path_with_extention_under_streaming_assets_folder)
{
string json = null;
try
{
//Android Platform
#if UNITY_ANDROID
//string full_path = string.Format("{0}/{1}",Application.persistentDataPath, path_with_extention_under_streaming_assets_folder);
//StreamReader reader = new StreamReader(full_path);
//json = reader.ReadToEnd().Trim();
//reader.Close();
string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
StreamReader reader = new StreamReader(full_path);
json = reader.ReadToEnd().Trim();
reader.Close();
// PK Debug 2017.12.11
//Debug.Log(json);
//its already a json file so you don't need to convert
//JsonData itemData = JsonMapper.ToObject(json);
/*string full_path = System.IO.Path.Combine(Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
string result;
if (full_path.Contains("://") || full_path.Contains(":///"))
{
WWW www = new WWW(full_path);
while (!www.isDone) { }
result = www.text;
} else
{
result = System.IO.File.ReadAllText(full_path);
}
Debug.Log("Loaded file: " + result); */
#elif UNITY_IOS //IOS Platform
#elif UNITY_STANDALONE //PC Platform
string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
StreamReader reader = new StreamReader(full_path);
json = reader.ReadToEnd().Trim();
reader.Close();
Debug.Log(json);
#endif
}
catch (Exception e)
{
Debug.LogWarningFormat("Failed to Load.\n{0}\n{1}", e, path_with_extention_under_streaming_assets_folder);
}
return json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment