Create a gist now

Instantly share code, notes, and snippets.

@asus4 /JsonNode.cs
Last active Nov 10, 2016

What would you like to do?
Simple json accessibility for Unity.
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// simple access to json
///
/// var json = JsonNode.Parse(jsonString);
/// string foo = json["hoge"][4].Get<string>();
/// </summary>
public class JsonNode : IEnumerable<JsonNode>, IDisposable
{
object obj;
public JsonNode(object obj)
{
this.obj = obj;
}
public void Dispose()
{
obj = null;
}
public static JsonNode Parse(string json)
{
return new JsonNode(MiniJSON.Json.Deserialize(json));
}
public JsonNode this [int i]
{
get
{
if (obj is IList)
{
return new JsonNode(((IList)obj)[i]);
}
throw new Exception("Object is not IList : " + obj.GetType().ToString());
}
}
public JsonNode this [string key]
{
get
{
if (obj is IDictionary)
{
return new JsonNode(((IDictionary)obj)[key]);
}
throw new Exception("Object is not IDictionary : " + obj.GetType().ToString());
}
}
public int Count
{
get
{
if (obj is IList)
{
return ((IList)obj).Count;
}
else if (obj is IDictionary)
{
return ((IDictionary)obj).Count;
}
else
{
return 0;
}
}
}
public T Get<T>()
{
return (T)obj;
}
public IEnumerator<JsonNode> GetEnumerator()
{
if (obj is IList)
{
foreach (var o in (IList)obj)
{
yield return new JsonNode(o);
}
}
else if (obj is IDictionary)
{
foreach (var o in (IDictionary)obj)
{
yield return new JsonNode(o);
}
}
else
{
yield return null;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
Owner

asus4 commented Jan 22, 2015

Usage

    JsonNode json = JsonNode.Parse(jsonString);
    string foo = json["hoge"][4].Get<string>();
    long boo = json["hoge"]["boo"].Get<long>();

giteen commented Feb 25, 2015

Forkさせて頂いたんですがGistでPullRequestの仕方が分かりません。諦めました、ごめんなさい。
ちなみにDeserialize機能を追加したくてForkしました。
こんな感じで使おうと思っています。
使っても問題ないでしょうか?

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