/// <summary> /// Serializes an object to a file as JSON /// </summary> /// <param name="Location">Location of the file</param> /// <param name="Object">Object to serialize</param> static void SerializeJson(FileReference Location, object Object) { using (MemoryStream Stream = new MemoryStream()) { DataContractJsonSerializer InputFileDataSerializer = new DataContractJsonSerializer(Object.GetType()); InputFileDataSerializer.WriteObject(Stream, Object); FileReference.WriteAllBytes(Location, Stream.ToArray()); } }
public void Dispose() { // Try to write the output file in a transactional way; write it to a temporary file and rename it. DirectoryReference.CreateDirectory(OutputFile.Directory); InputData Data = new InputData(); Data.Jobs.Add(Job); FileReference TempOutputFile = new FileReference(OutputFile.FullName + ".incoming"); using (MemoryStream Stream = new MemoryStream()) { DataContractJsonSerializer InputFileDataSerializer = new DataContractJsonSerializer(typeof(InputData)); InputFileDataSerializer.WriteObject(Stream, Data); FileReference.WriteAllBytes(TempOutputFile, Stream.ToArray()); } FileReference.Delete(OutputFile); FileReference.Move(TempOutputFile, OutputFile); }