/// <inheritdoc/> public void Write(IEnumerable <RankedTheorem> rankedTheorems) { // Check if we have begun if (!HasWritingBegun) { throw new RankedTheoremIOException("Writing hasn't begun yet."); } // Go through the theorems foreach (var rankedTheorem in rankedTheorems) { // If we haven written anything, write a colon to separate entries if (_firstEntryWritten) { _writingStream.Write(","); } // Otherwise mark that we've written something (we're about to) else { _firstEntryWritten = true; } // Serialize the theorem var theoremString = JsonConvert.SerializeObject(RankedTheoremIntermediate.Convert(rankedTheorem)); // Write it _writingStream.Write(theoremString); } // Flush _writingStream.Flush(); }
/// <summary> /// Converts a serialized <see cref="RankedTheoremIntermediate"/> object into an original object. /// </summary> /// <param name="rankedTheoremIntermediate">The object to be converted.</param> /// <returns>The result of the conversion.</returns> public static RankedTheorem Convert(RankedTheoremIntermediate rankedTheoremIntermediate) { // Parse the configuration from the lines var(configuration, objectNames) = Parser.ParseConfiguration(rankedTheoremIntermediate.ConfigurationString.Split('\n')); // Parse the theorem (we assume all objects are named) var theorem = Parser.ParseTheorem(rankedTheoremIntermediate.TheoremString, objectNames, autocreateUnnamedObjects: false); // Return the final object return(new RankedTheorem(theorem, rankedTheoremIntermediate.Ranking, configuration)); }
/// <inheritdoc/> public IEnumerable <RankedTheorem> Read(string filePath) { // Prepare the reader of the file using JsonTextReader reader = new JsonTextReader(new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read))); // Read until the end while (reader.Read()) { // If we have a token that starts an object... if (reader.TokenType == JsonToken.StartObject) { // Load the object from the stream and convert it to our intermediate one var intermediateObject = JObject.Load(reader).ToObject <RankedTheoremIntermediate>(); // Return its converted version to the real object yield return(RankedTheoremIntermediate.Convert(intermediateObject)); } } }