public static void Accept(Stream input, Stream output) { using (var reader = new BinaryReader(input)) { var commandStreamWriter = new BinaryWriter(output); var token = reader.ReadByte(); if (BinaryToken.BeginArray != token) { throw new NotSupportedException("Binary frontend only supports arrays as top-level objects"); } // Read size reader.ReadUInt32(); while ((token = reader.ReadByte()) != BinaryToken.EndArray) { reader.BaseStream.Position -= 1; var info = GetObjectInfo(reader); commandStreamWriter.Write(CommandType.GetCreateCommandType(info.TypeId)); commandStreamWriter.Write((uint)info.Size); // Directly pass the binary object to the output stream no conversion necessary CopyStream(input, output, info.Size); } } }
public static void Accept(Stream input, Stream output) { var jsonStreamReader = new JsonStreamReader(input); using (var binaryStream = new MemoryStream()) using (var binaryWriter = new BinaryWriter(binaryStream)) { var commandStreamWriter = new BinaryWriter(output); var c = jsonStreamReader.ReadChar(); // '[' Assert.IsTrue(c == '[', $"FlatJson.FrontEnd.Accept expected '[' but found '{c}' as the first character of the stream. Line=[1]"); var fileInfo = new JsonObjectFileInfo(); // Read the next full json object from '{' to '} JsonObjectReader objectReader; while (jsonStreamReader.TryReadObject(out objectReader, ref fileInfo)) { objectReader.ReadBeginObject(); // Unpack the typeId var property = objectReader.ReadPropertyNameSegment(); Assert.IsTrue(property.Array[property.Offset] == '$'); var typeId = (UTinyTypeId)objectReader.ReadUInt16(); objectReader.Position = 0; commandStreamWriter.Write(CommandType.GetCreateCommandType(typeId)); // Translate the json object to binary BinaryTranslator.TranslateObject(ref objectReader, binaryWriter); // Write the command payload as binary commandStreamWriter.Write((uint)binaryStream.Position); commandStreamWriter.Write(binaryStream.GetBuffer(), 0, (int)binaryStream.Position); binaryStream.Position = 0; c = jsonStreamReader.ReadChar(); // ',' or ']' if (!(c == ',' || c == ']')) { throw new Exception($"FlatJson.FrontEnd.Accept expected ',' or ']' but found '{Escape(c)}' as the next character in the stream. Line=[{objectReader.Line}]"); } } } }