-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathJsonUtility.bindings.cs
63 lines (48 loc) · 2.34 KB
/
JsonUtility.bindings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine.Bindings;
namespace UnityEngine
{
[NativeHeader("Modules/JSONSerialize/Public/JsonUtility.bindings.h")]
public static class JsonUtility
{
[FreeFunction("ToJsonInternal", true)]
[ThreadSafe]
private static extern string ToJsonInternal([NotNull] object obj, bool prettyPrint);
[FreeFunction("FromJsonInternal", true, ThrowsException = true)]
[ThreadSafe]
private static extern object FromJsonInternal(string json, object objectToOverwrite, Type type);
public static string ToJson(object obj) { return ToJson(obj, false); }
public static string ToJson(object obj, bool prettyPrint)
{
if (obj == null)
return "";
if (obj is UnityEngine.Object && !(obj is MonoBehaviour || obj is ScriptableObject))
throw new ArgumentException("JsonUtility.ToJson does not support engine types.");
return ToJsonInternal(obj, prettyPrint);
}
public static T FromJson<T>(string json) { return (T)FromJson(json, typeof(T)); }
public static object FromJson(string json, Type type)
{
if (string.IsNullOrEmpty(json))
return null;
if (type == null)
throw new ArgumentNullException("type");
if (type.IsAbstract || type.IsSubclassOf(typeof(UnityEngine.Object)))
throw new ArgumentException("Cannot deserialize JSON to new instances of type '" + type.Name + ".'");
return FromJsonInternal(json, null, type);
}
public static void FromJsonOverwrite(string json, object objectToOverwrite)
{
if (string.IsNullOrEmpty(json))
return;
if (objectToOverwrite == null)
throw new ArgumentNullException("objectToOverwrite");
if (objectToOverwrite is UnityEngine.Object && !(objectToOverwrite is MonoBehaviour || objectToOverwrite is ScriptableObject))
throw new ArgumentException("Engine types cannot be overwritten from JSON outside of the Editor.");
FromJsonInternal(json, objectToOverwrite, objectToOverwrite.GetType());
}
}
}