Ejemplo n.º 1
0
 public override JsonArray Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 {
     return(context.ReadJsonArray());
 }
Ejemplo n.º 2
0
        /// <summary> Directly Deserialize a <see cref="JsonArray"/></summary>
        /// <param name="ctx"> context </param>
        public static JsonArray ReadJsonArray(this BsonDeserializationContext ctx)
        {
            var firstType = ctx.Reader.CurrentBsonType;

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

            // Log.Info($"Starting Reading JsonArray");
            ctx.StartArray();
            while (true)
            {
                // Log.Info($"Top of loop, state is {ctx.Reader.State}");
                var nextType = ctx.Reader.ReadBsonType();
                // This is retarded, again it should know immediately upon consuming
                // the start of the array that the array immediately ends
                // and shouldn't need to enter the loop.
                if (nextType == BsonType.EndOfDocument)
                {
                    break;
                }

                // Log.Info($"Read type is [{nextType}], state is {ctx.Reader.State}");
                if (nextType == BsonType.String)
                {
                    value.Add(ctx.Reader.ReadString());
                }
                else if (nextType == BsonType.Int32)
                {
                    value.Add(ctx.Reader.ReadInt32());
                }
                else if (nextType == BsonType.Int64)
                {
                    value.Add(ctx.Reader.ReadInt64());
                }
                else if (nextType == BsonType.Double)
                {
                    value.Add(ctx.Reader.ReadDouble());
                }
                else if (nextType == BsonType.Document)
                {
                    value.Add(ctx.ReadJsonObject());
                }
                else if (nextType == BsonType.Array)
                {
                    value.Add(ctx.ReadJsonArray());
                }
                else if (nextType == BsonType.Boolean)
                {
                    value.Add(ctx.Reader.ReadBoolean());
                }
                else if (nextType == BsonType.Null)
                {
                    value.Add(JsonNull.instance); ctx.Reader.ReadNull();
                }
                else
                {
                    ctx.Reader.SkipValue();
                }
            }

            // Log.Info($"Finishing Reading JsonArray");
            ctx.EndArray();
            return(value);
        }