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 JsonWriter(256, Allocator.Temp)) { using (writer.WriteObjectScope()) { using (writer.WriteObjectScope("o")) { writer.WriteKeyValue("fileID", fileId); writer.WriteKeyValue("guid", guid); writer.WriteKeyValue("type", type); } } var json = writer.ToString(); UnityEditor.EditorJsonUtility.FromJsonOverwrite(json, container); return(container.o); } }
/// <summary> /// Initializes a new instance of the <see cref="JsonMigrationContext"/> structure. This is an internal method. /// </summary> /// <param name="serializedVersion">The serialized version read from the stream.</param> /// <param name="serializedObject">The view over the serialized data.</param> /// <param name="serializedType">The serialized type from the stream.</param> /// <param name="visitor">The current deserialization visitor, used for re-entry into normal deserialization.</param> internal JsonMigrationContext(int serializedVersion, SerializedObjectView serializedObject, Type serializedType, JsonPropertyReader visitor) { SerializedVersion = serializedVersion; SerializedObject = serializedObject; SerializedType = serializedType; m_Visitor = visitor; }
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); } }
/** * Deserialize an object view into the object buffer. */ JsonKeyHandle DeserializeToObjectBuffer(SerializedObjectView objectView) { var newObjMap = new UnsafeHashMap <FixedString128, JsonKeyHandle>(50, m_Allocator); ReadObjectViewIntoMap(objectView, ref newObjMap); m_ObjectBuffer.Add(newObjMap); return(new JsonKeyHandle(ref this, JsonValueType.Object, m_ObjectBuffer.Length - 1)); }
/// <summary> /// Reads the specified <see cref="SerializedValueView"/> as the specified <typeparamref name="TValue"/> type. /// </summary> /// <param name="view">The view to read.</param> /// <param name="value">When this method returns, contains the deserialized value, if successful. otherwise the default value for the <typeparamref name="TValue"/>.</param> /// <typeparam name="TValue">The value type.</typeparam> /// <returns><see langword="true"/> if the value was read successfully; otherwise, <see langword="false"/></returns> public bool TryRead <TValue>(SerializedObjectView view, out TValue value) { try { value = Read <TValue>(view); return(true); } catch { value = default; return(false); } }
private static SerializedObjectReader ReadSceneFile(string path, out SerializedObjectView headerInformations) { var config = new SerializedObjectReaderConfiguration { UseReadAsync = false, BlockBufferSize = 256 << 10, // 256 KB TokenBufferSize = 1024, NodeBufferSize = JsonFrontEnd.EntityBatchSize, ValidationType = JsonValidationType.Standard, OutputBufferSize = 1024 << 10 // 1 MB }; var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, config.BlockBufferSize); var reader = new SerializedObjectReader(stream, config, leaveInputOpen: false); try { if (reader.Step(out var root) != NodeType.BeginObject) { throw new Exception(); } // ASSUMPTION: All members are written BEFORE the blob of entities. // Read until we hit "Entities": NodeType node; while ((node = reader.Step(out var view)) != NodeType.None) { if ((node & NodeType.ObjectKey) == 0) { continue; } if (view.AsStringView().Equals("Entities")) { break; } } // Unpack header information from whats been read so far. headerInformations = root.AsObjectView(); return(reader); } catch { reader.Dispose(); throw; } }
/** * Deserialize each field in an object */ void ReadObjectViewIntoMap(SerializedObjectView objView, ref UnsafeHashMap <FixedString128, JsonKeyHandle> objMap) { var serializedViewEnum = objView.GetEnumerator(); while (serializedViewEnum.MoveNext()) { var view = serializedViewEnum.Current; var typeInfo = DeserializeValueView(view.Value()); FixedString64 fs = default; fs.Append(view.Name().ToString()); objMap.TryAdd(fs, typeInfo); } serializedViewEnum.Dispose(); }