public void GetJson() { _writer.WriteNameValue("a", 1); _writer.WriteNameValue("B", "C"); _writer.WriteNameArray("d", new[] { "e", "f" }); _writer.WriteObjectStart("g"); _writer.WriteNameValue("h", "i"); _writer.WriteObjectEnd(); const string expectedJson = @"{ ""a"": 1, ""B"": ""C"", ""d"": [ ""e"", ""f"" ], ""g"": { ""h"": ""i"" } }"; var actualJson = _writer.GetJson(); Assert.Equal(expectedJson, actualJson); }
internal static JObject ToJObject(this PackageSpec spec) { using (var jsonWriter = new JTokenWriter()) using (var writer = new JsonObjectWriter(jsonWriter)) { writer.WriteObjectStart(); PackageSpecWriter.Write(spec, writer); writer.WriteObjectEnd(); return((JObject)jsonWriter.Token); } }
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); }
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)); } }
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); } }
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(); } }
public void WriteObjectStart_WithNoParameters_WhenDisposed_Throws() { _writer.Dispose(); Assert.Throws <ObjectDisposedException>(() => _writer.WriteObjectStart()); }