Пример #1
0
        public static void TranscodeCBJson <From>(Stream from, Stream to)
        {
            var input  = new InputStream(from, 11);
            var reader = new CompactBinaryReader <InputStream>(input);

            var writer = new SimpleJsonWriter(to);

            Transcode <From> .FromTo(reader, writer);

            writer.Flush();
        }
Пример #2
0
        static void Main()
        {
            var config = new Config
            {
                Variant = "Simple",
                Enabled = true,
                Urls    = { "http://example.com", "http://www.example.com" }
            };

            var jsonString = new StringBuilder();
            var jsonWriter = new SimpleJsonWriter(new StringWriter(jsonString));

            Serialize.To(jsonWriter, config);
            jsonWriter.Flush();
            Console.WriteLine(jsonString);

            var reader = new SimpleJsonReader(new StringReader(jsonString.ToString()));

            config = Deserialize <Config> .From(reader);
        }
Пример #3
0
        public static string Serializer(string name, string uni, double year)
        {
            var src = new Record
            {
                Name        = name,
                University  = uni,
                GraduationY = year
            };

            // Inspect the Record
            Console.WriteLine(src.Name + " " + src.University + " " + src.GraduationY);
            Console.ReadLine();

            //Serialize the Record into Json format
            var jsonString = new StringBuilder();
            var jsonWriter = new SimpleJsonWriter(new StringWriter(jsonString));

            Serialize.To(jsonWriter, src);
            jsonWriter.Flush();

            //Return the serialized Record
            return(jsonString.ToString());
        }
Пример #4
0
 /// <summary>
 /// Serializes an object to a SimpleJsonWriter stream.
 /// </summary>
 /// <typeparam name="T">Object type to serialize.</typeparam>
 /// <typeparam name="O">Destination stream type.</typeparam>
 /// <param name="writer">Writer to emit to.</param>
 /// <param name="obj">Object to write.</param>
 public static void Write <T>(this SimpleJsonWriter writer, T obj)
 {
     Serialize.To(writer, obj);
     writer.Flush();
 }
Пример #5
0
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("Usage:\nBond.CompatibilityTest json|compact|compact2|fast|simple|simple2|schema input_file output_file [json|compact|fast|simple|simple2]");
                return;
            }

            var fromProtocol = args[0];
            var toProtocol   = fromProtocol;
            var inputFile    = args[1];
            var outputFile   = args[2];

            if (args.Length == 4)
            {
                toProtocol = args[3];
            }

            using (var inputStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
            {
                var input = new InputStream(inputStream);
                using (var outputStream = new FileStream(outputFile, FileMode.Create))
                {
                    var output = new OutputStream(outputStream);
                    if (fromProtocol == "json")
                    {
                        var reader     = new SimpleJsonReader(inputStream);
                        var writer     = new SimpleJsonWriter(outputStream);
                        var transcoder = new Transcoder <SimpleJsonReader, SimpleJsonWriter>(Schema <Compat> .RuntimeSchema);
                        transcoder.Transcode(reader, writer);
                        writer.Flush();
                    }
                    else if (fromProtocol == "compact")
                    {
                        var reader = new CompactBinaryReader <InputStream>(input);
                        Write(reader, output, toProtocol);
                    }
                    else if (fromProtocol == "compact2")
                    {
                        var reader = new CompactBinaryReader <InputStream>(input, 2);
                        Write(reader, output, toProtocol);
                    }
                    else if (fromProtocol == "fast")
                    {
                        var reader = new FastBinaryReader <InputStream>(input, 2);
                        Write(reader, output, toProtocol);
                    }
                    else if (fromProtocol == "simple")
                    {
                        var reader = new SimpleBinaryReader <InputStream>(input);
                        Write(reader, output, toProtocol);
                    }
                    else if (fromProtocol == "simple2")
                    {
                        var reader = new SimpleBinaryReader <InputStream>(input, 2);
                        Write(reader, output, toProtocol);
                    }
                    else if (fromProtocol == "schema")
                    {
                        var schema = Unmarshal <SchemaDef> .From(input);

                        if (!Comparer.Equal(schema, Schema <Compat> .RuntimeSchema.SchemaDef))
                        {
                            Console.WriteLine("SchemaDef is different");
                        }
                        var writer = new CompactBinaryWriter <OutputStream>(output);
                        Marshal.To(writer, Schema <Compat> .RuntimeSchema.SchemaDef);
                        output.Flush();
                    }
                    else
                    {
                        Console.WriteLine("Unsupported input protocol {0}", fromProtocol);
                    }
                }
            }
        }