forked from dotnet/interactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotebook.cs
More file actions
92 lines (78 loc) · 3.03 KB
/
Copy pathNotebook.cs
File metadata and controls
92 lines (78 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using Microsoft.DotNet.Interactive.Documents.Json;
namespace Microsoft.DotNet.Interactive.Documents.Jupyter;
public static class Notebook
{
private static readonly Encoding _encoding = new UTF8Encoding(false);
static Notebook()
{
JsonSerializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters =
{
new ByteArrayConverter(),
new DataDictionaryConverter(),
new InteractiveDocumentConverter(),
new InteractiveDocumentElementConverter(),
new InteractiveDocumentOutputElementConverter(),
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase),
}
};
}
internal static JsonSerializerOptions JsonSerializerOptions { get; }
public static InteractiveDocument Parse(
string json,
KernelInfoCollection? kernelInfos = null)
{
var document = JsonSerializer.Deserialize<InteractiveDocument>(json, JsonSerializerOptions) ??
throw new JsonException($"Unable to parse as {typeof(InteractiveDocument)}:\n\n{json}");
if (kernelInfos is not null)
{
document.NormalizeElementKernelNames(kernelInfos);
}
return document;
}
public static InteractiveDocument Read(
Stream stream,
KernelInfoCollection kernelInfos)
{
using var reader = new StreamReader(stream, _encoding);
var content = reader.ReadToEnd();
return Parse(content, kernelInfos);
}
public static string ToJupyterJson(
this InteractiveDocument document,
string? defaultLanguage = null)
{
if (defaultLanguage is { })
{
document.WithJupyterMetadata(defaultLanguage);
}
var json = JsonSerializer.Serialize(document, JsonSerializerOptions);
var singleSpaceIndentedJson =
Regex.Replace(
json,
"(^|\\G)( ){2}",
" ", RegexOptions.Multiline);
return singleSpaceIndentedJson;
}
public static void Write(InteractiveDocument document, Stream stream, KernelInfoCollection kernelInfos)
{
InteractiveDocument.MergeKernelInfos(document, kernelInfos);
using var writer = new StreamWriter(stream, _encoding, 1024, true);
var content = document.ToJupyterJson();
writer.Write(content);
writer.Flush();
}
}