Last active
June 6, 2018 15:01
-
-
Save su10/d76168c7d86d0dd420517571b7c33fdb to your computer and use it in GitHub Desktop.
Utf8Jsonでキーを網羅していないクラスで長いJSONをデシリアライズしようとするとNullReferenceExceptionになる例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"success": 0, | |
"data": { | |
"code": 10000 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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 | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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