Пример #1
0
        // this function profiles the json and amf parsing code
        // it takes in a path to a json file to load, converts json to amf, and parses amf
        public static void PerformanceTest(string path)
        {
            var newPath = PlayScript.Player.ResolveResourcePath(path);
            // read bytes from file
            var bytes = System.IO.File.ReadAllBytes(newPath);

            // perform json decoding
            var    jsonTimer = System.Diagnostics.Stopwatch.StartNew();
            var    jsonText  = System.Text.Encoding.UTF8.GetString(bytes);
            object jsonRoot  = _root.JSON.parse(jsonText);

            jsonTimer.Stop();

            // write to memory stream and return bytes
            using (var outStream = new MemoryStream()) {
                JsonStringToAmfStream(jsonText, outStream, true);

                outStream.Position = 0;

                var amfTimer = System.Diagnostics.Stopwatch.StartNew();
                var reader   = new Amf3Parser(outStream);
                var obj      = reader.ReadNextObject();
                amfTimer.Stop();
                Console.WriteLine("{0} JSONSize:{1} AMFSize:{2} JSONDecodeTime:{3} AMFDecodeTime:{4}", path, bytes.Length, outStream.Length,
                                  jsonTimer.Elapsed, amfTimer.Elapsed);
            }
        }
Пример #2
0
        internal void BeginRead(Amf3Parser parser)
        {
            mParser = parser;

            // read all property values from amf stream
            // these are in the order specified by the amf stream classDef and must be remapped for the deserializer for each propery read
            // note that mValues[0] is reserved for undefined, any missing properties will be mapped there
            int count = mStreamClassDef.Properties.Length;

            for (int i = 0; i < count; i++)
            {
                mParser.ReadNextObject(ref mValues[i + 1]);
            }

            // force value[0] to undefined to elegantly handle missing properties as index 0
            mValues[0] = Variant.Undefined;
        }
Пример #3
0
        // this function gets all the default values for a type by creating a dummy instance and serializing them in and out
        public static Amf3Object GetClassDefaultsAsObject(System.Type type)
        {
            // serialize to stream
            var stream    = new MemoryStream();
            var amfWriter = new Amf3Writer(stream);

            // write the defaults to amf stream
            amfWriter.WriteDefaultObjectForType(type);

            stream.Position = 0;

            // read back defaults object
            var amfReader = new Amf3Parser(stream);

            amfReader.OverrideSerializer = new Amf3Object.Serializer();
            return(amfReader.ReadNextObject() as Amf3Object);
        }
Пример #4
0
        public object ReadNextObject()
        {
            int b = stream.ReadByte();

            if (b < 0)
            {
                throw new EndOfStreamException();
            }

            AmfTypeCode code = (AmfTypeCode)b;

            switch (code)
            {
            case AmfTypeCode.Number:
                return(ReadNumber());

            case AmfTypeCode.Boolean:
                return(ReadBoolean());

            case AmfTypeCode.String:
                return(ReadString());

            case AmfTypeCode.Object:
                return(ReadObject());

            case AmfTypeCode.Null:
                return(null);

            case AmfTypeCode.Undefined:
                return(null);

            case AmfTypeCode.EndOfObject:
                return(EndOfObjectMarker);

            case AmfTypeCode.Array:
                return(ReadArray());

            case AmfTypeCode.Date:
                return(ReadDate());

            case AmfTypeCode.LongString:
            case AmfTypeCode.Xml:
                return(ReadLongString());

            case AmfTypeCode.TypedObject:
                return(ReadTypedObject());

            case AmfTypeCode.Amf3Data:
                if (amf3Parser == null || !PreserveAmf3Context)
                {
                    amf3Parser = new Amf3Parser(stream);
                }

                Amf3Wrapper wrapper = new Amf3Wrapper(amf3Parser.ReadNextObject());

                if (!PreserveAmf3Context)
                {
                    amf3Parser = null;
                }

                return(wrapper);
            }

            throw new InvalidOperationException("Cannot parse type " + code);
        }