JsonSerializer.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. using System.Globalization;
  2. using System.Runtime.CompilerServices;
  3. using System.Text;
  4. using Jint.Collections;
  5. using Jint.Native.BigInt;
  6. using Jint.Native.Boolean;
  7. using Jint.Native.Number;
  8. using Jint.Native.Number.Dtoa;
  9. using Jint.Native.Object;
  10. using Jint.Native.Proxy;
  11. using Jint.Native.String;
  12. using Jint.Runtime;
  13. using Jint.Runtime.Descriptors;
  14. using Jint.Runtime.Interop;
  15. namespace Jint.Native.Json
  16. {
  17. public sealed class JsonSerializer
  18. {
  19. private readonly Engine _engine;
  20. private ObjectTraverseStack _stack = null!;
  21. private string? _indent;
  22. private string _gap = string.Empty;
  23. private List<JsValue>? _propertyList;
  24. private JsValue _replacerFunction = JsValue.Undefined;
  25. private static readonly JsString toJsonProperty = new("toJSON");
  26. public JsonSerializer(Engine engine)
  27. {
  28. _engine = engine;
  29. }
  30. public JsValue Serialize(JsValue value)
  31. {
  32. return Serialize(value, JsValue.Undefined, JsValue.Undefined);
  33. }
  34. public JsValue Serialize(JsValue value, JsValue replacer, JsValue space)
  35. {
  36. _stack = new ObjectTraverseStack(_engine);
  37. // for JSON.stringify(), any function passed as the first argument will return undefined
  38. // if the replacer is not defined. The function is not called either.
  39. if (value.IsCallable && ReferenceEquals(replacer, JsValue.Undefined))
  40. {
  41. return JsValue.Undefined;
  42. }
  43. SetupReplacer(replacer);
  44. _gap = BuildSpacingGap(space);
  45. var wrapper = _engine.Realm.Intrinsics.Object.Construct(Arguments.Empty);
  46. wrapper.DefineOwnProperty(JsString.Empty, new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable));
  47. var jsonBuilder = new StringBuilder();
  48. var target = new SerializerState(jsonBuilder);
  49. if (SerializeJSONProperty(JsString.Empty, wrapper, ref target) == SerializeResult.Undefined)
  50. {
  51. return JsValue.Undefined;
  52. }
  53. return new JsString(target.Json.ToString());
  54. }
  55. private void SetupReplacer(JsValue replacer)
  56. {
  57. if (replacer is not ObjectInstance oi)
  58. {
  59. return;
  60. }
  61. if (oi.IsCallable)
  62. {
  63. _replacerFunction = replacer;
  64. }
  65. else
  66. {
  67. if (oi.IsArray())
  68. {
  69. _propertyList = new List<JsValue>();
  70. var len = oi.Length;
  71. var k = 0;
  72. while (k < len)
  73. {
  74. var prop = JsString.Create(k);
  75. var v = replacer.Get(prop);
  76. var item = JsValue.Undefined;
  77. if (v.IsString())
  78. {
  79. item = v;
  80. }
  81. else if (v.IsNumber())
  82. {
  83. item = TypeConverter.ToString(v);
  84. }
  85. else if (v.IsObject())
  86. {
  87. if (v is StringInstance or NumberInstance)
  88. {
  89. item = TypeConverter.ToString(v);
  90. }
  91. }
  92. if (!item.IsUndefined() && !_propertyList.Contains(item))
  93. {
  94. _propertyList.Add(item);
  95. }
  96. k++;
  97. }
  98. }
  99. }
  100. }
  101. private static string BuildSpacingGap(JsValue space)
  102. {
  103. if (space.IsObject())
  104. {
  105. var spaceObj = space.AsObject();
  106. if (spaceObj.Class == ObjectClass.Number)
  107. {
  108. space = TypeConverter.ToNumber(spaceObj);
  109. }
  110. else if (spaceObj.Class == ObjectClass.String)
  111. {
  112. space = TypeConverter.ToJsString(spaceObj);
  113. }
  114. }
  115. // defining the gap
  116. if (space.IsNumber())
  117. {
  118. var number = ((JsNumber) space)._value;
  119. if (number > 0)
  120. {
  121. return new string(' ', (int) System.Math.Min(10, number));
  122. }
  123. return string.Empty;
  124. }
  125. if (space.IsString())
  126. {
  127. var stringSpace = space.ToString();
  128. return stringSpace.Length <= 10 ? stringSpace : stringSpace.Substring(0, 10);
  129. }
  130. return string.Empty;
  131. }
  132. /// <summary>
  133. /// https://tc39.es/ecma262/#sec-serializejsonproperty
  134. /// </summary>
  135. private SerializeResult SerializeJSONProperty(JsValue key, JsValue holder, ref SerializerState target)
  136. {
  137. var value = ReadUnwrappedValue(key, holder);
  138. if (ReferenceEquals(value, JsValue.Null))
  139. {
  140. target.Json.Append("null");
  141. return SerializeResult.NotUndefined;
  142. }
  143. if (value.IsBoolean())
  144. {
  145. target.Json.Append(((JsBoolean) value)._value ? "true" : "false");
  146. return SerializeResult.NotUndefined;
  147. }
  148. if (value.IsString())
  149. {
  150. QuoteJSONString(value.ToString(), target.Json);
  151. return SerializeResult.NotUndefined;
  152. }
  153. if (value.IsNumber())
  154. {
  155. var doubleValue = ((JsNumber) value)._value;
  156. if (value.IsInteger())
  157. {
  158. target.Json.Append((long) doubleValue);
  159. return SerializeResult.NotUndefined;
  160. }
  161. var isFinite = !double.IsNaN(doubleValue) && !double.IsInfinity(doubleValue);
  162. if (isFinite)
  163. {
  164. if (TypeConverter.CanBeStringifiedAsLong(doubleValue))
  165. {
  166. target.Json.Append((long) doubleValue);
  167. return SerializeResult.NotUndefined;
  168. }
  169. target.DtoaBuilder.Reset();
  170. var sb = new ValueStringBuilder(stackalloc char[128]);
  171. NumberPrototype.NumberToString(doubleValue, target.DtoaBuilder, ref sb);
  172. target.Json.Append(sb.ToString());
  173. return SerializeResult.NotUndefined;
  174. }
  175. target.Json.Append(JsString.NullString);
  176. return SerializeResult.NotUndefined;
  177. }
  178. if (value.IsBigInt())
  179. {
  180. ExceptionHelper.ThrowTypeError(_engine.Realm, "Do not know how to serialize a BigInt");
  181. }
  182. if (value is ObjectInstance { IsCallable: false } objectInstance)
  183. {
  184. if (CanSerializesAsArray(objectInstance))
  185. {
  186. SerializeJSONArray(objectInstance, ref target);
  187. return SerializeResult.NotUndefined;
  188. }
  189. if (objectInstance is IObjectWrapper wrapper
  190. && _engine.Options.Interop.SerializeToJson is { } serialize)
  191. {
  192. target.Json.Append(serialize(wrapper.Target));
  193. return SerializeResult.NotUndefined;
  194. }
  195. SerializeJSONObject(objectInstance, ref target);
  196. return SerializeResult.NotUndefined;
  197. }
  198. return SerializeResult.Undefined;
  199. }
  200. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  201. private JsValue ReadUnwrappedValue(JsValue key, JsValue holder)
  202. {
  203. var value = holder.Get(key);
  204. if (value._type <= InternalTypes.Integer && _replacerFunction.IsUndefined())
  205. {
  206. return value;
  207. }
  208. var isBigInt = value is BigIntInstance || value.IsBigInt();
  209. if (value.IsObject() || isBigInt)
  210. {
  211. var toJson = value.GetV(_engine.Realm, toJsonProperty);
  212. if (toJson.IsUndefined() && isBigInt)
  213. {
  214. toJson = _engine.Realm.Intrinsics.BigInt.PrototypeObject.Get(toJsonProperty);
  215. }
  216. if (toJson.IsObject())
  217. {
  218. if (toJson.AsObject() is ICallable callableToJson)
  219. {
  220. value = callableToJson.Call(value, Arguments.From(TypeConverter.ToPropertyKey(key)));
  221. }
  222. }
  223. }
  224. if (!_replacerFunction.IsUndefined())
  225. {
  226. var replacerFunctionCallable = (ICallable) _replacerFunction.AsObject();
  227. value = replacerFunctionCallable.Call(holder, Arguments.From(TypeConverter.ToPropertyKey(key), value));
  228. }
  229. if (value.IsObject())
  230. {
  231. value = value switch
  232. {
  233. NumberInstance => TypeConverter.ToNumber(value),
  234. StringInstance => TypeConverter.ToString(value),
  235. BooleanInstance booleanInstance => booleanInstance.BooleanData,
  236. BigIntInstance bigIntInstance => bigIntInstance.BigIntData,
  237. _ => value
  238. };
  239. }
  240. return value;
  241. }
  242. private static bool CanSerializesAsArray(ObjectInstance value)
  243. {
  244. if (value is JsArray)
  245. {
  246. return true;
  247. }
  248. if (value is JsProxy proxyInstance && CanSerializesAsArray(proxyInstance._target))
  249. {
  250. return true;
  251. }
  252. if (value is ObjectWrapper { IsArrayLike: true })
  253. {
  254. return true;
  255. }
  256. return false;
  257. }
  258. /// <summary>
  259. /// https://tc39.es/ecma262/#sec-quotejsonstring
  260. /// </summary>
  261. /// <remarks>
  262. /// MethodImplOptions.AggressiveOptimization = 512 which is only exposed in .NET Core.
  263. /// </remarks>
  264. [MethodImpl(MethodImplOptions.AggressiveInlining | (MethodImplOptions)512)]
  265. private static unsafe void QuoteJSONString(string value, StringBuilder target)
  266. {
  267. if (value.Length == 0)
  268. {
  269. target.Append("\"\"");
  270. return;
  271. }
  272. target.Append('"');
  273. #if NETCOREAPP1_0_OR_GREATER
  274. fixed (char* ptr = value)
  275. {
  276. int remainingLength = value.Length;
  277. int offset = 0;
  278. while (true)
  279. {
  280. int index = System.Text.Encodings.Web.JavaScriptEncoder.Default.FindFirstCharacterToEncode(ptr + offset, remainingLength);
  281. if (index < 0)
  282. {
  283. // append the remaining text which doesn't need any encoding.
  284. target.Append(value.AsSpan(offset));
  285. break;
  286. }
  287. index += offset;
  288. if (index - offset > 0)
  289. {
  290. // append everything which does not need any encoding until the found index.
  291. target.Append(value.AsSpan(offset, index - offset));
  292. }
  293. AppendJsonStringCharacter(value, ref index, target);
  294. offset = index + 1;
  295. remainingLength = value.Length - offset;
  296. if (remainingLength == 0)
  297. {
  298. break;
  299. }
  300. }
  301. }
  302. #else
  303. for (var i = 0; i < value.Length; i++)
  304. {
  305. AppendJsonStringCharacter(value, ref i, target);
  306. }
  307. #endif
  308. target.Append('"');
  309. }
  310. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  311. private static void AppendJsonStringCharacter(string value, ref int index, StringBuilder target)
  312. {
  313. var c = value[index];
  314. switch (c)
  315. {
  316. case '\"':
  317. target.Append("\\\"");
  318. break;
  319. case '\\':
  320. target.Append("\\\\");
  321. break;
  322. case '\b':
  323. target.Append("\\b");
  324. break;
  325. case '\f':
  326. target.Append("\\f");
  327. break;
  328. case '\n':
  329. target.Append("\\n");
  330. break;
  331. case '\r':
  332. target.Append("\\r");
  333. break;
  334. case '\t':
  335. target.Append("\\t");
  336. break;
  337. default:
  338. if (char.IsSurrogatePair(value, index))
  339. {
  340. #if NETCOREAPP1_0_OR_GREATER
  341. target.Append(value.AsSpan(index, 2));
  342. index++;
  343. #else
  344. target.Append(c);
  345. index++;
  346. target.Append(value[index]);
  347. #endif
  348. }
  349. else if (c < 0x20 || char.IsSurrogate(c))
  350. {
  351. target.Append("\\u");
  352. target.Append(((int) c).ToString("x4", CultureInfo.InvariantCulture));
  353. }
  354. else
  355. {
  356. target.Append(c);
  357. }
  358. break;
  359. }
  360. }
  361. /// <summary>
  362. /// https://tc39.es/ecma262/#sec-serializejsonarray
  363. /// </summary>
  364. private void SerializeJSONArray(ObjectInstance value, ref SerializerState target)
  365. {
  366. var len = TypeConverter.ToUint32(value.Get(CommonProperties.Length));
  367. if (len == 0)
  368. {
  369. target.Json.Append("[]");
  370. return;
  371. }
  372. _stack.Enter(value);
  373. var stepback = _indent;
  374. if (_gap.Length > 0)
  375. {
  376. _indent += _gap;
  377. }
  378. const char separator = ',';
  379. bool hasPrevious = false;
  380. for (int i = 0; i < len; i++)
  381. {
  382. if (hasPrevious)
  383. {
  384. target.Json.Append(separator);
  385. }
  386. else
  387. {
  388. target.Json.Append('[');
  389. }
  390. if (_gap.Length > 0)
  391. {
  392. target.Json.Append('\n');
  393. target.Json.Append(_indent);
  394. }
  395. if (SerializeJSONProperty(i, value, ref target) == SerializeResult.Undefined)
  396. {
  397. target.Json.Append(JsString.NullString);
  398. }
  399. hasPrevious = true;
  400. }
  401. if (!hasPrevious)
  402. {
  403. _stack.Exit();
  404. _indent = stepback;
  405. target.Json.Append("[]");
  406. return;
  407. }
  408. if (_gap.Length > 0)
  409. {
  410. target.Json.Append('\n');
  411. target.Json.Append(stepback);
  412. }
  413. target.Json.Append(']');
  414. _stack.Exit();
  415. _indent = stepback;
  416. }
  417. /// <summary>
  418. /// https://tc39.es/ecma262/#sec-serializejsonobject
  419. /// </summary>
  420. private void SerializeJSONObject(ObjectInstance value, ref SerializerState target)
  421. {
  422. var enumeration = _propertyList is null
  423. ? PropertyEnumeration.FromObjectInstance(value)
  424. : PropertyEnumeration.FromList(_propertyList);
  425. if (enumeration.IsEmpty)
  426. {
  427. target.Json.Append("{}");
  428. return;
  429. }
  430. _stack.Enter(value);
  431. var stepback = _indent;
  432. if (_gap.Length > 0)
  433. {
  434. _indent += _gap;
  435. }
  436. const char separator = ',';
  437. var hasPrevious = false;
  438. for (var i = 0; i < enumeration.Keys.Count; i++)
  439. {
  440. var p = enumeration.Keys[i];
  441. int position = target.Json.Length;
  442. if (hasPrevious)
  443. {
  444. target.Json.Append(separator);
  445. }
  446. else
  447. {
  448. target.Json.Append('{');
  449. }
  450. if (_gap.Length > 0)
  451. {
  452. target.Json.Append('\n');
  453. target.Json.Append(_indent);
  454. }
  455. QuoteJSONString(p.ToString(), target.Json);
  456. target.Json.Append(':');
  457. if (_gap.Length > 0)
  458. {
  459. target.Json.Append(' ');
  460. }
  461. if (SerializeJSONProperty(p, value, ref target) == SerializeResult.Undefined)
  462. {
  463. target.Json.Length = position;
  464. }
  465. else
  466. {
  467. hasPrevious = true;
  468. }
  469. }
  470. if (!hasPrevious)
  471. {
  472. _stack.Exit();
  473. _indent = stepback;
  474. target.Json.Append("{}");
  475. return;
  476. }
  477. if (_gap.Length > 0)
  478. {
  479. target.Json.Append('\n');
  480. target.Json.Append(stepback);
  481. }
  482. target.Json.Append('}');
  483. _stack.Exit();
  484. _indent = stepback;
  485. }
  486. private readonly ref struct SerializerState
  487. {
  488. public SerializerState(StringBuilder jsonBuilder)
  489. {
  490. Json = jsonBuilder;
  491. DtoaBuilder = TypeConverter.CreateDtoaBuilderForDouble();
  492. }
  493. public readonly StringBuilder Json;
  494. public readonly DtoaBuilder DtoaBuilder;
  495. }
  496. private enum SerializeResult
  497. {
  498. NotUndefined,
  499. Undefined
  500. }
  501. private readonly struct PropertyEnumeration
  502. {
  503. private PropertyEnumeration(List<JsValue> keys, bool isEmpty)
  504. {
  505. Keys = keys;
  506. IsEmpty = isEmpty;
  507. }
  508. public static PropertyEnumeration FromList(List<JsValue> keys)
  509. => new PropertyEnumeration(keys, keys.Count == 0);
  510. public static PropertyEnumeration FromObjectInstance(ObjectInstance instance)
  511. {
  512. var allKeys = instance.GetOwnPropertyKeys(Types.String);
  513. RemoveUnserializableProperties(instance, allKeys);
  514. return new PropertyEnumeration(allKeys, allKeys.Count == 0);
  515. }
  516. private static void RemoveUnserializableProperties(ObjectInstance instance, List<JsValue> keys)
  517. {
  518. for (var i = 0; i < keys.Count; i++)
  519. {
  520. var key = keys[i];
  521. var desc = instance.GetOwnProperty(key);
  522. if (desc == PropertyDescriptor.Undefined || !desc.Enumerable)
  523. {
  524. keys.RemoveAt(i);
  525. i--;
  526. }
  527. }
  528. }
  529. public readonly List<JsValue> Keys;
  530. public readonly bool IsEmpty;
  531. }
  532. }
  533. }