Skip to content

Instantly share code, notes, and snippets.

@su10
Last active June 6, 2018 15:01
Show Gist options
  • Save su10/d76168c7d86d0dd420517571b7c33fdb to your computer and use it in GitHub Desktop.
Save su10/d76168c7d86d0dd420517571b7c33fdb to your computer and use it in GitHub Desktop.
Utf8Jsonでキーを網羅していないクラスで長いJSONをデシリアライズしようとするとNullReferenceExceptionになる例
{
"success": 0,
"data": {
"code": 10000
}
}
{
"success": 1,
"data": {
"transactions": [
{
"transaction_id": 12189864,
"side": "buy",
"price": "841708",
"amount": "0.0020",
"executed_at": 1528295539542
},
{
"transaction_id": 12189863,
"side": "buy",
"price": "841707",
"amount": "0.0080",
"executed_at": 1528295539500
}
]
}
}
using UniRx;
using UnityEditor;
using UnityEngine;
using Utf8Json;
public class ParseBigJsonWithUtf8Json : MonoBehaviour
{
public void ParseSmallJson()
{
ObservableWWW.Get("https://public.bitbank.cc/btc_jpy/transactions")
.Do(Debug.Log)
.Select(JsonSerializer.Deserialize<Response>)
.Subscribe(response => Debug.Log(JsonSerializer.ToJsonString(response)));
}
public void ParseBigJson()
{
ObservableWWW.Get("https://public.bitbank.cc/btc_jpy/transactions/20180603")
.Do(Debug.Log)
.Select(JsonSerializer.Deserialize<Response>)
.Subscribe(response => Debug.Log(JsonSerializer.ToJsonString(response)));
}
}
// JSONに含まれるdataを持たないクラス
public class Response
{
public readonly int success; // 0 or 1
public Response(int success)
{
this.success = success;
}
}
// インスペクタにボタンを表示する用
[CustomEditor(typeof(ParseBigJsonWithUtf8Json))]
public class ParseBigJsonWithUtf8JsonEditor : Editor
{
private ParseBigJsonWithUtf8Json monoBehaviour => (ParseBigJsonWithUtf8Json) target;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Parse Small JSON"))
{
monoBehaviour.ParseSmallJson();
}
if (GUILayout.Button("Parse Big JSON"))
{
monoBehaviour.ParseBigJson();
}
}
}
NullReferenceException: Object reference not set to an instance of an object
UniRx.Stubs.<Throw>m__1 (System.Exception ex) (at Assets/UniRx/Plugins/Observer.cs:495)
UniRx.Observer+Subscribe`1[T].OnError (System.Exception error) (at Assets/UniRx/Plugins/Observer.cs:172)
UniRx.Operators.SelectObservable`2+Select[T,TR].OnNext (T value) (at Assets/UniRx/Plugins/Operators/Select.cs:88)
UniRx.Operators.DoObservable`1+Do[T].OnNext (T value) (at Assets/UniRx/Plugins/Operators/Do.cs:56)
UniRx.Operators.FromCoroutineObservable`1+FromCoroutine[T].OnNext (T value) (at Assets/UniRx/Plugins/UnityEngineBridge/Operators/FromCoroutine.cs:49)
UniRx.ObservableWWW+<FetchText>c__Iterator1.MoveNext () (at Assets/UniRx/Plugins/UnityEngineBridge/ObservableWWW.cs:265)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment