-
Notifications
You must be signed in to change notification settings - Fork 131
Reference Newtonsoft.Json.Utilities.AotHelper
Namespace | Newtonsoft.Json.Utilities |
---|---|
Assemblies | Newtonsoft.Json.dll |
Utility class to enforce ahead of time (AOT) compilation of types.
This class does not need to be added to a script that's added to a GameObject. It just needs to be compiled. Inheriting from UnityEngine.MonoBehaviour will ensure to always be compiled.
AotHelper.Ensure(Action action)
AotHelper.EnsureType<T>()
AotHelper.EnsureList<T>()
AotHelper.EnsureDictionary<TKey, TValue>()
Don't run action but let a compiler detect the code in action as an executable block.
Will not run action but the compiler will still compile the content of the action as if it would run.
All other Ensure- methods base off of this method.
Ensures the properties MinorRevision
and MajorRevision
are not stripped.
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
using UnityEngine;
public class EnsureExample : MonoBehaviour
{
private void Awake()
{
AotHelper.Ensure(() => {
var version = new Version(1, 2, 3, 4);
print(version.MinorRevision);
print(version.MajorRevision);
});
}
private void Start()
{
var version = new Version(1, 2, 3, 4);
print(version.Major);
print(version.Minor);
print(version.Build);
print(version.Revision);
var result = JsonConvert.SerializeObject(version, Formatting.Indented);
print(result);
}
}
Console output with above AotHelper.Ensure:
1
2
3
4
{
"Major": 1,
"Minor": 2,
"Build": 3,
"Revision": 4,
"MajorRevision": 0,
"MinorRevision": 4
}
Console output without above AotHelper.Ensure:
1
2
3
4
{
"Major": 1,
"Minor": 2,
"Build": 3,
"Revision": 4
}
Ensures that the default constructor is compiled for a given type.
Ensures that MyData<string>
can be constructed, because
new MyData<string>()
is never called explicitly.
Usage of EnsureType here fixes error:
JsonSerializationException: Unable to find a constructor to use for type
EnsureTypeExample+MyData`1[System.String]. A class should either have a
default constructor, one constructor with arguments or a constructor marked
with the JsonConstructor attribute. Path 'valueA', line 2, position 13.
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
using UnityEngine;
public class EnsureTypeExample : MonoBehaviour
{
private void Awake()
{
AotHelper.EnsureType<MyData<string>>();
}
private void Start()
{
var json = @"{
""valueA"": ""foo"",
""valueB"": ""bar"",
}";
var data = JsonConvert.DeserializeObject<MyData<string>>(json);
print(data.valueA);
print(data.valueB);
}
private class MyData<T>
{
public T valueA;
public T valueB;
}
}
Console output:
foo
bar
Ensure generic list type can be (de)serialized on AOT environment.
Ensures the following constructors:
System.Collections.Generic.List<T>()
System.Collections.Generic.HashSet<T>()
Newtonsoft.Json.Utilities.CollectionWrapper<T>(IList list)
Newtonsoft.Json.Utilities.CollectionWrapper<T>(ICollection<T> list)
Ensures the type decimal
can be used in lists in act of deserializing json.
Usage of EnsureList here fixes error:
ExecutionEngineException: Attempting to call method
'Newtonsoft.Json.Utilities.CollectionWrapper`1[[System.Decimal, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]::.ctor'
for which no ahead of time (AOT) code was generated.
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
using UnityEngine;
public class EnsureListExample : MonoBehaviour
{
private void Awake()
{
AotHelper.EnsureList<decimal>();
}
private void Start()
{
var json = @"{
""myList"": [
125251314,
35135.135,
12345.678
]
}";
var data = JsonConvert.DeserializeObject<MyData>(json);
print(data.myList[0]);
print(data.myList[1]);
print(data.myList[2]);
}
private class MyData
{
public IList<decimal> myList;
}
}
Console output:
125251314
35135.135
12345.678
Ensure generic dictionary type can be (de)serialized on AOT environment.
Ensures the following constructors:
System.Collections.Generic.Dictionary<TKey, TValue>()
Newtonsoft.Json.Utilities.DictionaryWrapper<TKey, TValue>(IDictionary dictionary)
Newtonsoft.Json.Utilities.DictionaryWrapper<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
Newtonsoft.Json.Serialization.DefaultContractResolver.EnumerableDictionaryWrapper<TKey, TValue>(IEnumerable<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> e)
Ensures the type dictionary of type <string, ulong>
can be used in lists in
act of deserializing json.
Usage of EnsureDictionary here fixes error:
JsonSerializationException: Unable to find a constructor to use for type
EnsureDictionaryExample+MyData. A class should either have a default
constructor, one constructor with arguments or a constructor marked with the
JsonConstructor attribute. Path 'myDictionary', line 2, position 19.
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;
using UnityEngine;
public class EnsureDictionaryExample : MonoBehaviour
{
private void Awake()
{
AotHelper.EnsureDictionary<string, ulong>();
}
private void Start()
{
var json = @"{
""myDictionary"": {
""tripp"": 351353153,
""trapp"": 123453151343678,
""trull"": 315136125234151314
}
}";
var data = JsonConvert.DeserializeObject<MyData>(json);
print("tripp: " + data.myDictionary["tripp"]);
print("trapp: " + data.myDictionary["trapp"]);
print("trull: " + data.myDictionary["trull"]);
}
private class MyData
{
public IDictionary<string, ulong> myDictionary;
}
}
Console output:
tripp: 351353153
trapp: 123453151343678
trull: 315136125234151314
new!
Unity's package is now officially ready for public use: Install official UPM package
This package is licensed under The MIT License (MIT)
Copyright © 2019 Kalle Jillheden (jilleJr)
https://github.com/jilleJr/Newtonsoft.Json
See full copyrights in LICENSE.md inside repository
About- Home
-
Install Newtonsoft.Json
Deprecated:
-
Install converters for Unity