using System.Collections.Generic; using System.Security; namespace SimpleFirebaseUnity { using MiniJSON; public class DataSnapshot { protected object val_obj; protected Dictionary val_dict; protected List keys; protected string json; protected DataSnapshot() { val_dict = null; val_obj = null; keys = null; json = null; } /// /// Creates snapshot from Json string /// /// Json string public DataSnapshot(string _json = "") { object obj = (_json != null && _json.Length > 0)?Json.Deserialize(_json):null; if (obj is Dictionary) val_dict = obj as Dictionary; else val_obj = obj; keys = null; json = _json; } /// /// If snapshot is a Dictionary, returns keys of the snapshot , else null /// public List Keys { get { if (keys == null && val_dict != null) keys = new List(val_dict.Keys); return keys; } } /// /// If snapshot is a Dictionary, gives the first key founded on snapshot, else null /// public string FirstKey { get { return (val_dict == null) ? null : Keys[0]; } } /// /// Raw json of snapshot /// public string RawJson { get { return json; } } /// /// Raw value of snapshot /// public object RawValue { get { return (val_dict == null) ? val_obj : val_dict; } } /// /// Gets value from snapshot /// /// Desired type /// Value of snapshot as the defined type, null if typecasting failed [SecuritySafeCritical] public T Value() { try { if (val_obj != null) return (T)val_obj; object obj = val_dict; return (T)obj; } catch { return default(T); } } } }