// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) by respective owners including Yahoo!, Microsoft, and // individual contributors. All rights reserved. Released under a BSD // license as described in the file LICENSE. // // -------------------------------------------------------------------------------------------------------------------- using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; namespace VW.Serializer { /// /// A Json Reader allowing to prefix data from a wrapped JsonReader. /// internal class PrefixedJsonReader : JsonReader { private JsonReader reader; private Queue> prefix; /// /// Initializes a new instance of . /// /// The reader to be wrapped. /// The JsonTokens to be injected at the beginning of the stream. internal PrefixedJsonReader(JsonReader reader, params Tuple[] prefix) { this.reader = reader; this.prefix = new Queue>(prefix); } /// /// Injects the supplied prefix into the stream. /// /// True if another token is available, false otherwise. public override bool Read() { if (this.prefix.Count > 0) { var t = prefix.Dequeue(); this.SetToken(t.Item1, t.Item2); return true; } if (!this.reader.Read()) return false; this.SetToken(this.reader.TokenType, this.reader.Value); return true; } } }