-
Notifications
You must be signed in to change notification settings - Fork 368
/
JavaScriptKernelValueDeclarer.cs
41 lines (37 loc) · 1.56 KB
/
JavaScriptKernelValueDeclarer.cs
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
// 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.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Formatting.TabularData;
namespace Microsoft.DotNet.Interactive.ValueSharing
{
public class JavaScriptKernelValueDeclarer : IKernelValueDeclarer
{
private static readonly JsonSerializerOptions _serializerOptions;
static JavaScriptKernelValueDeclarer()
{
_serializerOptions = new JsonSerializerOptions
{
WriteIndented = false,
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
NumberHandling = JsonNumberHandling.AllowReadingFromString |
JsonNumberHandling.AllowNamedFloatingPointLiterals,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters =
{
new TableSchemaFieldTypeConverter(),
new TabularDataResourceConverter(),
new DataDictionaryConverter()
}
};
}
public bool TryGetValueDeclaration(string valueName, object value, out KernelCommand command)
{
var code = $"{valueName} = {JsonSerializer.Serialize(value, _serializerOptions)};";
command = new SubmitCode(code);
return true;
}
}
}