private static JObject WriteLockFile(LockFile lockFile) { var json = new JObject { [VersionProperty] = new JValue(lockFile.Version), [TargetsProperty] = JsonUtility.WriteObject(lockFile.Targets, WriteTarget), [LibrariesProperty] = JsonUtility.WriteObject(lockFile.Libraries, WriteLibrary), [ProjectFileDependencyGroupsProperty] = JsonUtility.WriteObject(lockFile.ProjectFileDependencyGroups, WriteProjectFileDependencyGroup) }; if (lockFile.PackageFolders?.Any() == true) { json[PackageFoldersProperty] = JsonUtility.WriteObject(lockFile.PackageFolders, WriteFileItem); } if (lockFile.Version >= 2) { if (lockFile.PackageSpec != null) { using (var jsonWriter = new JTokenWriter()) using (var writer = new JsonObjectWriter(jsonWriter)) { writer.WriteObjectStart(); PackageSpecWriter.Write(lockFile.PackageSpec, writer); writer.WriteObjectEnd(); json[PackageSpecProperty] = (JObject)jsonWriter.Token; } } } if (lockFile.Version >= 3) { if (lockFile.LogMessages.Count > 0) { var projectPath = lockFile.PackageSpec?.RestoreMetadata?.ProjectPath; json[LogsProperty] = WriteLogMessages(lockFile.LogMessages, projectPath); } } if (lockFile.CentralTransitiveDependencyGroups.Any()) { JToken token = WriteCentralTransitiveDependencyGroup(lockFile.CentralTransitiveDependencyGroups); json[CentralTransitiveDependencyGroupsProperty] = (JObject)token; } return(json); }
/// <summary> /// Clone a PackageSpec and underlying JObject. /// </summary> public PackageSpec Clone() { var writer = new JsonObjectWriter(); PackageSpecWriter.Write(this, writer); var json = writer.GetJObject(); var spec = JsonPackageSpecReader.GetPackageSpec(json); spec.Name = Name; spec.FilePath = FilePath; return(spec); }
internal static PackageSpec RoundTrip(this PackageSpec spec, string packageSpecName, string packageSpecPath) { using (var stringWriter = new StringWriter()) using (var jsonWriter = new JsonTextWriter(stringWriter)) using (var writer = new JsonObjectWriter(jsonWriter)) { writer.WriteObjectStart(); PackageSpecWriter.Write(spec, writer); writer.WriteObjectEnd(); return(JsonPackageSpecReader.GetPackageSpec(stringWriter.ToString(), packageSpecName, packageSpecPath)); } }
public static void SaveRuntimeGraph(string filePath, RuntimeGraph runtimeGraph) { var jsonObjectWriter = new JsonObjectWriter(); var runtimeJsonWriter = new RuntimeJsonWriter(jsonObjectWriter); JsonRuntimeFormat.WriteRuntimeGraph(runtimeJsonWriter, runtimeGraph); using (var file = File.CreateText(filePath)) using (var jsonWriter = new JsonTextWriter(file)) { jsonWriter.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii; jsonWriter.Formatting = Formatting.Indented; jsonObjectWriter.WriteTo(jsonWriter); } }
private static JToken WriteCentralTransitiveDependencyGroup(IList <CentralTransitiveDependencyGroup> centralTransitiveDependencyGroups) { using (var jsonWriter = new JTokenWriter()) using (var writer = new JsonObjectWriter(jsonWriter)) { writer.WriteObjectStart(); foreach (var centralTransitiveDepGroup in centralTransitiveDependencyGroups.OrderBy(ptdg => ptdg.FrameworkName)) { PackageSpecWriter.SetCentralTransitveDependencyGroup(writer, centralTransitiveDepGroup.FrameworkName, centralTransitiveDepGroup.TransitiveDependencies); } writer.WriteObjectEnd(); return(jsonWriter.Token); } }
private static void VerifyJsonPackageSpecRoundTrip(string json) { // Arrange & Act var spec = JsonPackageSpecReader.GetPackageSpec(json, "testName", @"C:\fake\path"); var writer = new JsonObjectWriter(); PackageSpecWriter.Write(spec, writer); var actualResult = writer.GetJson(); var expected = JObject.Parse(json).ToString(); // Assert Assert.Equal(expected, actualResult); }
public static PackageSpec RoundTrip(this PackageSpec spec) { using (var jsonWriter = new JTokenWriter()) using (var writer = new JsonObjectWriter(jsonWriter)) { writer.WriteObjectStart(); PackageSpecWriter.Write(spec, writer); writer.WriteObjectEnd(); #pragma warning disable CS0618 return(JsonPackageSpecReader.GetPackageSpec((JObject)jsonWriter.Token)); #pragma warning restore CS0618 } }
public static void WriteRuntimeGraph(string filePath, RuntimeGraph runtimeGraph) { using (var fileStream = new FileStream(filePath, FileMode.Create)) using (var textWriter = new StreamWriter(fileStream)) using (var jsonWriter = new JsonTextWriter(textWriter)) using (var writer = new JsonObjectWriter(jsonWriter)) { jsonWriter.Formatting = Formatting.Indented; // workaround https://github.com/NuGet/Home/issues/9532 writer.WriteObjectStart(); JsonRuntimeFormat.WriteRuntimeGraph(writer, runtimeGraph); writer.WriteObjectEnd(); } }
/// <summary> /// Writes a PackageSpec to a file. /// </summary> /// <param name="packageSpec">A <c>PackageSpec</c> instance.</param> /// <param name="filePath">A file path to write to.</param> public static void WriteToFile(PackageSpec packageSpec, string filePath) { if (packageSpec == null) { throw new ArgumentNullException(nameof(packageSpec)); } if (string.IsNullOrEmpty(filePath)) { throw new ArgumentException(Strings.ArgumentNullOrEmpty, nameof(filePath)); } using (var fileStream = new FileStream(filePath, FileMode.Create)) using (var textWriter = new StreamWriter(fileStream)) using (var jsonWriter = new JsonTextWriter(textWriter)) using (var writer = new JsonObjectWriter(jsonWriter)) { jsonWriter.Formatting = Formatting.Indented; Write(packageSpec, writer); } }
private static JObject WriteLockFile(LockFile lockFile) { var json = new JObject { [VersionProperty] = new JValue(lockFile.Version), [TargetsProperty] = WriteObject(lockFile.Targets, WriteTarget), [LibrariesProperty] = WriteObject(lockFile.Libraries, WriteLibrary), [ProjectFileDependencyGroupsProperty] = WriteObject(lockFile.ProjectFileDependencyGroups, WriteProjectFileDependencyGroup) }; if (lockFile.PackageFolders?.Any() == true) { json[PackageFoldersProperty] = WriteObject(lockFile.PackageFolders, WriteFileItem); } if (lockFile.Version >= 2) { if (lockFile.PackageSpec != null) { var writer = new JsonObjectWriter(); PackageSpecWriter.Write(lockFile.PackageSpec, writer); var packageSpec = writer.GetJObject(); json[PackageSpecProperty] = packageSpec; } } if (lockFile.Version >= 3) { if (lockFile.LogMessages.Count > 0) { var projectPath = lockFile.PackageSpec?.RestoreMetadata?.ProjectPath; json[LogsProperty] = WriteLogMessages(lockFile.LogMessages, projectPath); } } return(json); }
public void FromJson(Type type, JObject jObject) { JsonObjectWriter.WriteListObject(type, jObject, this); }
public JsonObjectWriterTests() { _stringWriter = new StringWriter(); _jsonWriter = new JsonTextWriter(_stringWriter); _writer = new JsonObjectWriter(_jsonWriter); }
public JsonObjectWriterTests() { _writer = new JsonObjectWriter(); }
public void Write_ThrowsForNullPackageSpec() { var writer = new JsonObjectWriter(); Assert.Throws <ArgumentNullException>(() => PackageSpecWriter.Write(packageSpec: null, writer: writer)); }