コード例 #1
0
        public void TestNest()
        {
            var ms       = new MemoryStream();
            var ss       = new StreamSerializer(ms);
            var expected = new Dictionary <string, object>
            {
                ["a"] = new object[]
                {
                    "a1",
                    "a2",
                    new Dictionary <string, object>
                    {
                        ["b"] = new object[]
                        {
                            "b1",
                            "b2",
                        }
                    },
                },
            };

            ss.Write(expected);
            ms.Position = 0;
            var sd     = new StreamDeserializer(ms);
            var actual = (Dictionary <string, object>)sd.Read();
            var a      = (object[])actual["a"];

            Assert.Equal("a1", a[0]);
            Assert.Equal("a2", a[1]);
            var a2 = (Dictionary <string, object>)((object[])actual["a"])[2];
            var b  = (object[])(a2["b"]);

            Assert.Equal("b1", b[0]);
            Assert.Equal("b2", b[1]);
        }
コード例 #2
0
 public static int GetNeededLength(byte[] buffer, int offset)
 {
     using (var stream = new MemoryStream(buffer, offset, buffer.Length - offset, false))
     {
         var deserializer  = new StreamDeserializer(stream);
         var messageLength = deserializer.ReadInt();
         return(messageLength + HeaderSize);
     }
 }
コード例 #3
0
 public static MessageType GetMessageType(byte[] buffer, int offset)
 {
     using (var stream = new MemoryStream(buffer, offset, buffer.Length - offset, false))
     {
         var deserializer = new StreamDeserializer(stream);
         deserializer.ReadInt();
         var messageType = (MessageType)deserializer.ReadInt();
         return(messageType);
     }
 }
コード例 #4
0
        public void TestArray()
        {
            var ms       = new MemoryStream();
            var ss       = new StreamSerializer(ms);
            var expected = new object[] { 123, "abc", null };

            ss.Write(expected);
            ms.Position = 0;
            var sd = new StreamDeserializer(ms);

            Assert.Equal(expected, sd.Read());
        }
コード例 #5
0
        public void TestString()
        {
            var ms = new MemoryStream();
            var ss = new StreamSerializer(ms);

            ss.Write("Hello world!");
            Assert.Equal(22, ms.Length);
            ms.Position = 0;
            var sd = new StreamDeserializer(ms);

            Assert.Equal("Hello world!", sd.Read());
        }
コード例 #6
0
        public void TestInteger()
        {
            var ms = new MemoryStream();
            var ss = new StreamSerializer(ms);

            ss.Write(101);
            Assert.Equal(13, ms.Length);
            ms.Position = 0;
            var sd = new StreamDeserializer(ms);

            Assert.Equal(101, sd.Read());
        }
コード例 #7
0
        public void TestNull()
        {
            var ms = new MemoryStream();
            var ss = new StreamSerializer(ms);

            ss.WriteNull();
            Assert.Equal(9, ms.Length);
            ms.Position = 0;
            var sd = new StreamDeserializer(ms);

            Assert.Null(sd.Read());
        }
コード例 #8
0
 public static MessageContainer ReadFromBuffer(byte[] buffer, int offset, IConnectionUtility utility)
 {
     using (var stream = new MemoryStream(buffer, offset, buffer.Length - offset, false))
     {
         var deserializer = new StreamDeserializer(stream);
         deserializer.ReadInt();
         var messageType = (MessageType)deserializer.ReadInt();
         if (!Enum.IsDefined(typeof(MessageType), messageType))
         {
             //Console.WriteLine("Invalid message type!");
             return(null);
         }
         //Console.WriteLine(" !! MessageType = " + messageType);
         return(new MessageContainer(MessageHelper.GetDeserializeMethod(messageType, utility)(deserializer)));
     }
 }
コード例 #9
0
 private static IDataStorage LoadStorage(string path)
 {
     try
     {
         using (var stream = File.OpenRead(path))
         {
             var deserializer = new StreamDeserializer(stream);
             return(DataStorage.Deserialize(deserializer));
         }
     }
     catch
     {
         Console.WriteLine("!! Cannot load data storage from '{0}'", path);
     }
     return(null);
 }
コード例 #10
0
        /// <inheritdoc />
        public async Task <TResponse> ParseAsync <TResponse>(HttpResponseMessage message, string jsonTokenSelector = null, params HttpStatusCode[] validCodes)
        {
            if (String.IsNullOrWhiteSpace(jsonTokenSelector))
            {
                using (var stream = await this.MessageReader.GetStreamIfSuccessfulOrThrowAsync(message, validCodes).ConfigureAwait(false))
                    using (var reader = new StreamReader(stream))
                        using (var json = new JsonTextReader(reader))
                        {
                            return(StreamDeserializer.Deserialize <TResponse>(json));
                        }
            }

            string bodyContent = await this.MessageReader.ReadMessageIfSuccessfulOrThrowAsync(message, validCodes).ConfigureAwait(false);

            return(InnerParse <TResponse>(bodyContent, jsonTokenSelector));
        }
コード例 #11
0
        public void TestSequence()
        {
            var ms = new MemoryStream();
            var ss = new StreamSerializer(ms);

            ss.WriteNull();
            ss.Write("abc");
            ss.Write(123);
            ss.Write(new object[] { 1, 2, 3 });
            ms.Position = 0;
            var sd = new StreamDeserializer(ms);

            Assert.Null(sd.Read());
            Assert.Equal("abc", sd.Read());
            Assert.Equal(123, sd.Read());
            Assert.Equal(new object[] { 1, 2, 3 }, sd.Read());
        }
コード例 #12
0
 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)));
     }
 }
コード例 #13
0
        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);
        }
コード例 #14
0
        public void TestDictionary()
        {
            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.Read();

            Assert.IsType <Dictionary <string, object> >(actual);
            var actualDict = (Dictionary <string, object>)actual;

            Assert.Equal(expected["a"], actualDict["a"]);
            Assert.Equal(expected["bcd"], actualDict["bcd"]);
            Assert.Equal(expected["hijk"], actualDict["hijk"]);
        }
コード例 #15
0
        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);
        }