public T Load <T>(string contextName, Func <Stream, T> loader) { using (var stream = PostProcessorHost?.LoadContextInfo()) { if (stream == null) { return(default(T)); } var deserializer = new StreamDeserializer(stream); var seg = deserializer.ReadSegment(); var entries = deserializer.ReadDictionaryLazy(seg); if (!entries.TryGetValue(contextName, out Lazy <object> lazy)) { return(default(T)); } var bytes = (byte[])lazy.Value; return(loader(new MemoryStream(bytes))); } }
public void TestDictionaryLazy() { var ms = new MemoryStream(); var ss = new StreamSerializer(ms); var expected = new Dictionary <string, object> { ["a"] = 1, ["bcd"] = "efg", ["hijk"] = null, }; ss.Write(expected); ms.Position = 0; var sd = new StreamDeserializer(ms); var actual = sd.ReadDictionaryLazy(sd.ReadSegment()); Assert.Equal(expected["a"], actual["a"].Value); Assert.Equal(expected["bcd"], actual["bcd"].Value); Assert.Equal(expected["hijk"], actual["hijk"].Value); }
public static FileModel Deserialize(Stream stream, IFormatter formatter, Func <Stream, object> contentDeserializer, Func <Stream, IDictionary <string, object> > propertyDeserializer, Action <Stream, FileModel> otherDeserializer) { var sd = new StreamDeserializer(stream); var basicPropertiesSegment = sd.ReadSegment(); var basicProperties = sd.ReadDictionary(basicPropertiesSegment); var contentSegment = sd.ReadNext(basicPropertiesSegment); var content = contentDeserializer(sd.ReadBinaryAsStream(contentSegment)); var result = new FileModel( JsonUtility.Deserialize <FileAndType>(new StringReader((string)basicProperties[nameof(FileModel.FileAndType)])), content, JsonUtility.Deserialize <FileAndType>(new StringReader((string)basicProperties[nameof(FileModel.OriginalFileAndType)])), formatter); // Deserialize basic properties. result.LocalPathFromRoot = (string)basicProperties[nameof(FileModel.LocalPathFromRoot)]; result.LinkToFiles = ((object[])basicProperties[nameof(FileModel.LinkToFiles)]).OfType <string>().ToImmutableHashSet(); result.LinkToUids = ((object[])basicProperties[nameof(FileModel.LinkToUids)]).OfType <string>().ToImmutableHashSet(); result.FileLinkSources = JsonUtility.Deserialize <Dictionary <string, List <LinkSourceInfo> > >( new StringReader((string)basicProperties[nameof(FileModel.FileLinkSources)])) .ToImmutableDictionary( pair => pair.Key, pair => pair.Value.ToImmutableList()); result.UidLinkSources = JsonUtility.Deserialize <Dictionary <string, List <LinkSourceInfo> > >( new StringReader((string)basicProperties[nameof(FileModel.UidLinkSources)])) .ToImmutableDictionary( pair => pair.Key, pair => pair.Value.ToImmutableList()); result.Uids = JsonUtility.Deserialize <List <UidDefinition> >( new StringReader((string)basicProperties[nameof(FileModel.Uids)])).ToImmutableArray(); var manifestProperties = (IDictionary <string, object>)result.ManifestProperties; foreach (var pair in JsonUtility.Deserialize <Dictionary <string, object> >( new StringReader((string)basicProperties[nameof(FileModel.ManifestProperties)]))) { manifestProperties[pair.Key] = pair.Value; } // Deserialize properties. var propertySegment = sd.ReadNext(contentSegment); var properties = (IDictionary <string, object>)result.Properties; if (propertyDeserializer != null && propertySegment.ContentType == StreamSegmentType.Binary) { var dictionary = propertyDeserializer(sd.ReadBinaryAsStream(propertySegment)); foreach (var pair in dictionary) { properties[pair.Key] = pair.Value; } } // Deserialize others. var otherSegment = sd.ReadNext(propertySegment); if (otherDeserializer != null && otherSegment.ContentType == StreamSegmentType.Binary) { otherDeserializer(sd.ReadBinaryAsStream(otherSegment), result); } return(result); }