-
Notifications
You must be signed in to change notification settings - Fork 0
API Host TypedDocumentSerializer
Static TDoc text serializer
命名空间:AuroraScript.Runtime.Serialization。类型:static class。适用版本:4.0.0。
Namespace:
AuroraScript.Runtime.Serialization. Kind: static class. Applies to 4.0.0.
Introduction
TypedDocumentSerializer 是最小的宿主字符串 API:给定 AuroraEngine、ScriptDatum 或文本后,直接进行 TDoc 序列化或反序列化。
TypedDocumentSerializeris the minimal host text API: given anAuroraEngine, aScriptDatum, or text, it directly serializes or deserializes TDoc.
它适合一次性文本转换。需要复用引擎、读写文件或流时,使用 AuroraTypedDocument。
It suits one-off text conversion. Use AuroraTypedDocument when reusing an engine or reading and writing files or streams.
Quick Start
using AuroraScript;
using AuroraScript.Runtime;
using AuroraScript.Runtime.Serialization;
using System;
var engine = new AuroraEngine(EngineOptions.Default);
var text = TypedDocumentSerializer.Serialize(engine, ScriptDatum.FromString("Aurora"));
var value = TypedDocumentSerializer.Deserialize(engine, text);
Console.WriteLine(value); // AuroraAPI
static string Serialize(AuroraEngine engine, ScriptDatum value, TypedDocumentOptions options = null)
将一个 AuroraScript 值写为独立 TDoc 文本。
Writes one AuroraScript value as standalone TDoc text.
Parameters:
-
engine
用于日期格式和 CLR/CIL 类型注册表的 AuroraScript 引擎。不能为null。AuroraScript engine used for date formatting and the CLR/CIL type registry. Cannot be
null. -
value
待序列化的 AuroraScript 值。AuroraScript value to serialize.
-
options
可选文本选项;为null时使用TypedDocumentOptions.Default。Optional text options;
TypedDocumentOptions.Defaultis used whennull.
Returns
TDoc 文本。
TDoc text.
边界行为
Boundary Behavior
- 文本不含脚本
tdoc标记;默认使用四空格缩进、尾逗号,并仅输出无法由字面量推断的类型名。Text contains no script
tdocmarker; the default uses four-space indentation, trailing commas, and only type names that cannot be inferred from literals. - 写入运行时不可表示值时不会因为该值失败:对象属性会省略;数组元素以及
HashMap条目中的不可表示键或值会写为null;根值会写为null。这包括函数、代理、访问器属性、未注册 CLR/CIL 对象、非有限 Number,以及已见过的循环/共享引用。Writing an unrepresentable runtime value does not fail because of that value: object properties are omitted; array elements and unrepresentable keys or values in a
HashMapentry becomenull; and a root value becomesnull. This includes functions, proxies, accessor properties, unregistered CLR/CIL objects, non-finite Numbers, and previously seen circular/shared references. -
ScriptObject的可枚举 prototype 属性会写为扁平的可见成员;读取后不会还原原型链。Enumerable prototype properties of a
ScriptObjectare written as flat visible members; reading does not restore the prototype chain.
简单示例
Simple Example
var text = TypedDocumentSerializer.Serialize(
engine,
ScriptDatum.FromNumber(42.5),
new TypedDocumentOptions { Indented = false });
Console.WriteLine(text); // 42.5static ScriptDatum Deserialize(AuroraEngine engine, string text, TypedDocumentOptions options = null)
将一个独立 TDoc 文本读为 ScriptDatum。根级原始值保持其原始种类,而不是总被包装为 ScriptObject。
Reads standalone TDoc text as
ScriptDatum. Root primitive values keep their original kind instead of always being wrapped asScriptObject.
Parameters:
-
engine
用于日期格式和 CLR/CIL 类型注册表的 AuroraScript 引擎。不能为null。AuroraScript engine used for date formatting and the CLR/CIL type registry. Cannot be
null. -
text
要读取的完整 TDoc 文本。不能为null。Complete TDoc text to read. Cannot be
null. -
options
可选读取与诊断选项;为null时使用TypedDocumentOptions.Default。Optional read and diagnostic options;
TypedDocumentOptions.Defaultis used whennull.
Returns
解析得到的 ScriptDatum。
Parsed
ScriptDatum.
边界行为
Boundary Behavior
- 文本只能有一个根值;未知类型、错误的类型形状、重复属性或超出深度限制时抛出
TypedDocumentException。Text may contain one root value only; unknown types, invalid type shapes, duplicate properties, or depth-limit violations throw
TypedDocumentException. -
Date字符串必须匹配引擎的Runtime.DateTimeFormat;CLR/CIL 别名必须已经由当前引擎注册。Datestrings must match the engineRuntime.DateTimeFormat; CLR/CIL aliases must already be registered on the current engine.
简单示例
Simple Example
using AuroraScript.Runtime.Types;
var value = TypedDocumentSerializer.Deserialize(
engine,
"Object { String name \"Aurora\", Int8Array levels [1, 2] }");
var profile = (ScriptObject)value.Object;
Console.WriteLine(profile.GetPropertyValue("name")); // AuroraType-Emission Option
EmitTypeNames 默认是 false。默认省略原始字面量能够唯一推断的 Object、Array、String、Number 和 Boolean;Packed Array 与 CLR/CIL 类型始终保留类型名。设为 true 时强制输出所有可用类型名。
EmitTypeNamesdefaults tofalse. By default it omitsObject,Array,String,Number, andBooleanwhen a raw literal uniquely infers them; packed arrays and CLR/CIL types always retain type names. Set it totrueto force every available type name.
var compact = TypedDocumentSerializer.Serialize(
engine,
ScriptDatum.FromString("Aurora"),
new TypedDocumentOptions { Indented = false, EmitTypeNames = true });
Console.WriteLine(compact); // String "Aurora"Related APIs
AuroraScript.JIT 4.0.0 · Documentation Home · Repository · MIT License