62 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			C#
		
	
	
| 
 | |
| using System;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace Guru
 | |
| {
 | |
|     using Newtonsoft.Json;
 | |
| 
 | |
|     
 | |
|     
 | |
|     public static class JsonDataHelper
 | |
|     {
 | |
|         public static Action<Exception> ExceptionHandler;
 | |
| 
 | |
|         
 | |
|         public static string ToJsonString(object obj)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 return JsonConvert.SerializeObject(obj);
 | |
|             }
 | |
|             catch (Exception e)
 | |
|             {
 | |
|                 ExceptionHandler?.Invoke(e);
 | |
|                 Debug.LogError(e);
 | |
|             }
 | |
|             return "";
 | |
|         }
 | |
| 
 | |
| 
 | |
|         public static bool Parse<T>(string json, out T result)
 | |
|         {
 | |
|             bool success = false;
 | |
|             result = default(T);
 | |
|             
 | |
|             if(string.IsNullOrEmpty(json)) return false;
 | |
|             
 | |
|             try
 | |
|             {
 | |
|                 result =  JsonConvert.DeserializeObject<T>(json);
 | |
|                 success = true;
 | |
|             }
 | |
|             catch (Exception e)
 | |
|             {
 | |
|                 ExceptionHandler?.Invoke(e);
 | |
|                 Debug.LogError(e);
 | |
|             }
 | |
| 
 | |
|             return success;
 | |
|         }
 | |
| 
 | |
| 
 | |
| 
 | |
|         public static string ToJson(this IJsonData  obj)
 | |
|         {
 | |
|             return ToJsonString((object)obj);
 | |
|         }
 | |
| 
 | |
|         
 | |
| 
 | |
|     }
 | |
| } |