Ejemplo n.º 1
0
        /// <summary> Directly Deserialize a <see cref="JsonObject"/></summary>
        /// <param name="ctx"> context </param>
        public static JsonObject ReadJsonObject(this BsonDeserializationContext ctx)
        {
            var firstType = ctx.Reader.CurrentBsonType;

            if (firstType == BsonType.Null)
            {
                ctx.Reader.ReadNull();
                return(null);
            }
            JsonObject value = new JsonObject();

            //Log.Info($"Starting Reading JsonObject");
            ctx.StartObject();

            //Log.Info($"Before loop, state is {ctx.Reader.State}");
            while (true)
            {
                // Why is the type before the name?
                // Log.Info($"Top of loop, state is {ctx.Reader.State}");
                var nextType = ctx.Reader.ReadBsonType();

                // This is retarded, it should know after reading the start
                // that the next thing is going to be the end, and shouldn't even need to enter the loop.
                if (nextType == BsonType.EndOfDocument)
                {
                    break;
                }
                // Log.Info($"Read type is [{nextType}], state is {ctx.Reader.State}");
                var name = ctx.ReadName();
                // Log.Info($"Read name is \"{name}\", state is {ctx.Reader.State}");

                if (nextType == BsonType.String)
                {
                    value[name] = ctx.Reader.ReadString();
                }
                else if (nextType == BsonType.Int32)
                {
                    value[name] = ctx.Reader.ReadInt32();
                }
                else if (nextType == BsonType.Int64)
                {
                    value[name] = ctx.Reader.ReadInt64();
                }
                else if (nextType == BsonType.Double)
                {
                    value[name] = ctx.Reader.ReadDouble();
                }
                else if (nextType == BsonType.Document)
                {
                    value[name] = ReadJsonObject(ctx);
                }
                else if (nextType == BsonType.Array)
                {
                    value[name] = ReadJsonArray(ctx);
                }
                else if (nextType == BsonType.Boolean)
                {
                    value[name] = ctx.Reader.ReadBoolean();
                }
                else if (nextType == BsonType.Null)
                {
                    value[name] = JsonNull.instance; ctx.Reader.ReadNull();
                }
                else
                {
                    ctx.Reader.SkipValue();
                }
            }

            // Log.Info($"Finishing Reading JsonObject");
            ctx.EndObject();
            return(value);
        }