static bool TrySerializeAdapter <TValue>(IJsonAdapter adapter, JsonWriter writer, ref TValue value) { if (adapter is IJsonAdapter <TValue> typed) { using (var buffer = new JsonStringBuffer(16, Allocator.TempJob)) { typed.Serialize(buffer, value); unsafe { writer.AsUnsafe().WriteValueLiteral(buffer.GetUnsafePtr(), buffer.Length); } } return(true); } if (adapter is Adapters.Contravariant.IJsonAdapter <TValue> typedContravariant) { using (var buffer = new JsonStringBuffer(16, Allocator.TempJob)) { typedContravariant.Serialize(buffer, value); unsafe { writer.AsUnsafe().WriteValueLiteral(buffer.GetUnsafePtr(), buffer.Length); } } return(true); } return(false); }
public static UnityObject FromObjectHandle(SerializedObjectView objectView) { var container = new Container(); objectView.TryGetValueAsString("Guid", out var guid); objectView.TryGetValueAsInt64("FileId", out var fileId); objectView.TryGetValueAsInt64("Type", out var type); if (guid == s_EmptyGuid || guid == string.Empty) { return(null); } using (var writer = new JsonStringBuffer(256, Allocator.Temp)) { writer.Write("{\"o\":{"); writer.Write("\"fileID\":"); writer.Write(fileId); writer.Write(",\"guid\":\""); writer.Write(guid); writer.Write("\",\"type\": "); writer.Write(type); writer.Write("}}"); var json = writer.ToString(); UnityEditor.EditorJsonUtility.FromJsonOverwrite(json, container); return(container.o); } }
void Contravariant.IJsonAdapter <UnityObject> .Serialize(JsonStringBuffer writer, UnityObject value) { #if UNITY_EDITOR var id = UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(value).ToString(); writer.WriteEncodedJsonString(id); #else writer.Write("null"); #endif }
public void Serialize(JsonStringBuffer writer, BuildPipelineBase value) { string json = null; if (value != null) { json = value.GetType().GetAssemblyQualifiedTypeName(); } writer.WriteEncodedJsonString(json); }
public void Serialize(JsonStringBuffer writer, RunStep value) { string json = null; if (value != null) { json = value.GetType().GetQualifedAssemblyTypeName(); } writer.WriteEncodedJsonString(json); }
public void Serialize(JsonStringBuffer writer, Platform value) { string json = null; if (value != null) { json = value.Name; } writer.WriteEncodedJsonString(json); }
void IJsonAdapter <DirectoryInfo> .Serialize(JsonStringBuffer writer, DirectoryInfo value) { if (null == value) { writer.Write("null"); } else { writer.WriteEncodedJsonString(value.GetRelativePath()); } }
public void Write_WhenValueIsFloat_ValueIsCorrectlyWritten(float value) { using (var writer = new JsonStringBuffer(16, Allocator.Temp)) { writer.Write(value); var str = writer.ToString(); Assert.That(str, Is.EqualTo(value.ToString(CultureInfo.InvariantCulture))); } }
public void ToJson_StructWithInt32Property_DoesNotAllocate() { var container = new StructWithInt32Property(); GCAllocTest.Method(() => { using (var writer = new JsonStringBuffer(16, Allocator.Temp)) { JsonSerialization.ToJson(writer, container); } }) .ExpectedCount(0) .Warmup() .Run(); }
static bool TrySerializeAdapter <TValue>(IJsonAdapter adapter, JsonStringBuffer writer, ref TValue value) { if (adapter is IJsonAdapter <TValue> typed) { typed.Serialize(writer, value); return(true); } if (adapter is Adapters.Contravariant.IJsonAdapter <TValue> typedContravariant) { typedContravariant.Serialize(writer, value); return(true); } return(false); }
public void Serialize(JsonStringBuffer writer, IBuildStep value) { string json = null; if (value != null) { if (value is BuildPipeline pipeline) { json = GlobalObjectId.GetGlobalObjectIdSlow(pipeline).ToString(); } else { json = value.GetType().GetQualifedAssemblyTypeName(); } } writer.WriteEncodedJsonString(json); }
static bool TrySerializeLazyLoadReference <TValue>(JsonStringBuffer writer, ref TValue value) { if (!RuntimeTypeInfoCache <TValue> .IsLazyLoadReference) { return(false); } var instanceID = PropertyContainer.GetValue <TValue, int>(ref value, k_LazyLoadReference_InstanceID); #if UNITY_2020_1_OR_NEWER writer.WriteEncodedJsonString(UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(instanceID).ToString()); #else var asset = UnityEditor.EditorUtility.InstanceIDToObject(instanceID); // Here if asset is null and false, and instanceID is NOT zero, it means we have an unloaded asset // that still has a valid pptr but we are going to serialize it as null and lose the reference. // Only way to fix this would be to backport GlobalObjectId.GetGlobalObjectIdSlow(instanceID). writer.WriteEncodedJsonString(UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(asset).ToString()); #endif return(true); }
public bool TrySerialize <TValue>(JsonStringBuffer writer, ref TValue value) { if (null != UserDefined && UserDefined.Count > 0) { foreach (var adapter in UserDefined) { if (TrySerializeAdapter(adapter, writer, ref value)) { return(true); } } } if (null != Global && Global.Count > 0) { foreach (var adapter in Global) { if (TrySerializeAdapter(adapter, writer, ref value)) { return(true); } } } if (TrySerializeAdapter(InternalAdapter, writer, ref value)) { return(true); } #if UNITY_EDITOR if (TrySerializeLazyLoadReference(writer, ref value)) { return(true); } #endif return(false); }
/// <summary> /// Alternative method to JsonSerialization.ToJson which uses JsonSceneWriter /// Use this if you need to support `ISerializationCallbacks` /// </summary> /// <param name="value">The value to serialize</param> /// <param name="metadata">SerializationMetadata for this call</param> /// <typeparam name="T">The type of the value being serialized</typeparam> /// <returns>A string containing the Json serialized representation of `value`</returns> public static string ToJson <T>(T value, SerializationMetadata metadata = null) { var parameters = new JsonSerializationParameters { DisableRootAdapters = true, DisableSerializedReferences = true }; using (var writer = new JsonStringBuffer(parameters.InitialCapacity, Allocator.Temp)) { var container = new PropertyWrapper <T>(value); var visitor = new JsonSceneWriter(metadata); visitor.SetStringWriter(writer); visitor.SetSerializedType(parameters.SerializedType); visitor.SetMinified(parameters.Minified); visitor.SetSimplified(parameters.Simplified); using (visitor.Lock()) PropertyContainer.Visit(ref container, visitor); return(writer.ToString()); } }
void IJsonAdapter <UnityEditor.GlobalObjectId> .Serialize(JsonStringBuffer writer, UnityEditor.GlobalObjectId value) => writer.WriteEncodedJsonString(value.ToString());
void IJsonAdapter <string> .Serialize(JsonStringBuffer writer, string value) => writer.WriteEncodedJsonString(value);
void IJsonAdapter <char> .Serialize(JsonStringBuffer writer, char value) => writer.WriteEncodedJsonString(value);
void IJsonAdapter <bool> .Serialize(JsonStringBuffer writer, bool value) => writer.Write(value ? "true" : "false");
void IJsonAdapter <double> .Serialize(JsonStringBuffer writer, double value) => writer.Write(value);
void IJsonAdapter <float> .Serialize(JsonStringBuffer writer, float value) => writer.Write(value);
void IJsonAdapter <DateTime> .Serialize(JsonStringBuffer writer, DateTime value) => writer.WriteEncodedJsonString(value.ToString("o", CultureInfo.InvariantCulture));
void IJsonAdapter <uint> .Serialize(JsonStringBuffer writer, uint value) => writer.Write(value);
void IJsonAdapter <byte> .Serialize(JsonStringBuffer writer, byte value) => writer.Write(value);
void IJsonAdapter <short> .Serialize(JsonStringBuffer writer, short value) => writer.Write(value);
void IJsonAdapter <UnityEditor.GUID> .Serialize(JsonStringBuffer writer, UnityEditor.GUID value) => writer.WriteEncodedJsonString(value.ToString());
void IJsonAdapter <Guid> .Serialize(JsonStringBuffer writer, Guid value) => writer.WriteEncodedJsonString(value.ToString("N", CultureInfo.InvariantCulture));
public void Serialize(JsonStringBuffer writer, Version value) => writer.WriteEncodedJsonString(value.ToString());
void IJsonAdapter <ulong> .Serialize(JsonStringBuffer writer, ulong value) => writer.Write(value);
public void Serialize(JsonStringBuffer writer, BuildTarget value) { writer.WriteEncodedJsonString(value?.BeeTargetName); }
void IJsonAdapter <TimeSpan> .Serialize(JsonStringBuffer writer, TimeSpan value) => writer.WriteEncodedJsonString(value.ToString("c", CultureInfo.InvariantCulture));